From e27abacf808dea90676149cb3e8699a7f14172ad Mon Sep 17 00:00:00 2001 From: Armano Date: Thu, 25 Feb 2021 20:26:05 +0100 Subject: [PATCH] fix(typescript-estree): add support for ThisKeyword as JSXNamespaceOrIdentifier --- .../fixtures/jsx/namespace-this-name.src.js | 1 + packages/typescript-estree/src/convert.ts | 21 +- .../src/ts-estree/estree-to-ts-node-types.ts | 2 +- .../semantic-diagnostics-enabled.test.ts.snap | 2 + .../jsx/namespace-this-name.src.js.shot | 226 ++++++++++++++++++ 5 files changed, 239 insertions(+), 13 deletions(-) create mode 100644 packages/shared-fixtures/fixtures/jsx/namespace-this-name.src.js create mode 100644 packages/typescript-estree/tests/snapshots/jsx/namespace-this-name.src.js.shot diff --git a/packages/shared-fixtures/fixtures/jsx/namespace-this-name.src.js b/packages/shared-fixtures/fixtures/jsx/namespace-this-name.src.js new file mode 100644 index 00000000000..71eb04b5e41 --- /dev/null +++ b/packages/shared-fixtures/fixtures/jsx/namespace-this-name.src.js @@ -0,0 +1 @@ +; diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 50415390fb2..28442094c2c 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -517,19 +517,22 @@ export class Converter { return result; } - private convertJSXIdentifier(node: ts.Identifier): TSESTree.JSXIdentifier { + private convertJSXIdentifier( + node: ts.Identifier | ts.ThisExpression, + ): TSESTree.JSXIdentifier { const result = this.createNode(node, { type: AST_NODE_TYPES.JSXIdentifier, - name: node.text, + name: node.getText(), }); this.registerTSNodeInNodeMap(node, result); return result; } private convertJSXNamespaceOrIdentifier( - node: ts.Identifier, + node: ts.Identifier | ts.ThisExpression, ): TSESTree.JSXIdentifier | TSESTree.JSXNamespacedName { - const colonIndex = node.text.indexOf(':'); + const text = node.getText(); + const colonIndex = text.indexOf(':'); // this is intentional we can ignore conversion if `:` is in first character if (colonIndex > 0) { const range = getRange(node, this.ast); @@ -537,12 +540,12 @@ export class Converter { type: AST_NODE_TYPES.JSXNamespacedName, namespace: this.createNode(node, { type: AST_NODE_TYPES.JSXIdentifier, - name: node.text.slice(0, colonIndex), + name: text.slice(0, colonIndex), range: [range[0], range[0] + colonIndex], }), name: this.createNode(node, { type: AST_NODE_TYPES.JSXIdentifier, - name: node.text.slice(colonIndex + 1), + name: text.slice(colonIndex + 1), range: [range[0] + colonIndex + 1, range[1]], }), range, @@ -581,12 +584,6 @@ export class Converter { break; case SyntaxKind.ThisKeyword: - result = this.createNode(node, { - type: AST_NODE_TYPES.JSXIdentifier, - name: 'this', - }); - break; - case SyntaxKind.Identifier: default: return this.convertJSXNamespaceOrIdentifier(node); diff --git a/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts b/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts index f8999685aa7..92ef8c42e60 100644 --- a/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts +++ b/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts @@ -93,7 +93,7 @@ export interface EstreeToTsNodeTypes { [AST_NODE_TYPES.JSXSpreadAttribute]: ts.JsxSpreadAttribute; [AST_NODE_TYPES.JSXSpreadChild]: ts.JsxExpression; [AST_NODE_TYPES.JSXMemberExpression]: ts.PropertyAccessExpression; - [AST_NODE_TYPES.JSXNamespacedName]: ts.Identifier; + [AST_NODE_TYPES.JSXNamespacedName]: ts.Identifier | ts.ThisExpression; [AST_NODE_TYPES.JSXText]: ts.JsxText; [AST_NODE_TYPES.LabeledStatement]: ts.LabeledStatement; [AST_NODE_TYPES.Literal]: diff --git a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.test.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.test.ts.snap index ba6f70c40b2..02ad9fa47f1 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.test.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.test.ts.snap @@ -1580,6 +1580,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/multiple-blank-spaces.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; +exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/namespace-this-name.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; + exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/namespaced-attribute-and-value-inserted.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/namespaced-name-and-attribute.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; diff --git a/packages/typescript-estree/tests/snapshots/jsx/namespace-this-name.src.js.shot b/packages/typescript-estree/tests/snapshots/jsx/namespace-this-name.src.js.shot new file mode 100644 index 00000000000..58e66b804b7 --- /dev/null +++ b/packages/typescript-estree/tests/snapshots/jsx/namespace-this-name.src.js.shot @@ -0,0 +1,226 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`jsx namespace-this-name.src 1`] = ` +Object { + "body": Array [ + Object { + "expression": Object { + "children": Array [], + "closingElement": null, + "loc": Object { + "end": Object { + "column": 12, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "openingElement": Object { + "attributes": Array [], + "loc": Object { + "end": Object { + "column": 12, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "name": Object { + "loc": Object { + "end": Object { + "column": 9, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "name": Object { + "loc": Object { + "end": Object { + "column": 9, + "line": 1, + }, + "start": Object { + "column": 6, + "line": 1, + }, + }, + "name": "bar", + "range": Array [ + 6, + 9, + ], + "type": "JSXIdentifier", + }, + "namespace": Object { + "loc": Object { + "end": Object { + "column": 5, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "name": "this", + "range": Array [ + 1, + 5, + ], + "type": "JSXIdentifier", + }, + "range": Array [ + 1, + 9, + ], + "type": "JSXNamespacedName", + }, + "range": Array [ + 0, + 12, + ], + "selfClosing": true, + "type": "JSXOpeningElement", + "typeParameters": undefined, + }, + "range": Array [ + 0, + 12, + ], + "type": "JSXElement", + }, + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 13, + ], + "type": "ExpressionStatement", + }, + ], + "comments": Array [], + "loc": Object { + "end": Object { + "column": 0, + "line": 2, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 14, + ], + "sourceType": "script", + "tokens": Array [ + Object { + "loc": Object { + "end": Object { + "column": 1, + "line": 1, + }, + "start": Object { + "column": 0, + "line": 1, + }, + }, + "range": Array [ + 0, + 1, + ], + "type": "Punctuator", + "value": "<", + }, + Object { + "loc": Object { + "end": Object { + "column": 9, + "line": 1, + }, + "start": Object { + "column": 1, + "line": 1, + }, + }, + "range": Array [ + 1, + 9, + ], + "type": "Keyword", + "value": "this:bar", + }, + Object { + "loc": Object { + "end": Object { + "column": 11, + "line": 1, + }, + "start": Object { + "column": 10, + "line": 1, + }, + }, + "range": Array [ + 10, + 11, + ], + "type": "Punctuator", + "value": "/", + }, + Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 1, + }, + "start": Object { + "column": 11, + "line": 1, + }, + }, + "range": Array [ + 11, + 12, + ], + "type": "Punctuator", + "value": ">", + }, + Object { + "loc": Object { + "end": Object { + "column": 13, + "line": 1, + }, + "start": Object { + "column": 12, + "line": 1, + }, + }, + "range": Array [ + 12, + 13, + ], + "type": "Punctuator", + "value": ";", + }, + ], + "type": "Program", +} +`;