Skip to content

Commit

Permalink
test(ts-estree): add missing test cases (#300)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
armano2 committed Feb 19, 2019
1 parent 1d9ca84 commit 1b3d31e
Show file tree
Hide file tree
Showing 11 changed files with 1,101 additions and 24 deletions.
50 changes: 50 additions & 0 deletions packages/parser/tests/lib/__snapshots__/tsx.ts.snap
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
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
@@ -0,0 +1 @@
<MyComponent<number> data={12}></MyComponent>
@@ -0,0 +1,3 @@
interface Foo {
readonly g(bar: string): void;
}
7 changes: 3 additions & 4 deletions packages/typescript-estree/src/convert.ts
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: {
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
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
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
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
*/
'interface-method-readonly',
],
});

Expand Down
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

0 comments on commit 1b3d31e

Please sign in to comment.