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

Commit 2d22321

Browse files
mysticateakaicataldo
authored andcommitted
Breaking: switch 'jsx' option by filename (fixes #517) (#543)
* Breaking: switch 'jsx' option by filename (fixes #517) * update README.md * don't modify the option object * update behavior * use expect() instead of assert()
1 parent 3b1ed17 commit 2d22321

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ By far the most common case will be installing the [eslint-plugin-typescript](ht
3636

3737
The following additional configuration options are available by specifying them in [`parserOptions`](https://eslint.org/docs/user-guide/configuring#specifying-parser-options) in your ESLint configuration file.
3838

39-
**`jsx`** - default `false`. Enable parsing JSX when `true`. More details can be found [here](https://www.typescriptlang.org/docs/handbook/jsx.html).
39+
- **`jsx`** - default `false`. Enable parsing JSX when `true`. More details can be found [here](https://www.typescriptlang.org/docs/handbook/jsx.html).
40+
- It's `false` on `*.ts` files regardless of this option.
41+
- It's `true` on `*.tsx` files regardless of this option.
42+
- Otherwise, it respects this option.
4043

41-
**`useJSXTextNode`** - default `false`. The JSX AST changed the node type for string literals inside a JSX Element from `Literal` to `JSXText`. When value is `true`, these nodes will be parsed as type `JSXText`. When value is `false`, these nodes will be parsed as type `Literal`.
44+
- **`useJSXTextNode`** - default `false`. The JSX AST changed the node type for string literals inside a JSX Element from `Literal` to `JSXText`. When value is `true`, these nodes will be parsed as type `JSXText`. When value is `false`, these nodes will be parsed as type `Literal`.
4245

4346
### .eslintrc.json
4447

parser.js

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ 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") {
24+
const tsx = options.filePath.endsWith(".tsx");
25+
if (tsx || options.filePath.endsWith(".ts")) {
26+
options = Object.assign({}, options, { jsx: tsx });
27+
}
28+
}
29+
2330
const ast = parse(code, options);
2431
traverser.traverse(ast, {
2532
enter: node => {

tests/lib/tsx.js

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

14-
const path = require("path"),
14+
const
15+
path = require("path"),
16+
{ Linter } = require("eslint"),
1517
shelljs = require("shelljs"),
18+
parser = require("../../"),
1619
testUtils = require("../../tools/test-utils");
1720

1821
//------------------------------------------------------------------------------
@@ -39,4 +42,102 @@ describe("TSX", () => {
3942
};
4043
test(`fixtures/${filename}.src`, testUtils.createSnapshotTestBlock(code, config));
4144
});
45+
46+
describe("if the filename ends with '.tsx', enable jsx option automatically.", () => {
47+
const linter = new Linter();
48+
linter.defineParser("typescript-eslint-parser", parser);
49+
50+
test("filePath was not provided", () => {
51+
const code = "const element = <T/>";
52+
const config = {
53+
parser: "typescript-eslint-parser"
54+
};
55+
const messages = linter.verify(code, config);
56+
57+
expect(messages).toStrictEqual([{
58+
column: 18,
59+
fatal: true,
60+
line: 1,
61+
message: "Parsing error: '>' expected.",
62+
ruleId: null,
63+
severity: 2,
64+
source: "const element = <T/>"
65+
}]);
66+
});
67+
68+
test("filePath was not provided and 'jsx:true' option", () => {
69+
const code = "const element = <T/>";
70+
const config = {
71+
parser: "typescript-eslint-parser",
72+
parserOptions: {
73+
jsx: true
74+
}
75+
};
76+
const messages = linter.verify(code, config);
77+
78+
expect(messages).toStrictEqual([]);
79+
});
80+
81+
test("test.ts", () => {
82+
const code = "const element = <T/>";
83+
const config = {
84+
parser: "typescript-eslint-parser"
85+
};
86+
const messages = linter.verify(code, config, { filename: "test.ts" });
87+
88+
expect(messages).toStrictEqual([{
89+
column: 18,
90+
fatal: true,
91+
line: 1,
92+
message: "Parsing error: '>' expected.",
93+
ruleId: null,
94+
severity: 2,
95+
source: "const element = <T/>"
96+
}]);
97+
});
98+
99+
test("test.ts with 'jsx:true' option", () => {
100+
const code = "const element = <T/>";
101+
const config = {
102+
parser: "typescript-eslint-parser",
103+
parserOptions: {
104+
jsx: true
105+
}
106+
};
107+
const messages = linter.verify(code, config, { filename: "test.ts" });
108+
109+
expect(messages).toStrictEqual([{
110+
column: 18,
111+
fatal: true,
112+
line: 1,
113+
message: "Parsing error: '>' expected.",
114+
ruleId: null,
115+
severity: 2,
116+
source: "const element = <T/>"
117+
}]);
118+
});
119+
120+
test("test.tsx", () => {
121+
const code = "const element = <T/>";
122+
const config = {
123+
parser: "typescript-eslint-parser"
124+
};
125+
const messages = linter.verify(code, config, { filename: "test.tsx" });
126+
127+
expect(messages).toStrictEqual([]);
128+
});
129+
130+
test("test.tsx with 'jsx:false' option", () => {
131+
const code = "const element = <T/>";
132+
const config = {
133+
parser: "typescript-eslint-parser",
134+
parserOptions: {
135+
jsx: false
136+
}
137+
};
138+
const messages = linter.verify(code, config, { filename: "test.tsx" });
139+
140+
expect(messages).toStrictEqual([]);
141+
});
142+
});
42143
});

0 commit comments

Comments
 (0)