Skip to content

Commit f35b64e

Browse files
committed
fix: correct end position, remove obsolete index
Fixes TyrealHu/acorn-typescript#54
1 parent 646ddce commit f35b64e

File tree

169 files changed

+8574
-13752
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+8574
-13752
lines changed

.changeset/tame-words-cut.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/acorn-typescript': patch
3+
---
4+
5+
fix: correct end position, remove obsolete index

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ Version 1.0 of `@sveltejs/acorn-typescript` has some breaking changes compared t
2828
- ESM only (no CJS build)
2929
- JSX parsing is disabled by default now (you can turn it back on by passing `{ jsx: true }`)
3030
- `allowSatisfies` option was removed, `satisfies` operator is always parsed now
31+
- `index` on `loc` was removed
3132

3233
Changelog of the project this originated from: https://github.com/TyrealHu/acorn-typescript/CHANGELOG.md

src/index.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,14 @@ export function tsPlugin(options?: {
387387
return type;
388388
}
389389

390-
resetEndLocation(node: any, endLoc: Position = this.lastTokEndLoc): void {
391-
node.end = endLoc.column;
390+
resetEndLocation(
391+
node: any,
392+
endPos: number = this.lastTokEnd,
393+
endLoc: Position = this.lastTokEndLoc
394+
): void {
395+
node.end = endPos;
392396
node.loc.end = endLoc;
393-
if (this.options.ranges) node.range[1] = endLoc.column;
397+
if (this.options.ranges) node.range[1] = endPos;
394398
}
395399

396400
startNodeAtNode(type: Node): any {
@@ -758,7 +762,7 @@ export function tsPlugin(options?: {
758762
}
759763

760764
hasPrecedingLineBreak(): boolean {
761-
return lineBreak.test(this.input.slice(this.lastTokEndLoc.index, this.start));
765+
return lineBreak.test(this.input.slice(this.lastTokEnd, this.start));
762766
}
763767

764768
createIdentifier(node: Omit<any, 'type'>, name: string): any {
@@ -3210,7 +3214,11 @@ export function tsPlugin(options?: {
32103214

32113215
typeCastToParameter(node: any): any {
32123216
node.expression.typeAnnotation = node.typeAnnotation;
3213-
this.resetEndLocation(node.expression, node.typeAnnotation.end);
3217+
this.resetEndLocation(
3218+
node.expression,
3219+
node.typeAnnotation.end,
3220+
node.typeAnnotation.loc?.end
3221+
);
32143222
return node.expression;
32153223
}
32163224

@@ -4302,23 +4310,6 @@ export function tsPlugin(options?: {
43024310
}
43034311
}
43044312

4305-
curPosition() {
4306-
if (this.options.locations) {
4307-
const position = super.curPosition();
4308-
Object.defineProperty(position, 'offset', {
4309-
get() {
4310-
return function (n: number) {
4311-
const np = new (_acorn as any).Position(this.line, this.column + n);
4312-
np['index'] = this['index'] + n;
4313-
return np;
4314-
};
4315-
}
4316-
});
4317-
position['index'] = this.pos;
4318-
return position;
4319-
}
4320-
}
4321-
43224313
parseBindingAtom(): any {
43234314
switch (this.type) {
43244315
case tt._this:

test/arrow-function_type_test_assignment_pattern/expected.json

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
"loc": {
66
"start": {
77
"line": 1,
8-
"column": 0,
9-
"index": 0
8+
"column": 0
109
},
1110
"end": {
1211
"line": 1,
13-
"column": 20,
14-
"index": 20
12+
"column": 20
1513
}
1614
},
1715
"body": [
@@ -22,13 +20,11 @@
2220
"loc": {
2321
"start": {
2422
"line": 1,
25-
"column": 0,
26-
"index": 0
23+
"column": 0
2724
},
2825
"end": {
2926
"line": 1,
30-
"column": 20,
31-
"index": 20
27+
"column": 20
3228
}
3329
},
3430
"expression": {
@@ -38,13 +34,11 @@
3834
"loc": {
3935
"start": {
4036
"line": 1,
41-
"column": 0,
42-
"index": 0
37+
"column": 0
4338
},
4439
"end": {
4540
"line": 1,
46-
"column": 20,
47-
"index": 20
41+
"column": 20
4842
}
4943
},
5044
"returnType": {
@@ -54,13 +48,11 @@
5448
"loc": {
5549
"start": {
5650
"line": 1,
57-
"column": 8,
58-
"index": 8
51+
"column": 8
5952
},
6053
"end": {
6154
"line": 1,
62-
"column": 14,
63-
"index": 14
55+
"column": 14
6456
}
6557
},
6658
"typeAnnotation": {
@@ -70,13 +62,11 @@
7062
"loc": {
7163
"start": {
7264
"line": 1,
73-
"column": 10,
74-
"index": 10
65+
"column": 10
7566
},
7667
"end": {
7768
"line": 1,
78-
"column": 14,
79-
"index": 14
69+
"column": 14
8070
}
8171
}
8272
}
@@ -93,13 +83,11 @@
9383
"loc": {
9484
"start": {
9585
"line": 1,
96-
"column": 1,
97-
"index": 1
86+
"column": 1
9887
},
9988
"end": {
10089
"line": 1,
101-
"column": 7,
102-
"index": 7
90+
"column": 7
10391
}
10492
},
10593
"left": {
@@ -109,13 +97,11 @@
10997
"loc": {
11098
"start": {
11199
"line": 1,
112-
"column": 1,
113-
"index": 1
100+
"column": 1
114101
},
115102
"end": {
116103
"line": 1,
117-
"column": 2,
118-
"index": 2
104+
"column": 2
119105
}
120106
},
121107
"name": "x"
@@ -127,13 +113,11 @@
127113
"loc": {
128114
"start": {
129115
"line": 1,
130-
"column": 5,
131-
"index": 5
116+
"column": 5
132117
},
133118
"end": {
134119
"line": 1,
135-
"column": 7,
136-
"index": 7
120+
"column": 7
137121
}
138122
},
139123
"value": 42,
@@ -148,13 +132,11 @@
148132
"loc": {
149133
"start": {
150134
"line": 1,
151-
"column": 18,
152-
"index": 18
135+
"column": 18
153136
},
154137
"end": {
155138
"line": 1,
156-
"column": 20,
157-
"index": 20
139+
"column": 20
158140
}
159141
},
160142
"body": []

test/arrow-function_type_test_async_+_rest_parameter/expected.json

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
"loc": {
66
"start": {
77
"line": 1,
8-
"column": 0,
9-
"index": 0
8+
"column": 0
109
},
1110
"end": {
1211
"line": 1,
13-
"column": 36,
14-
"index": 36
12+
"column": 36
1513
}
1614
},
1715
"body": [
@@ -22,13 +20,11 @@
2220
"loc": {
2321
"start": {
2422
"line": 1,
25-
"column": 0,
26-
"index": 0
23+
"column": 0
2724
},
2825
"end": {
2926
"line": 1,
30-
"column": 36,
31-
"index": 36
27+
"column": 36
3228
}
3329
},
3430
"declarations": [
@@ -39,13 +35,11 @@
3935
"loc": {
4036
"start": {
4137
"line": 1,
42-
"column": 6,
43-
"index": 6
38+
"column": 6
4439
},
4540
"end": {
4641
"line": 1,
47-
"column": 36,
48-
"index": 36
42+
"column": 36
4943
}
5044
},
5145
"id": {
@@ -55,13 +49,11 @@
5549
"loc": {
5650
"start": {
5751
"line": 1,
58-
"column": 6,
59-
"index": 6
52+
"column": 6
6053
},
6154
"end": {
6255
"line": 1,
63-
"column": 7,
64-
"index": 7
56+
"column": 7
6557
}
6658
},
6759
"name": "f"
@@ -73,13 +65,11 @@
7365
"loc": {
7466
"start": {
7567
"line": 1,
76-
"column": 10,
77-
"index": 10
68+
"column": 10
7869
},
7970
"end": {
8071
"line": 1,
81-
"column": 36,
82-
"index": 36
72+
"column": 36
8373
}
8474
},
8575
"id": null,
@@ -94,13 +84,11 @@
9484
"loc": {
9585
"start": {
9686
"line": 1,
97-
"column": 17,
98-
"index": 17
87+
"column": 17
9988
},
10089
"end": {
10190
"line": 1,
102-
"column": 24,
103-
"index": 24
91+
"column": 24
10492
}
10593
},
10694
"argument": {
@@ -110,13 +98,11 @@
11098
"loc": {
11199
"start": {
112100
"line": 1,
113-
"column": 20,
114-
"index": 20
101+
"column": 20
115102
},
116103
"end": {
117104
"line": 1,
118-
"column": 24,
119-
"index": 24
105+
"column": 24
120106
}
121107
},
122108
"name": "args"
@@ -128,13 +114,11 @@
128114
"loc": {
129115
"start": {
130116
"line": 1,
131-
"column": 24,
132-
"index": 24
117+
"column": 24
133118
},
134119
"end": {
135120
"line": 1,
136-
"column": 29,
137-
"index": 29
121+
"column": 29
138122
}
139123
},
140124
"typeAnnotation": {
@@ -144,13 +128,11 @@
144128
"loc": {
145129
"start": {
146130
"line": 1,
147-
"column": 26,
148-
"index": 26
131+
"column": 26
149132
},
150133
"end": {
151134
"line": 1,
152-
"column": 29,
153-
"index": 29
135+
"column": 29
154136
}
155137
}
156138
}
@@ -164,13 +146,11 @@
164146
"loc": {
165147
"start": {
166148
"line": 1,
167-
"column": 34,
168-
"index": 34
149+
"column": 34
169150
},
170151
"end": {
171152
"line": 1,
172-
"column": 36,
173-
"index": 36
153+
"column": 36
174154
}
175155
},
176156
"body": []

0 commit comments

Comments
 (0)