diff --git a/README.md b/README.md index f2c4442..cf355d7 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ 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). +- **`ecmaFeatures.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. @@ -49,7 +49,9 @@ The following additional configuration options are available by specifying them { "parser": "typescript-eslint-parser", "parserOptions": { - "jsx": true, + "ecmaFeatures": { + "jsx": true + }, "useJSXTextNode": true } } diff --git a/parser.js b/parser.js index 59f5836..7ce6434 100644 --- a/parser.js +++ b/parser.js @@ -20,16 +20,27 @@ const visitorKeys = require("./visitor-keys"); exports.version = require("./package.json").version; -exports.parseForESLint = function parseForESLint(code, options) { - if (typeof options !== "object" || options === null) { - options = { useJSXTextNode: true }; - } else if (typeof options.useJSXTextNode !== "boolean") { - options = Object.assign({}, options, { useJSXTextNode: true }); +exports.parseForESLint = function parseForESLint(code, inputOptions) { + const options = Object.assign({ + useJSXTextNode: true, + // typescript-estree doesn't respect ecmaFeatures object + jsx: false + }, inputOptions); + + if (typeof options.useJSXTextNode !== "boolean") { + options.useJSXTextNode = true; } - if (typeof options.filePath === "string") { - const tsx = options.filePath.endsWith(".tsx"); - if (tsx || options.filePath.endsWith(".ts")) { - options = Object.assign({}, options, { jsx: tsx }); + + options.jsx = (options.ecmaFeatures || {}).jsx; + if (typeof options.jsx !== "boolean") { + options.jsx = false; + } + + // override the jsx option depending on the file path + if (typeof inputOptions.filePath === "string") { + const tsx = inputOptions.filePath.endsWith(".tsx"); + if (tsx || inputOptions.filePath.endsWith(".ts")) { + options.jsx = tsx; } } diff --git a/tests/lib/comments.js b/tests/lib/comments.js index 008924e..314ec44 100644 --- a/tests/lib/comments.js +++ b/tests/lib/comments.js @@ -35,7 +35,9 @@ describe("Comments", () => { testFiles.forEach(filename => { const code = shelljs.cat(`${path.resolve(FIXTURES_DIR, filename)}.src.js`); const config = { - jsx: true + ecmaFeatures: { + jsx: true + } }; test(`fixtures/${filename}.src`, testUtils.createSnapshotTestBlock(code, config)); }); diff --git a/tests/lib/jsx.js b/tests/lib/jsx.js index f1ee7fe..8458c57 100644 --- a/tests/lib/jsx.js +++ b/tests/lib/jsx.js @@ -54,7 +54,9 @@ describe("JSX", () => { const code = shelljs.cat(`${path.resolve(fixturesDir, filename)}.src.js`); const config = { useJSXTextNode, - jsx: true + ecmaFeatures: { + jsx: true + } }; test(`fixtures/${filename}.src`, testUtils.createSnapshotTestBlock(code, config)); }; diff --git a/tests/lib/tsx.js b/tests/lib/tsx.js index 831d5fd..471a398 100644 --- a/tests/lib/tsx.js +++ b/tests/lib/tsx.js @@ -38,7 +38,9 @@ describe("TSX", () => { const code = shelljs.cat(`${path.resolve(TSX_FIXTURES_DIR, filename)}.src.tsx`); const config = { useJSXTextNode: true, - jsx: true + ecmaFeatures: { + jsx: true + } }; test(`fixtures/${filename}.src`, testUtils.createSnapshotTestBlock(code, config)); }); @@ -70,7 +72,9 @@ describe("TSX", () => { const config = { parser: "typescript-eslint-parser", parserOptions: { - jsx: true + ecmaFeatures: { + jsx: true + } } }; const messages = linter.verify(code, config); @@ -101,7 +105,9 @@ describe("TSX", () => { const config = { parser: "typescript-eslint-parser", parserOptions: { - jsx: true + ecmaFeatures: { + jsx: true + } } }; const messages = linter.verify(code, config, { filename: "test.ts" }); @@ -132,7 +138,9 @@ describe("TSX", () => { const config = { parser: "typescript-eslint-parser", parserOptions: { - jsx: false + ecmaFeatures: { + jsx: false + } } }; const messages = linter.verify(code, config, { filename: "test.tsx" });