Skip to content

Commit b48403d

Browse files
Copilotsandersn
andcommitted
Fix type argument panic and improve type node detection - partial fix for literal null/undefined issue
Co-authored-by: sandersn <293473+sandersn@users.noreply.github.com>
1 parent 74416e1 commit b48403d

File tree

167 files changed

+4683
-309
lines changed

Some content is hidden

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

167 files changed

+4683
-309
lines changed

internal/parser/js_diagnostics.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,41 @@ func (v *jsDiagnosticsVisitor) walkNodeForJSDiagnosticsWorker(node *ast.Node) bo
6060
return false
6161
}
6262

63-
// Check for type nodes - these are always illegal in JS
64-
if ast.IsTypeNode(node) {
63+
// Check for specific TypeScript-only constructs
64+
switch node.Kind {
65+
// Type nodes that are always illegal in JS (but exclude some that are valid as literals/expressions)
66+
case ast.KindAnyKeyword, ast.KindUnknownKeyword, ast.KindNumberKeyword, ast.KindBigIntKeyword,
67+
ast.KindStringKeyword, ast.KindBooleanKeyword, ast.KindSymbolKeyword, ast.KindObjectKeyword,
68+
ast.KindNeverKeyword, ast.KindIntrinsicKeyword:
6569
v.diagnostics = append(v.diagnostics, v.createDiagnosticForNode(node, diagnostics.Type_annotations_can_only_be_used_in_TypeScript_files))
6670
return false
71+
case ast.KindVoidKeyword:
72+
// Only flag void when not used as void expression
73+
if parent.Kind != ast.KindVoidExpression {
74+
v.diagnostics = append(v.diagnostics, v.createDiagnosticForNode(node, diagnostics.Type_annotations_can_only_be_used_in_TypeScript_files))
75+
return false
76+
}
77+
case ast.KindNullKeyword, ast.KindUndefinedKeyword:
78+
// Only flag null/undefined when used in type context, not as literal values
79+
if ast.IsPartOfTypeNode(node) {
80+
v.diagnostics = append(v.diagnostics, v.createDiagnosticForNode(node, diagnostics.Type_annotations_can_only_be_used_in_TypeScript_files))
81+
return false
82+
}
83+
case ast.KindExpressionWithTypeArguments:
84+
// Only flag when it's actually part of a type expression (implements clause), not extends clause
85+
if ast.IsPartOfTypeNode(node) {
86+
v.diagnostics = append(v.diagnostics, v.createDiagnosticForNode(node, diagnostics.Type_annotations_can_only_be_used_in_TypeScript_files))
87+
return false
88+
}
89+
}
90+
91+
// Check for type nodes in the type node range
92+
if node.Kind >= ast.KindFirstTypeNode && node.Kind <= ast.KindLastTypeNode {
93+
// Skip ExpressionWithTypeArguments as it's handled above
94+
if node.Kind != ast.KindExpressionWithTypeArguments {
95+
v.diagnostics = append(v.diagnostics, v.createDiagnosticForNode(node, diagnostics.Type_annotations_can_only_be_used_in_TypeScript_files))
96+
return false
97+
}
6798
}
6899

69100
// Check node-specific constructs
@@ -242,8 +273,14 @@ func (v *jsDiagnosticsVisitor) getTypeArguments(node *ast.Node) *ast.NodeList {
242273

243274
var typeArguments *ast.NodeList
244275

245-
// Use the built-in TypeArgumentList() method for supported nodes
246-
if ast.IsCallLikeExpression(node) || node.Kind == ast.KindExpressionWithTypeArguments {
276+
// Handle specific node types that can have type arguments
277+
switch node.Kind {
278+
case ast.KindCallExpression, ast.KindNewExpression, ast.KindTaggedTemplateExpression,
279+
ast.KindJsxOpeningElement, ast.KindJsxSelfClosingElement:
280+
if ast.IsCallLikeExpression(node) {
281+
typeArguments = node.TypeArgumentList()
282+
}
283+
case ast.KindExpressionWithTypeArguments:
247284
typeArguments = node.TypeArgumentList()
248285
}
249286

testdata/baselines/reference/compiler/superCallInJSWithWrongBaseTypeArgumentCount1(strict=false).errors.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
error TS5055: Cannot write file 'b.js' because it would overwrite input file.
22
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
3-
b.js(9,27): error TS8011: Type arguments can only be used in TypeScript files.
4-
b.js(15,27): error TS8011: Type arguments can only be used in TypeScript files.
3+
b.js(3,25): error TS8010: Type annotations can only be used in TypeScript files.
4+
b.js(9,25): error TS8010: Type annotations can only be used in TypeScript files.
5+
b.js(15,25): error TS8010: Type annotations can only be used in TypeScript files.
56

67

78
!!! error TS5055: Cannot write file 'b.js' because it would overwrite input file.
89
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
910
==== a.ts (0 errors) ====
1011
export class A<T> {}
1112

12-
==== b.js (2 errors) ====
13+
==== b.js (3 errors) ====
1314
import { A } from './a.js';
1415

1516
export class B1 extends A {
17+
~
18+
!!! error TS8010: Type annotations can only be used in TypeScript files.
1619
constructor() {
1720
super();
1821
}
1922
}
2023

2124
export class B2 extends A<string> {
22-
~~~~~~
23-
!!! error TS8011: Type arguments can only be used in TypeScript files.
25+
~~~~~~~~~
26+
!!! error TS8010: Type annotations can only be used in TypeScript files.
2427
constructor() {
2528
super();
2629
}
2730
}
2831

2932
export class B3 extends A<string, string> {
30-
~~~~~~~~~~~~~~
31-
!!! error TS8011: Type arguments can only be used in TypeScript files.
33+
~~~~~~~~~~~~~~~~~
34+
!!! error TS8010: Type annotations can only be used in TypeScript files.
3235
constructor() {
3336
super();
3437
}

testdata/baselines/reference/compiler/superCallInJSWithWrongBaseTypeArgumentCount1(strict=true).errors.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
11
error TS5055: Cannot write file 'b.js' because it would overwrite input file.
22
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
3+
b.js(3,25): error TS8010: Type annotations can only be used in TypeScript files.
34
b.js(3,25): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
4-
b.js(9,27): error TS8011: Type arguments can only be used in TypeScript files.
5+
b.js(9,25): error TS8010: Type annotations can only be used in TypeScript files.
6+
b.js(15,25): error TS8010: Type annotations can only be used in TypeScript files.
57
b.js(15,25): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
6-
b.js(15,27): error TS8011: Type arguments can only be used in TypeScript files.
78

89

910
!!! error TS5055: Cannot write file 'b.js' because it would overwrite input file.
1011
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
1112
==== a.ts (0 errors) ====
1213
export class A<T> {}
1314

14-
==== b.js (4 errors) ====
15+
==== b.js (5 errors) ====
1516
import { A } from './a.js';
1617

1718
export class B1 extends A {
1819
~
20+
!!! error TS8010: Type annotations can only be used in TypeScript files.
21+
~
1922
!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
2023
constructor() {
2124
super();
2225
}
2326
}
2427

2528
export class B2 extends A<string> {
26-
~~~~~~
27-
!!! error TS8011: Type arguments can only be used in TypeScript files.
29+
~~~~~~~~~
30+
!!! error TS8010: Type annotations can only be used in TypeScript files.
2831
constructor() {
2932
super();
3033
}
3134
}
3235

3336
export class B3 extends A<string, string> {
3437
~~~~~~~~~~~~~~~~~
38+
!!! error TS8010: Type annotations can only be used in TypeScript files.
39+
~~~~~~~~~~~~~~~~~
3540
!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
36-
~~~~~~~~~~~~~~
37-
!!! error TS8011: Type arguments can only be used in TypeScript files.
3841
constructor() {
3942
super();
4043
}

testdata/baselines/reference/compiler/superCallInJSWithWrongBaseTypeArgumentCount2(strict=false).errors.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
11
error TS5055: Cannot write file 'b.js' because it would overwrite input file.
22
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
3+
b.js(4,25): error TS8010: Type annotations can only be used in TypeScript files.
4+
b.js(11,25): error TS8010: Type annotations can only be used in TypeScript files.
5+
b.js(18,25): error TS8010: Type annotations can only be used in TypeScript files.
36

47

58
!!! error TS5055: Cannot write file 'b.js' because it would overwrite input file.
69
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
710
==== a.ts (0 errors) ====
811
export class A<T> {}
912

10-
==== b.js (0 errors) ====
13+
==== b.js (3 errors) ====
1114
import { A } from './a.js';
1215

1316
/** @extends {A} */
1417
export class B1 extends A {
18+
~
19+
!!! error TS8010: Type annotations can only be used in TypeScript files.
1520
constructor() {
1621
super();
1722
}
1823
}
1924

2025
/** @extends {A<string>} */
2126
export class B2 extends A {
27+
~
28+
!!! error TS8010: Type annotations can only be used in TypeScript files.
2229
constructor() {
2330
super();
2431
}
2532
}
2633

2734
/** @extends {A<string, string>} */
2835
export class B3 extends A {
36+
~
37+
!!! error TS8010: Type annotations can only be used in TypeScript files.
2938
constructor() {
3039
super();
3140
}

testdata/baselines/reference/compiler/superCallInJSWithWrongBaseTypeArgumentCount2(strict=true).errors.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error TS5055: Cannot write file 'b.js' because it would overwrite input file.
22
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig.
3+
b.js(4,25): error TS8010: Type annotations can only be used in TypeScript files.
34
b.js(4,25): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
5+
b.js(11,25): error TS8010: Type annotations can only be used in TypeScript files.
6+
b.js(18,25): error TS8010: Type annotations can only be used in TypeScript files.
47
b.js(18,25): error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
58

69

@@ -9,12 +12,14 @@ b.js(18,25): error TS8026: Expected A<T> type arguments; provide these with an '
912
==== a.ts (0 errors) ====
1013
export class A<T> {}
1114

12-
==== b.js (2 errors) ====
15+
==== b.js (5 errors) ====
1316
import { A } from './a.js';
1417

1518
/** @extends {A} */
1619
export class B1 extends A {
1720
~
21+
!!! error TS8010: Type annotations can only be used in TypeScript files.
22+
~
1823
!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
1924
constructor() {
2025
super();
@@ -23,6 +28,8 @@ b.js(18,25): error TS8026: Expected A<T> type arguments; provide these with an '
2328

2429
/** @extends {A<string>} */
2530
export class B2 extends A {
31+
~
32+
!!! error TS8010: Type annotations can only be used in TypeScript files.
2633
constructor() {
2734
super();
2835
}
@@ -31,6 +38,8 @@ b.js(18,25): error TS8026: Expected A<T> type arguments; provide these with an '
3138
/** @extends {A<string, string>} */
3239
export class B3 extends A {
3340
~
41+
!!! error TS8010: Type annotations can only be used in TypeScript files.
42+
~
3443
!!! error TS8026: Expected A<T> type arguments; provide these with an '@extends' tag.
3544
constructor() {
3645
super();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/a.js(7,17): error TS8010: Type annotations can only be used in TypeScript files.
2+
3+
4+
==== /a.js (1 errors) ====
5+
class A {
6+
get arguments() {
7+
return { bar: {} };
8+
}
9+
}
10+
11+
class B extends A {
12+
~
13+
!!! error TS8010: Type annotations can only be used in TypeScript files.
14+
/**
15+
* Constructor
16+
*
17+
* @param {object} [foo={}]
18+
*/
19+
constructor(foo = {}) {
20+
super();
21+
22+
/**
23+
* @type object
24+
*/
25+
this.foo = foo;
26+
27+
/**
28+
* @type object
29+
*/
30+
this.bar = super.arguments.foo;
31+
}
32+
}
33+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/a.js(7,17): error TS8010: Type annotations can only be used in TypeScript files.
2+
3+
4+
==== /a.js (1 errors) ====
5+
class A {
6+
get arguments() {
7+
return { bar: {} };
8+
}
9+
}
10+
11+
class B extends A {
12+
~
13+
!!! error TS8010: Type annotations can only be used in TypeScript files.
14+
/**
15+
* @param {object} [foo={}]
16+
*/
17+
m(foo = {}) {
18+
/**
19+
* @type object
20+
*/
21+
this.x = foo;
22+
23+
/**
24+
* @type object
25+
*/
26+
this.y = super.arguments.bar;
27+
}
28+
}
29+

testdata/baselines/reference/submodule/compiler/checkIndexConstraintOfJavascriptClassExpression.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
error TS5102: Option 'outFile' has been removed. Please remove it from your configuration.
22
weird.js(1,1): error TS2552: Cannot find name 'someFunction'. Did you mean 'Function'?
33
weird.js(1,23): error TS7006: Parameter 'BaseClass' implicitly has an 'any' type.
4+
weird.js(4,25): error TS8010: Type annotations can only be used in TypeScript files.
45
weird.js(9,17): error TS7006: Parameter 'error' implicitly has an 'any' type.
56

67

78
!!! error TS5102: Option 'outFile' has been removed. Please remove it from your configuration.
8-
==== weird.js (3 errors) ====
9+
==== weird.js (4 errors) ====
910
someFunction(function(BaseClass) {
1011
~~~~~~~~~~~~
1112
!!! error TS2552: Cannot find name 'someFunction'. Did you mean 'Function'?
@@ -15,6 +16,8 @@ weird.js(9,17): error TS7006: Parameter 'error' implicitly has an 'any' type.
1516
'use strict';
1617
const DEFAULT_MESSAGE = "nop!";
1718
class Hello extends BaseClass {
19+
~~~~~~~~~
20+
!!! error TS8010: Type annotations can only be used in TypeScript files.
1821
constructor() {
1922
super();
2023
this.foo = "bar";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
a.js(11,17): error TS8010: Type annotations can only be used in TypeScript files.
2+
3+
4+
==== a.js (1 errors) ====
5+
// @ts-check
6+
class A {
7+
constructor() {
8+
9+
}
10+
foo() {
11+
return 4;
12+
}
13+
}
14+
15+
class B extends A {
16+
~
17+
!!! error TS8010: Type annotations can only be used in TypeScript files.
18+
constructor() {
19+
super();
20+
this.foo = () => 3;
21+
}
22+
}
23+
24+
const i = new B();
25+
i.foo();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
b.js(1,17): error TS8010: Type annotations can only be used in TypeScript files.
2+
3+
4+
==== a.ts (0 errors) ====
5+
declare var Err: any
6+
class A extends Err {
7+
payload: string
8+
constructor() {
9+
super(1,2,3,3,4,56)
10+
super.unknown
11+
super['unknown']
12+
}
13+
process() {
14+
return this.payload + "!";
15+
}
16+
}
17+
18+
var o = {
19+
m() {
20+
super.unknown
21+
}
22+
}
23+
==== b.js (1 errors) ====
24+
class B extends Err {
25+
~~~
26+
!!! error TS8010: Type annotations can only be used in TypeScript files.
27+
constructor() {
28+
super()
29+
this.wat = 12
30+
}
31+
f() {
32+
this.wat
33+
this.wit
34+
this['wot']
35+
super.alsoBad
36+
}
37+
}
38+

0 commit comments

Comments
 (0)