Skip to content
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

fix(typescript-estree): error on unexpected jsdoc nodes #1525

Merged
merged 2 commits into from Jan 26, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -379,14 +379,27 @@ export class Converter {
* property instead of a kind property. Recursively copies all children.
*/
private deeplyCopy(node: TSNode): any {
if (
node.kind >= SyntaxKind.FirstJSDocNode &&
node.kind <= SyntaxKind.LastJSDocNode
) {
throw createError(
this.ast,
node.pos,
'JSDoc types can only be used inside documentation comments.',
);
}
bradzacher marked this conversation as resolved.
Show resolved Hide resolved

const customType = `TS${SyntaxKind[node.kind]}` as AST_NODE_TYPES;

/**
* If the "errorOnUnknownASTType" option is set to true, throw an error,
* otherwise fallback to just including the unknown type as-is.
*/
if (this.options.errorOnUnknownASTType && !AST_NODE_TYPES[customType]) {
throw new Error(`Unknown AST_NODE_TYPE: "${customType}"`);
}

const result = this.createNode<any>(node, {
type: customType,
});
Expand Down
310 changes: 16 additions & 294 deletions packages/typescript-estree/tests/lib/__snapshots__/convert.ts.snap
Expand Up @@ -179,24 +179,6 @@ exports[`convert deeplyCopy should convert node correctly 1`] = `
Object {
"body": Array [
Object {
"id": Object {
"loc": Object {
"end": Object {
"column": 8,
"line": 1,
},
"start": Object {
"column": 5,
"line": 1,
},
},
"name": "foo",
"range": Array [
5,
8,
],
"type": "Identifier",
},
"loc": Object {
"end": Object {
"column": 35,
Expand All @@ -207,294 +189,34 @@ Object {
"line": 1,
},
},
"range": Array [
0,
35,
],
"type": "TSTypeAliasDeclaration",
"typeAnnotation": Object {
"modifiers": undefined,
"name": Object {
"escapedText": "foo",
"loc": Object {
"end": Object {
"column": 35,
"column": 8,
"line": 1,
},
"start": Object {
"column": 11,
"column": 5,
"line": 1,
},
},
"range": Array [
11,
35,
5,
8,
],
"transformFlags": 0,
"type": "TSJSDocNullableType",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 35,
"line": 1,
},
"start": Object {
"column": 11,
"line": 1,
},
},
"range": Array [
11,
35,
],
"type": "TSTypeAnnotation",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 35,
"line": 1,
},
"start": Object {
"column": 12,
"line": 1,
},
},
"range": Array [
12,
35,
],
"type": "TSUnionType",
"types": Array [
Object {
"loc": Object {
"end": Object {
"column": 18,
"line": 1,
},
"start": Object {
"column": 12,
"line": 1,
},
},
"range": Array [
12,
18,
],
"type": "TSTypeReference",
"typeName": Object {
"loc": Object {
"end": Object {
"column": 15,
"line": 1,
},
"start": Object {
"column": 12,
"line": 1,
},
},
"name": "foo",
"range": Array [
12,
15,
],
"type": "Identifier",
},
"typeParameters": Object {
"loc": Object {
"end": Object {
"column": 18,
"line": 1,
},
"start": Object {
"column": 15,
"line": 1,
},
},
"params": Array [
Object {
"loc": Object {
"end": Object {
"column": 17,
"line": 1,
},
"start": Object {
"column": 16,
"line": 1,
},
},
"range": Array [
16,
17,
],
"type": "TSTypeReference",
"typeName": Object {
"loc": Object {
"end": Object {
"column": 17,
"line": 1,
},
"start": Object {
"column": 16,
"line": 1,
},
},
"name": "T",
"range": Array [
16,
17,
],
"type": "Identifier",
},
"typeParameters": undefined,
},
],
"range": Array [
15,
18,
],
"type": "TSTypeParameterInstantiation",
},
},
Object {
"loc": Object {
"end": Object {
"column": 35,
"line": 1,
},
"start": Object {
"column": 21,
"line": 1,
},
},
"range": Array [
21,
35,
],
"transformFlags": 0,
"type": "TSJSDocNullableType",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 35,
"line": 1,
},
"start": Object {
"column": 21,
"line": 1,
},
},
"range": Array [
21,
35,
],
"type": "TSTypeAnnotation",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 35,
"line": 1,
},
"start": Object {
"column": 22,
"line": 1,
},
},
"range": Array [
22,
35,
],
"transformFlags": 0,
"type": "TSJSDocNullableType",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 34,
"line": 1,
},
"start": Object {
"column": 21,
"line": 1,
},
},
"range": Array [
21,
34,
],
"type": "TSTypeAnnotation",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 34,
"line": 1,
},
"start": Object {
"column": 22,
"line": 1,
},
},
"range": Array [
22,
34,
],
"type": "TSParenthesizedType",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 33,
"line": 1,
},
"start": Object {
"column": 23,
"line": 1,
},
},
"params": Array [],
"range": Array [
23,
33,
],
"returnType": Object {
"loc": Object {
"end": Object {
"column": 33,
"line": 1,
},
"start": Object {
"column": 26,
"line": 1,
},
},
"range": Array [
26,
33,
],
"type": "TSTypeAnnotation",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 33,
"line": 1,
},
"start": Object {
"column": 29,
"line": 1,
},
},
"range": Array [
29,
33,
],
"type": "TSVoidKeyword",
},
},
"type": "TSFunctionType",
},
},
},
},
},
},
],
},
},
"type": "TSUnparsedPrologue",
},
"range": Array [
0,
35,
],
"transformFlags": 0,
"type": "TSUnparsedPrologue",
"typeAnnotation": null,
"typeParameters": null,
},
],
"loc": Object {
Expand Down