Skip to content

Commit cd3843e

Browse files
committed
fix: allow override modifier in class that extends another class
Fixes TyrealHu/acorn-typescript#56
1 parent bc39dcb commit cd3843e

File tree

7 files changed

+178
-9
lines changed

7 files changed

+178
-9
lines changed

.changeset/light-carpets-spend.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: allow override modifier in class that extends another class

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3776,7 +3776,7 @@ export function tsPlugin(options?: {
37763776
}
37773777

37783778
if ((node as any).override) {
3779-
if (constructorAllowsSuper) {
3779+
if (!constructorAllowsSuper) {
37803780
this.raise(node.start, TypeScriptError.OverrideNotInSubClass);
37813781
}
37823782
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This member cannot have an 'override' modifier because its containing class does not extend another class. (2:4)

test/class_no_super_override/input.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class A {
2+
override c() {}
3+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
{
2+
"type": "Program",
3+
"start": 0,
4+
"end": 42,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 4,
12+
"column": 0
13+
}
14+
},
15+
"body": [
16+
{
17+
"type": "ClassDeclaration",
18+
"start": 0,
19+
"end": 41,
20+
"loc": {
21+
"start": {
22+
"line": 1,
23+
"column": 0
24+
},
25+
"end": {
26+
"line": 3,
27+
"column": 1
28+
}
29+
},
30+
"id": {
31+
"type": "Identifier",
32+
"start": 6,
33+
"end": 7,
34+
"loc": {
35+
"start": {
36+
"line": 1,
37+
"column": 6
38+
},
39+
"end": {
40+
"line": 1,
41+
"column": 7
42+
}
43+
},
44+
"name": "A"
45+
},
46+
"superClass": {
47+
"type": "Identifier",
48+
"start": 16,
49+
"end": 17,
50+
"loc": {
51+
"start": {
52+
"line": 1,
53+
"column": 16
54+
},
55+
"end": {
56+
"line": 1,
57+
"column": 17
58+
}
59+
},
60+
"name": "B"
61+
},
62+
"body": {
63+
"type": "ClassBody",
64+
"start": 18,
65+
"end": 41,
66+
"loc": {
67+
"start": {
68+
"line": 1,
69+
"column": 18
70+
},
71+
"end": {
72+
"line": 3,
73+
"column": 1
74+
}
75+
},
76+
"body": [
77+
{
78+
"type": "MethodDefinition",
79+
"start": 24,
80+
"end": 39,
81+
"loc": {
82+
"start": {
83+
"line": 2,
84+
"column": 4
85+
},
86+
"end": {
87+
"line": 2,
88+
"column": 19
89+
}
90+
},
91+
"override": true,
92+
"static": false,
93+
"computed": false,
94+
"key": {
95+
"type": "Identifier",
96+
"start": 33,
97+
"end": 34,
98+
"loc": {
99+
"start": {
100+
"line": 2,
101+
"column": 13
102+
},
103+
"end": {
104+
"line": 2,
105+
"column": 14
106+
}
107+
},
108+
"name": "c"
109+
},
110+
"kind": "method",
111+
"value": {
112+
"type": "FunctionExpression",
113+
"start": 34,
114+
"end": 39,
115+
"loc": {
116+
"start": {
117+
"line": 2,
118+
"column": 14
119+
},
120+
"end": {
121+
"line": 2,
122+
"column": 19
123+
}
124+
},
125+
"id": null,
126+
"expression": false,
127+
"generator": false,
128+
"async": false,
129+
"params": [],
130+
"body": {
131+
"type": "BlockStatement",
132+
"start": 37,
133+
"end": 39,
134+
"loc": {
135+
"start": {
136+
"line": 2,
137+
"column": 17
138+
},
139+
"end": {
140+
"line": 2,
141+
"column": 19
142+
}
143+
},
144+
"body": []
145+
}
146+
}
147+
}
148+
]
149+
}
150+
}
151+
],
152+
"sourceType": "module"
153+
}

test/class_super_override/input.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class A extends B {
2+
override c() {}
3+
}

test/run.test.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,25 @@ describe('tests', () => {
2222
if (fs.existsSync(expected_path)) {
2323
it(dirent.name, () => {
2424
const input_code = fs.readFileSync(input_path, 'utf-8');
25-
const expected_result = JSON.parse(fs.readFileSync(expected_path, 'utf-8'));
25+
const expected_result = fs.readFileSync(expected_path, 'utf-8');
2626

2727
const parsed_result = dirent.name.startsWith('jsx_')
2828
? parseJsxSource(input_code)
2929
: dirent.name.startsWith('dts_')
3030
? parseDtsSource(input_code)
3131
: parseSource(input_code);
3232

33-
try {
34-
equalNode(parsed_result, expected_result);
35-
} catch (e) {
36-
if (process.env.UPDATE_SNAPSHOT) {
37-
fs.writeFileSync(expected_path, JSON.stringify(parsed_result, null, 2));
38-
} else {
39-
throw e;
33+
if (!expected_result) {
34+
fs.writeFileSync(expected_path, JSON.stringify(parsed_result, null, 2));
35+
} else {
36+
try {
37+
equalNode(parsed_result, JSON.parse(expected_result));
38+
} catch (e) {
39+
if (process.env.UPDATE_SNAPSHOT) {
40+
fs.writeFileSync(expected_path, JSON.stringify(parsed_result, null, 2));
41+
} else {
42+
throw e;
43+
}
4044
}
4145
}
4246
});

0 commit comments

Comments
 (0)