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 000000000000..71eb04b5e41a --- /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 50415390fb25..28442094c2cd 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 f8999685aa71..92ef8c42e604 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/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 000000000000..58e66b804b7e --- /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", +} +`;