Skip to content

Commit

Permalink
fix(typescript-estree): add support for ThisKeyword as JSXNamespaceOr…
Browse files Browse the repository at this point in the history
…Identifier
  • Loading branch information
armano2 committed Feb 25, 2021
1 parent 279bc8e commit 6892874
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 13 deletions.
@@ -0,0 +1 @@
<this:bar />;
21 changes: 9 additions & 12 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -517,32 +517,35 @@ 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<TSESTree.JSXIdentifier>(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);
const result = this.createNode<TSESTree.JSXNamespacedName>(node, {
type: AST_NODE_TYPES.JSXNamespacedName,
namespace: this.createNode<TSESTree.JSXIdentifier>(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<TSESTree.JSXIdentifier>(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,
Expand Down Expand Up @@ -581,12 +584,6 @@ export class Converter {
break;

case SyntaxKind.ThisKeyword:
result = this.createNode<TSESTree.JSXIdentifier>(node, {
type: AST_NODE_TYPES.JSXIdentifier,
name: 'this',
});
break;

case SyntaxKind.Identifier:
default:
return this.convertJSXNamespaceOrIdentifier(node);
Expand Down
Expand Up @@ -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]:
Expand Down
@@ -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",
}
`;

0 comments on commit 6892874

Please sign in to comment.