diff --git a/README.md b/README.md
index 68c8dee..fd8d6bb 100644
--- a/README.md
+++ b/README.md
@@ -36,9 +36,12 @@ By far the most common case will be installing the [eslint-plugin-typescript](ht
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.
-**`jsx`** - default `false`. Enable parsing JSX when `true`. More details can be found [here](https://www.typescriptlang.org/docs/handbook/jsx.html).
+- **`jsx`** - default `false`. Enable parsing JSX when `true`. More details can be found [here](https://www.typescriptlang.org/docs/handbook/jsx.html).
+ - It's `false` on `*.ts` files regardless of this option.
+ - It's `true` on `*.tsx` files regardless of this option.
+ - Otherwise, it respects this option.
-**`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`.
+- **`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`.
### .eslintrc.json
diff --git a/parser.js b/parser.js
index 32b4e7c..10d3063 100644
--- a/parser.js
+++ b/parser.js
@@ -20,6 +20,13 @@ const visitorKeys = require("./visitor-keys");
exports.version = require("./package.json").version;
exports.parseForESLint = function parseForESLint(code, options) {
+ if (options && typeof options.filePath === "string") {
+ const tsx = options.filePath.endsWith(".tsx");
+ if (tsx || options.filePath.endsWith(".ts")) {
+ options = Object.assign({}, options, { jsx: tsx });
+ }
+ }
+
const ast = parse(code, options);
traverser.traverse(ast, {
enter: node => {
diff --git a/tests/lib/tsx.js b/tests/lib/tsx.js
index 4fdf201..831d5fd 100644
--- a/tests/lib/tsx.js
+++ b/tests/lib/tsx.js
@@ -11,8 +11,11 @@
// Requirements
//------------------------------------------------------------------------------
-const path = require("path"),
+const
+ path = require("path"),
+ { Linter } = require("eslint"),
shelljs = require("shelljs"),
+ parser = require("../../"),
testUtils = require("../../tools/test-utils");
//------------------------------------------------------------------------------
@@ -39,4 +42,102 @@ describe("TSX", () => {
};
test(`fixtures/${filename}.src`, testUtils.createSnapshotTestBlock(code, config));
});
+
+ describe("if the filename ends with '.tsx', enable jsx option automatically.", () => {
+ const linter = new Linter();
+ linter.defineParser("typescript-eslint-parser", parser);
+
+ test("filePath was not provided", () => {
+ const code = "const element = ";
+ const config = {
+ parser: "typescript-eslint-parser"
+ };
+ const messages = linter.verify(code, config);
+
+ expect(messages).toStrictEqual([{
+ column: 18,
+ fatal: true,
+ line: 1,
+ message: "Parsing error: '>' expected.",
+ ruleId: null,
+ severity: 2,
+ source: "const element = "
+ }]);
+ });
+
+ test("filePath was not provided and 'jsx:true' option", () => {
+ const code = "const element = ";
+ const config = {
+ parser: "typescript-eslint-parser",
+ parserOptions: {
+ jsx: true
+ }
+ };
+ const messages = linter.verify(code, config);
+
+ expect(messages).toStrictEqual([]);
+ });
+
+ test("test.ts", () => {
+ const code = "const element = ";
+ const config = {
+ parser: "typescript-eslint-parser"
+ };
+ const messages = linter.verify(code, config, { filename: "test.ts" });
+
+ expect(messages).toStrictEqual([{
+ column: 18,
+ fatal: true,
+ line: 1,
+ message: "Parsing error: '>' expected.",
+ ruleId: null,
+ severity: 2,
+ source: "const element = "
+ }]);
+ });
+
+ test("test.ts with 'jsx:true' option", () => {
+ const code = "const element = ";
+ const config = {
+ parser: "typescript-eslint-parser",
+ parserOptions: {
+ jsx: true
+ }
+ };
+ const messages = linter.verify(code, config, { filename: "test.ts" });
+
+ expect(messages).toStrictEqual([{
+ column: 18,
+ fatal: true,
+ line: 1,
+ message: "Parsing error: '>' expected.",
+ ruleId: null,
+ severity: 2,
+ source: "const element = "
+ }]);
+ });
+
+ test("test.tsx", () => {
+ const code = "const element = ";
+ const config = {
+ parser: "typescript-eslint-parser"
+ };
+ const messages = linter.verify(code, config, { filename: "test.tsx" });
+
+ expect(messages).toStrictEqual([]);
+ });
+
+ test("test.tsx with 'jsx:false' option", () => {
+ const code = "const element = ";
+ const config = {
+ parser: "typescript-eslint-parser",
+ parserOptions: {
+ jsx: false
+ }
+ };
+ const messages = linter.verify(code, config, { filename: "test.tsx" });
+
+ expect(messages).toStrictEqual([]);
+ });
+ });
});