Skip to content

Commit

Permalink
fix(typescript-estree): error on unexpected jsdoc nodes (#1525)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored and bradzacher committed Jan 26, 2020
1 parent 6d1d2a2 commit c8dfac3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 305 deletions.
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.',
);
}

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

0 comments on commit c8dfac3

Please sign in to comment.