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

feat(typescript-estree): add new node type TSUnknownJSDocType #1541

Closed
wants to merge 2 commits into from
Closed
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
1,492 changes: 1,492 additions & 0 deletions packages/parser/tests/lib/__snapshots__/typescript.ts.snap

Large diffs are not rendered by default.

@@ -0,0 +1,23 @@
//// [https://github.com/microsoft/TypeScript/blob/566202f55d85e2a61299eb34571354ec1550bdd2/tests/baselines/reference/jsdocDisallowedInTypescript.js]
// grammar error from checker
var ara: Array.<number> = [1,2,3];

function f(x: ?number, y: Array.<number>) {
return x ? x + y[1] : y[0];
}
function hof(ctor: function(new: number, string)) {
return new ctor('hi');
}
function hof2(f: function(this: number, string): string) {
return f(12, 'hullo');
}
var whatevs: * = 1001;
var ques: ? = 'what';
var g: function(number, number): number = (n,m) => n + m;
var most: !string = 'definite';
var postfixdef: number! = 101;
var postfixopt: number? = undefined;

var nns: Array<?number>;
var dns: Array<!number>;
var anys: Array<*>;
26 changes: 15 additions & 11 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -379,17 +379,6 @@ 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;

/**
Expand Down Expand Up @@ -2651,6 +2640,21 @@ export class Converter {
type: AST_NODE_TYPES.TSAbstractKeyword,
});
}
case SyntaxKind.JSDocAllType:
case SyntaxKind.JSDocUnknownType:
case SyntaxKind.JSDocFunctionType:
case SyntaxKind.JSDocNullableType:
case SyntaxKind.JSDocNonNullableType:
return this.createNode<TSESTree.TSUnknownJSDocType>(node, {
type: AST_NODE_TYPES.TSUnknownJSDocType,
kind: ts.SyntaxKind[node.kind] as
| 'JSDocAllType'
| 'JSDocUnknownType'
| 'JSDocFunctionType'
| 'JSDocNullableType'
| 'JSDocNonNullableType',
value: node.getText(this.ast),
});
default:
return this.deeplyCopy(node);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/typescript-estree/src/ts-estree/ast-node-types.ts
Expand Up @@ -161,6 +161,9 @@ export enum AST_NODE_TYPES {
TSUndefinedKeyword = 'TSUndefinedKeyword',
TSUnknownKeyword = 'TSUnknownKeyword',
TSVoidKeyword = 'TSVoidKeyword',

// Invalid jsdoc node types
TSUnknownJSDocType = 'TSUnknownJSDocType',
}

export enum AST_TOKEN_TYPES {
Expand Down
Expand Up @@ -258,6 +258,14 @@ export interface EstreeToTsNodeTypes {
[AST_NODE_TYPES.TSVoidKeyword]: ts.KeywordTypeNode;
[AST_NODE_TYPES.TSUndefinedKeyword]: ts.KeywordTypeNode;

// Invalid nodes
[AST_NODE_TYPES.TSUnknownJSDocType]:
| ts.JSDocAllType
| ts.JSDocUnknownType
| ts.JSDocFunctionType
| ts.JSDocNullableType
| ts.JSDocNonNullableType;

// Unused
[AST_NODE_TYPES.TSAsyncKeyword]: ts.Token<ts.SyntaxKind.AsyncKeyword>;
[AST_NODE_TYPES.TSDeclareKeyword]: ts.Token<ts.SyntaxKind.DeclareKeyword>;
Expand Down
14 changes: 13 additions & 1 deletion packages/typescript-estree/src/ts-estree/ts-estree.ts
Expand Up @@ -295,7 +295,8 @@ export type Node =
| VariableDeclarator
| WhileStatement
| WithStatement
| YieldExpression;
| YieldExpression
| TSUnknownJSDocType;

//////////
// Reusable Unions
Expand Down Expand Up @@ -1702,3 +1703,14 @@ export interface YieldExpression extends BaseNode {
delegate: boolean;
argument?: Expression;
}

export interface TSUnknownJSDocType extends BaseNode {
type: AST_NODE_TYPES.TSUnknownJSDocType;
kind:
| 'JSDocAllType'
| 'JSDocUnknownType'
| 'JSDocFunctionType'
| 'JSDocNullableType'
| 'JSDocNonNullableType';
value: string;
}
3 changes: 3 additions & 0 deletions packages/typescript-estree/src/visitor-keys.ts
Expand Up @@ -123,4 +123,7 @@ export const visitorKeys = eslintVisitorKeys.unionWith({
TSUndefinedKeyword: [],
TSUnknownKeyword: [],
TSVoidKeyword: [],

// Invalid nodes
TSUnknownJSDocType: [],
});
Expand Up @@ -489,6 +489,10 @@ tester.addFixturePatternConfig('typescript/errorRecovery', {
* TODO: enable error code TS1024: 'readonly' modifier can only appear on a property declaration or index signature.
*/
'interface-method-readonly',
/**
* Babel correctly errors on this
*/
'jsdoc-disallowed-in-typescript',
],
});

Expand Down
Expand Up @@ -2471,6 +2471,8 @@ Object {

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

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

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/errorRecovery/object-assertion-not-allowed.src 1`] = `
Object {
"column": 3,
Expand Down