Skip to content
This repository was archived by the owner on Apr 30, 2021. It is now read-only.

Commit f4d9659

Browse files
committed
Adding support for React (JSX, TSX). Closes #37
1 parent bd66353 commit f4d9659

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async function run(ctx: vscode.ExtensionContext, editor: vscode.TextEditor | und
5757
return;
5858
}
5959

60-
const supportedLanguages = ["javascript", "typescript"];
60+
const supportedLanguages = ["javascript", "typescript", "javascriptreact", "typescriptreact"];
6161

6262
if (supportedLanguages.indexOf(editor.document.languageId) === -1) {
6363
return;

src/parser.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,60 @@ export function getFunctionCalls(sourceCode: string, editor: vscode.TextEditor):
1111
options.parser = require("recast/parsers/esprima");
1212
} else if (editor.document.languageId === "typescript") {
1313
options.parser = require("recast/parsers/typescript");
14+
} else if (editor.document.languageId === "javascriptreact") {
15+
options.parser = require("recast/parsers/babel");
16+
} else if (editor.document.languageId === "typescriptreact") {
17+
options.parser = {
18+
parse(source) {
19+
const babelParser = require("recast/parsers/babel").parser;
20+
const opts = {
21+
allowImportExportEverywhere: true,
22+
allowReturnOutsideFunction: true,
23+
plugins: [
24+
"asyncGenerators",
25+
"bigInt",
26+
"classPrivateMethods",
27+
"classPrivateProperties",
28+
"classProperties",
29+
"decorators-legacy",
30+
"doExpressions",
31+
"dynamicImport",
32+
"exportDefaultFrom",
33+
"exportExtensions",
34+
"exportNamespaceFrom",
35+
"functionBind",
36+
"functionSent",
37+
"importMeta",
38+
"nullishCoalescingOperator",
39+
"numericSeparator",
40+
"objectRestSpread",
41+
"optionalCatchBinding",
42+
"optionalChaining",
43+
["pipelineOperator", { proposal: "minimal" }],
44+
"throwExpressions",
45+
"typescript",
46+
"jsx"
47+
],
48+
sourceType: "unambiguous",
49+
startLine: 1,
50+
strictMode: false,
51+
tokens: true
52+
};
53+
54+
return babelParser.parse(source, opts);
55+
}
56+
};
1457
}
1558

1659
sourceCode = removeShebang(sourceCode);
1760

18-
const ast = recast.parse(sourceCode, options);
61+
let ast;
62+
63+
try {
64+
ast = recast.parse(sourceCode, options);
65+
} catch (err) {
66+
console.log(err.message);
67+
}
1968

2069
fcArray = lookForFunctionCalls(editor, fcArray, ast.program.body);
2170

src/test/e2e/extension.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ suite("js annotations", () => {
7777
assert.strictEqual(decArray[0].renderOptions.before.contentText, " str: ");
7878
assert.deepEqual(errDecArray.length, 0);
7979
});
80+
81+
test("Should work for JSX files", async () => {
82+
const [decArray, errDecArray] = await getDecorationsFromExample("react.jsx");
83+
assert.deepEqual(decArray.length, 1);
84+
assert.deepEqual(errDecArray.length, 0);
85+
});
8086
});
8187

8288
function sleep(ms: number): Promise<void> {

src/test/examples/react.jsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function jsxComponent(text) {
2+
return (
3+
<div>{text}</div>
4+
)
5+
}
6+
7+
jsxComponent("Hello World")

0 commit comments

Comments
 (0)