Skip to content

Commit 1b3d31e

Browse files
authored
test(ts-estree): add missing test cases (typescript-eslint#300)
* refactor(ts-estree): improve types in convertBodyExpressions * refactor(ts-estree): change FirstTypeNode to TypePredicate * test(ts-estree): add missing test cases * chore(ts-estree): disable coverage for Debug.fail * refactor(ts-estree): make sure that BindingElement is always converted
1 parent 1d9ca84 commit 1b3d31e

File tree

11 files changed

+1101
-24
lines changed

11 files changed

+1101
-24
lines changed

packages/parser/tests/lib/__snapshots__/tsx.ts.snap

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,56 @@ Object {
5050
}
5151
`;
5252

53+
exports[`TSX fixtures/generic-jsx-opening-element.src 1`] = `
54+
Object {
55+
"$id": 1,
56+
"block": Object {
57+
"range": Array [
58+
0,
59+
46,
60+
],
61+
"type": "Program",
62+
},
63+
"childScopes": Array [
64+
Object {
65+
"$id": 0,
66+
"block": Object {
67+
"range": Array [
68+
0,
69+
46,
70+
],
71+
"type": "Program",
72+
},
73+
"childScopes": Array [],
74+
"functionExpressionScope": false,
75+
"isStrict": true,
76+
"references": Array [],
77+
"throughReferences": Array [],
78+
"type": "module",
79+
"upperScope": Object {
80+
"$ref": 1,
81+
},
82+
"variableMap": Object {},
83+
"variableScope": Object {
84+
"$ref": 0,
85+
},
86+
"variables": Array [],
87+
},
88+
],
89+
"functionExpressionScope": false,
90+
"isStrict": false,
91+
"references": Array [],
92+
"throughReferences": Array [],
93+
"type": "global",
94+
"upperScope": null,
95+
"variableMap": Object {},
96+
"variableScope": Object {
97+
"$ref": 1,
98+
},
99+
"variables": Array [],
100+
}
101+
`;
102+
53103
exports[`TSX fixtures/react-typed-props.src 1`] = `
54104
Object {
55105
"$id": 7,

packages/parser/tests/lib/__snapshots__/typescript.ts.snap

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32512,6 +32512,56 @@ Object {
3251232512
}
3251332513
`;
3251432514

32515+
exports[`typescript fixtures/errorRecovery/interface-method-readonly.src 1`] = `
32516+
Object {
32517+
"$id": 1,
32518+
"block": Object {
32519+
"range": Array [
32520+
0,
32521+
51,
32522+
],
32523+
"type": "Program",
32524+
},
32525+
"childScopes": Array [
32526+
Object {
32527+
"$id": 0,
32528+
"block": Object {
32529+
"range": Array [
32530+
0,
32531+
51,
32532+
],
32533+
"type": "Program",
32534+
},
32535+
"childScopes": Array [],
32536+
"functionExpressionScope": false,
32537+
"isStrict": true,
32538+
"references": Array [],
32539+
"throughReferences": Array [],
32540+
"type": "module",
32541+
"upperScope": Object {
32542+
"$ref": 1,
32543+
},
32544+
"variableMap": Object {},
32545+
"variableScope": Object {
32546+
"$ref": 0,
32547+
},
32548+
"variables": Array [],
32549+
},
32550+
],
32551+
"functionExpressionScope": false,
32552+
"isStrict": false,
32553+
"references": Array [],
32554+
"throughReferences": Array [],
32555+
"type": "global",
32556+
"upperScope": null,
32557+
"variableMap": Object {},
32558+
"variableScope": Object {
32559+
"$ref": 1,
32560+
},
32561+
"variables": Array [],
32562+
}
32563+
`;
32564+
3251532565
exports[`typescript fixtures/errorRecovery/interface-method-static.src 1`] = `
3251632566
Object {
3251732567
"$id": 1,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<MyComponent<number> data={12}></MyComponent>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Foo {
2+
readonly g(bar: string): void;
3+
}

packages/typescript-estree/src/convert.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export class Converter {
266266
*/
267267
private convertBodyExpressions(
268268
nodes: ts.NodeArray<ts.Statement>,
269-
parent: ts.Node,
269+
parent: ts.SourceFile | ts.Block | ts.ModuleBlock,
270270
): any[] {
271271
let allowDirectives = canContainDirective(parent);
272272

@@ -1183,7 +1183,7 @@ export class Converter {
11831183
} else {
11841184
return arrayItem;
11851185
}
1186-
} else if (parent.kind === SyntaxKind.ObjectBindingPattern) {
1186+
} else {
11871187
let result: TSESTree.RestElement | TSESTree.Property;
11881188
if (node.dotDotDotToken) {
11891189
result = this.createNode<TSESTree.RestElement>(node, {
@@ -1215,7 +1215,6 @@ export class Converter {
12151215
}
12161216
return result;
12171217
}
1218-
return null;
12191218
}
12201219

12211220
case SyntaxKind.ArrowFunction: {
@@ -2360,7 +2359,7 @@ export class Converter {
23602359
return this.fixExports(node, result);
23612360
}
23622361

2363-
case SyntaxKind.FirstTypeNode: {
2362+
case SyntaxKind.TypePredicate: {
23642363
const result = this.createNode<TSESTree.TSTypePredicate>(node, {
23652364
type: AST_NODE_TYPES.TSTypePredicate,
23662365
parameterName: this.convertChild(node.parameterName),

packages/typescript-estree/src/node-utils.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -255,27 +255,24 @@ export function getLocFor(
255255
* @param node
256256
* @returns returns true if node can contain directive
257257
*/
258-
export function canContainDirective(node: ts.Node): boolean {
259-
switch (node.kind) {
260-
case ts.SyntaxKind.SourceFile:
261-
case ts.SyntaxKind.ModuleBlock:
262-
return true;
263-
case ts.SyntaxKind.Block:
264-
switch (node.parent.kind) {
265-
case ts.SyntaxKind.Constructor:
266-
case ts.SyntaxKind.GetAccessor:
267-
case ts.SyntaxKind.SetAccessor:
268-
case ts.SyntaxKind.ArrowFunction:
269-
case ts.SyntaxKind.FunctionExpression:
270-
case ts.SyntaxKind.FunctionDeclaration:
271-
case ts.SyntaxKind.MethodDeclaration:
272-
return true;
273-
default:
274-
return false;
275-
}
276-
default:
277-
return false;
258+
export function canContainDirective(
259+
node: ts.SourceFile | ts.Block | ts.ModuleBlock,
260+
): boolean {
261+
if (node.kind === ts.SyntaxKind.Block) {
262+
switch (node.parent.kind) {
263+
case ts.SyntaxKind.Constructor:
264+
case ts.SyntaxKind.GetAccessor:
265+
case ts.SyntaxKind.SetAccessor:
266+
case ts.SyntaxKind.ArrowFunction:
267+
case ts.SyntaxKind.FunctionExpression:
268+
case ts.SyntaxKind.FunctionDeclaration:
269+
case ts.SyntaxKind.MethodDeclaration:
270+
return true;
271+
default:
272+
return false;
273+
}
278274
}
275+
return true;
279276
}
280277

281278
/**

packages/typescript-estree/src/semantic-errors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export function getFirstSemanticOrSyntacticError(
4444
* For our current use-cases this is undesired behavior, so we just suppress it
4545
* and log a a warning.
4646
*/
47+
/* istanbul ignore next */
4748
console.warn(`Warning From TSC: "${e.message}`);
49+
/* istanbul ignore next */
4850
return undefined;
4951
}
5052
}

packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,11 @@ tester.addFixturePatternConfig('typescript/errorRecovery', {
452452
'empty-type-parameters-in-function-expression',
453453
'empty-type-parameters-in-method',
454454
'empty-type-parameters-in-method-signature',
455+
/**
456+
* Babel correctly errors on this
457+
* TODO: enable error code TS1024
458+
*/
459+
'interface-method-readonly',
455460
],
456461
});
457462

packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e
16211621

16221622
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/tsx/generic-jsx-element.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
16231623

1624+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/tsx/generic-jsx-opening-element.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
1625+
16241626
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/tsx/react-typed-props.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
16251627

16261628
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/babylon-convergence/type-parameter-whitespace-loc.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
@@ -2287,6 +2289,8 @@ Object {
22872289
}
22882290
`;
22892291

2292+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/interface-method-readonly.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
2293+
22902294
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/interface-method-static.src 1`] = `
22912295
Object {
22922296
"column": 2,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy