From 628c47d37aec8b6c849c57ee73b26775c463c33b Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Wed, 9 Jan 2019 09:37:32 -0800 Subject: [PATCH 1/6] Fix: support ecmaFeatures.jsx flag Fixes #594 --- parser.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/parser.js b/parser.js index 59f5836..5823878 100644 --- a/parser.js +++ b/parser.js @@ -32,6 +32,10 @@ exports.parseForESLint = function parseForESLint(code, options) { options = Object.assign({}, options, { jsx: tsx }); } } + if (options.ecmaFeatures && options.ecmaFeatures.jsx) { + // allow the user to override the jsx setting + options.jsx = options.ecmaFeatures.jsx + } const ast = parse(code, options); const extraOptions = { From 9b3c0cc72ff809c9cb9cba81310a42a07cd84f24 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Wed, 9 Jan 2019 09:40:47 -0800 Subject: [PATCH 2/6] Allow user to turn off jsx --- parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser.js b/parser.js index 5823878..5a4f7fc 100644 --- a/parser.js +++ b/parser.js @@ -32,7 +32,7 @@ exports.parseForESLint = function parseForESLint(code, options) { options = Object.assign({}, options, { jsx: tsx }); } } - if (options.ecmaFeatures && options.ecmaFeatures.jsx) { + if (options.ecmaFeatures && typeof options.ecmaFeatures.jsx === "boolean") { // allow the user to override the jsx setting options.jsx = options.ecmaFeatures.jsx } From 8137085429d04c551807c55388f433a69e27395e Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Wed, 9 Jan 2019 09:46:47 -0800 Subject: [PATCH 3/6] added missing semi i should pay attention when using the github UI to edit files --- parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser.js b/parser.js index 5a4f7fc..46d2802 100644 --- a/parser.js +++ b/parser.js @@ -34,7 +34,7 @@ exports.parseForESLint = function parseForESLint(code, options) { } if (options.ecmaFeatures && typeof options.ecmaFeatures.jsx === "boolean") { // allow the user to override the jsx setting - options.jsx = options.ecmaFeatures.jsx + options.jsx = options.ecmaFeatures.jsx; } const ast = parse(code, options); From ee895f7ca5a1336822ce7a69a6dea473330cd158 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Thu, 10 Jan 2019 08:52:54 -0800 Subject: [PATCH 4/6] don't overwrite existing jsx option --- parser.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/parser.js b/parser.js index 46d2802..854090c 100644 --- a/parser.js +++ b/parser.js @@ -32,7 +32,9 @@ exports.parseForESLint = function parseForESLint(code, options) { options = Object.assign({}, options, { jsx: tsx }); } } - if (options.ecmaFeatures && typeof options.ecmaFeatures.jsx === "boolean") { + if (typeof options.jsx !== "boolean" && + options.ecmaFeatures && + typeof options.ecmaFeatures.jsx === "boolean") { // allow the user to override the jsx setting options.jsx = options.ecmaFeatures.jsx; } From 742cf6a999abc3949453ba24ceb6189e0962d56f Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sat, 12 Jan 2019 17:47:11 -0800 Subject: [PATCH 5/6] drop support for inputting options.jsx --- README.md | 6 ++++-- parser.js | 35 ++++++++++++++++++++--------------- tests/lib/comments.js | 4 +++- tests/lib/jsx.js | 4 +++- tests/lib/tsx.js | 16 ++++++++++++---- 5 files changed, 42 insertions(+), 23 deletions(-) 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 854090c..45173e0 100644 --- a/parser.js +++ b/parser.js @@ -20,23 +20,28 @@ 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") { + inputOptions.jsx = false; } - if (typeof options.jsx !== "boolean" && - options.ecmaFeatures && - typeof options.ecmaFeatures.jsx === "boolean") { - // allow the user to override the jsx setting - options.jsx = options.ecmaFeatures.jsx; + + // 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; + } } const ast = parse(code, options); 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" }); From 1d46dea7438e1a869f344fdc9991f34bf6fa1461 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sat, 12 Jan 2019 18:01:21 -0800 Subject: [PATCH 6/6] fix typo --- parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser.js b/parser.js index 45173e0..7ce6434 100644 --- a/parser.js +++ b/parser.js @@ -33,7 +33,7 @@ exports.parseForESLint = function parseForESLint(code, inputOptions) { options.jsx = (options.ecmaFeatures || {}).jsx; if (typeof options.jsx !== "boolean") { - inputOptions.jsx = false; + options.jsx = false; } // override the jsx option depending on the file path