Skip to content

test(ts-estree): add missing test cases #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions packages/parser/tests/lib/__snapshots__/tsx.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,56 @@ Object {
}
`;

exports[`TSX fixtures/generic-jsx-opening-element.src 1`] = `
Object {
"$id": 1,
"block": Object {
"range": Array [
0,
46,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 0,
"block": Object {
"range": Array [
0,
46,
],
"type": "Program",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [],
"type": "module",
"upperScope": Object {
"$ref": 1,
},
"variableMap": Object {},
"variableScope": Object {
"$ref": 0,
},
"variables": Array [],
},
],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 1,
},
"variables": Array [],
}
`;

exports[`TSX fixtures/react-typed-props.src 1`] = `
Object {
"$id": 7,
Expand Down
50 changes: 50 additions & 0 deletions packages/parser/tests/lib/__snapshots__/typescript.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32512,6 +32512,56 @@ Object {
}
`;

exports[`typescript fixtures/errorRecovery/interface-method-readonly.src 1`] = `
Object {
"$id": 1,
"block": Object {
"range": Array [
0,
51,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 0,
"block": Object {
"range": Array [
0,
51,
],
"type": "Program",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [],
"type": "module",
"upperScope": Object {
"$ref": 1,
},
"variableMap": Object {},
"variableScope": Object {
"$ref": 0,
},
"variables": Array [],
},
],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 1,
},
"variables": Array [],
}
`;

exports[`typescript fixtures/errorRecovery/interface-method-static.src 1`] = `
Object {
"$id": 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<MyComponent<number> data={12}></MyComponent>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface Foo {
readonly g(bar: string): void;
}
7 changes: 3 additions & 4 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export class Converter {
*/
private convertBodyExpressions(
nodes: ts.NodeArray<ts.Statement>,
parent: ts.Node,
parent: ts.SourceFile | ts.Block | ts.ModuleBlock,
): any[] {
let allowDirectives = canContainDirective(parent);

Expand Down Expand Up @@ -1183,7 +1183,7 @@ export class Converter {
} else {
return arrayItem;
}
} else if (parent.kind === SyntaxKind.ObjectBindingPattern) {
} else {
let result: TSESTree.RestElement | TSESTree.Property;
if (node.dotDotDotToken) {
result = this.createNode<TSESTree.RestElement>(node, {
Expand Down Expand Up @@ -1215,7 +1215,6 @@ export class Converter {
}
return result;
}
return null;
}

case SyntaxKind.ArrowFunction: {
Expand Down Expand Up @@ -2360,7 +2359,7 @@ export class Converter {
return this.fixExports(node, result);
}

case SyntaxKind.FirstTypeNode: {
case SyntaxKind.TypePredicate: {
Copy link
Collaborator Author

@armano2 armano2 Feb 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both SyntaxKind.FirstTypeNode and SyntaxKind.TypePredicate have same value (163),
but its better to use node kind instead of "counter"

const result = this.createNode<TSESTree.TSTypePredicate>(node, {
type: AST_NODE_TYPES.TSTypePredicate,
parameterName: this.convertChild(node.parameterName),
Expand Down
37 changes: 17 additions & 20 deletions packages/typescript-estree/src/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,24 @@ export function getLocFor(
* @param node
* @returns returns true if node can contain directive
*/
export function canContainDirective(node: ts.Node): boolean {
switch (node.kind) {
case ts.SyntaxKind.SourceFile:
case ts.SyntaxKind.ModuleBlock:
return true;
case ts.SyntaxKind.Block:
switch (node.parent.kind) {
case ts.SyntaxKind.Constructor:
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.MethodDeclaration:
return true;
default:
return false;
}
default:
return false;
export function canContainDirective(
node: ts.SourceFile | ts.Block | ts.ModuleBlock,
): boolean {
if (node.kind === ts.SyntaxKind.Block) {
switch (node.parent.kind) {
case ts.SyntaxKind.Constructor:
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.MethodDeclaration:
return true;
default:
return false;
}
}
return true;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript-estree/src/semantic-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export function getFirstSemanticOrSyntacticError(
* For our current use-cases this is undesired behavior, so we just suppress it
* and log a a warning.
*/
/* istanbul ignore next */
console.warn(`Warning From TSC: "${e.message}`);
/* istanbul ignore next */
return undefined;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ tester.addFixturePatternConfig('typescript/errorRecovery', {
'empty-type-parameters-in-function-expression',
'empty-type-parameters-in-method',
'empty-type-parameters-in-method-signature',
/**
* Babel correctly errors on this
* TODO: enable error code TS1024
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should enable this error code in separate PR..

*/
'interface-method-readonly',
],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

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

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

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/tsx/react-typed-props.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

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"`;
Expand Down Expand Up @@ -2287,6 +2289,8 @@ Object {
}
`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/interface-method-readonly.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/interface-method-static.src 1`] = `
Object {
"column": 2,
Expand Down
Loading