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

Commit da5a810

Browse files
committed
update behavior
1 parent 98cbcf0 commit da5a810

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
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`. It's `true` on `*.tsx` files automatically. 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

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ 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 = Object.assign({}, options, { jsx: true });
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+
}
2528
}
2629

2730
const ast = parse(code, options);

tests/lib/tsx.js

+40-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe("TSX", () => {
4848
const linter = new Linter();
4949
linter.defineParser("typescript-eslint-parser", parser);
5050

51-
test("anonymous", () => {
51+
test("filePath was not provided", () => {
5252
const code = "const element = <T/>";
5353
const config = {
5454
parser: "typescript-eslint-parser"
@@ -69,6 +69,19 @@ describe("TSX", () => {
6969
);
7070
});
7171

72+
test("filePath was not provided and 'jsx:true' option", () => {
73+
const code = "const element = <T/>";
74+
const config = {
75+
parser: "typescript-eslint-parser",
76+
parserOptions: {
77+
jsx: true
78+
}
79+
};
80+
const messages = linter.verify(code, config);
81+
82+
assert.deepStrictEqual(messages, []);
83+
});
84+
7285
test("test.ts", () => {
7386
const code = "const element = <T/>";
7487
const config = {
@@ -90,7 +103,7 @@ describe("TSX", () => {
90103
);
91104
});
92105

93-
test("test.ts with 'jsx' option", () => {
106+
test("test.ts with 'jsx:true' option", () => {
94107
const code = "const element = <T/>";
95108
const config = {
96109
parser: "typescript-eslint-parser",
@@ -100,7 +113,18 @@ describe("TSX", () => {
100113
};
101114
const messages = linter.verify(code, config, { filename: "test.ts" });
102115

103-
assert.deepStrictEqual(messages, []);
116+
assert.deepStrictEqual(
117+
messages,
118+
[{
119+
column: 18,
120+
fatal: true,
121+
line: 1,
122+
message: "Parsing error: '>' expected.",
123+
ruleId: null,
124+
severity: 2,
125+
source: "const element = <T/>"
126+
}]
127+
);
104128
});
105129

106130
test("test.tsx", () => {
@@ -112,5 +136,18 @@ describe("TSX", () => {
112136

113137
assert.deepStrictEqual(messages, []);
114138
});
139+
140+
test("test.tsx with 'jsx:false' option", () => {
141+
const code = "const element = <T/>";
142+
const config = {
143+
parser: "typescript-eslint-parser",
144+
parserOptions: {
145+
jsx: false
146+
}
147+
};
148+
const messages = linter.verify(code, config, { filename: "test.tsx" });
149+
150+
assert.deepStrictEqual(messages, []);
151+
});
115152
});
116153
});

0 commit comments

Comments
 (0)