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

Fix: support ecmaFeatures.jsx flag #595

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
}
Expand Down
29 changes: 20 additions & 9 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved
const tsx = inputOptions.filePath.endsWith(".tsx");
if (tsx || inputOptions.filePath.endsWith(".ts")) {
options.jsx = tsx;
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/lib/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand Down
4 changes: 3 additions & 1 deletion tests/lib/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
Expand Down
16 changes: 12 additions & 4 deletions tests/lib/tsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand Down Expand Up @@ -70,7 +72,9 @@ describe("TSX", () => {
const config = {
parser: "typescript-eslint-parser",
parserOptions: {
jsx: true
ecmaFeatures: {
jsx: true
}
}
};
const messages = linter.verify(code, config);
Expand Down Expand Up @@ -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" });
Expand Down Expand Up @@ -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" });
Expand Down