Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 90ccc00

Browse files
committed
Breaking: switch 'jsx' option by filename (fixes #517)
1 parent a9d932a commit 90ccc00

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

parser.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const visitorKeys = require("./visitor-keys");
2020
exports.version = require("./package.json").version;
2121

2222
exports.parseForESLint = function parseForESLint(code, options) {
23+
if (options && typeof options.filePath === "string" && options.filePath.endsWith(".tsx")) {
24+
options.jsx = true;
25+
}
26+
2327
const ast = parse(code, options);
2428
traverser.traverse(ast, {
2529
enter: node => {

tests/lib/tsx.js

+75-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
// Requirements
1212
//------------------------------------------------------------------------------
1313

14-
const path = require("path"),
14+
const
15+
assert = require("assert"),
16+
path = require("path"),
17+
{ Linter } = require("eslint"),
1518
shelljs = require("shelljs"),
19+
parser = require("../../"),
1620
testUtils = require("../../tools/test-utils");
1721

1822
//------------------------------------------------------------------------------
@@ -39,4 +43,74 @@ describe("TSX", () => {
3943
};
4044
test(`fixtures/${filename}.src`, testUtils.createSnapshotTestBlock(code, config));
4145
});
46+
47+
describe("if the filename ends with '.tsx', enable jsx option automatically.", () => {
48+
const linter = new Linter();
49+
linter.defineParser("typescript-eslint-parser", parser);
50+
51+
test("anonymous", () => {
52+
const code = "const element = <T/>";
53+
const config = {
54+
parser: "typescript-eslint-parser"
55+
};
56+
const messages = linter.verify(code, config);
57+
58+
assert.deepStrictEqual(
59+
messages,
60+
[{
61+
column: 18,
62+
fatal: true,
63+
line: 1,
64+
message: "Parsing error: '>' expected.",
65+
ruleId: null,
66+
severity: 2,
67+
source: "const element = <T/>"
68+
}]
69+
);
70+
});
71+
72+
test("test.ts", () => {
73+
const code = "const element = <T/>";
74+
const config = {
75+
parser: "typescript-eslint-parser"
76+
};
77+
const messages = linter.verify(code, config, { filename: "test.ts" });
78+
79+
assert.deepStrictEqual(
80+
messages,
81+
[{
82+
column: 18,
83+
fatal: true,
84+
line: 1,
85+
message: "Parsing error: '>' expected.",
86+
ruleId: null,
87+
severity: 2,
88+
source: "const element = <T/>"
89+
}]
90+
);
91+
});
92+
93+
test("test.ts with 'jsx' option", () => {
94+
const code = "const element = <T/>";
95+
const config = {
96+
parser: "typescript-eslint-parser",
97+
parserOptions: {
98+
jsx: true
99+
}
100+
};
101+
const messages = linter.verify(code, config, { filename: "test.ts" });
102+
103+
assert.deepStrictEqual(messages, []);
104+
});
105+
106+
test("test.tsx", () => {
107+
const code = "const element = <T/>";
108+
const config = {
109+
parser: "typescript-eslint-parser"
110+
};
111+
const messages = linter.verify(code, config, { filename: "test.tsx" });
112+
113+
assert.deepStrictEqual(messages, []);
114+
});
115+
});
42116
});

0 commit comments

Comments
 (0)