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

Breaking: switch 'jsx' option by filename (fixes #517) #543

Merged
merged 5 commits into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion 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).
**`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).

**`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`.

Expand Down
4 changes: 4 additions & 0 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const visitorKeys = require("./visitor-keys");
exports.version = require("./package.json").version;

exports.parseForESLint = function parseForESLint(code, options) {
if (options && typeof options.filePath === "string" && options.filePath.endsWith(".tsx")) {
options = Object.assign({}, options, { jsx: true });
}

const ast = parse(code, options);
traverser.traverse(ast, {
enter: node => {
Expand Down
76 changes: 75 additions & 1 deletion tests/lib/tsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
// Requirements
//------------------------------------------------------------------------------

const path = require("path"),
const
assert = require("assert"),
path = require("path"),
{ Linter } = require("eslint"),
shelljs = require("shelljs"),
parser = require("../../"),
testUtils = require("../../tools/test-utils");

//------------------------------------------------------------------------------
Expand All @@ -39,4 +43,74 @@ 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("anonymous", () => {
const code = "const element = <T/>";
const config = {
parser: "typescript-eslint-parser"
};
const messages = linter.verify(code, config);

assert.deepStrictEqual(
messages,
[{
column: 18,
fatal: true,
line: 1,
message: "Parsing error: '>' expected.",
ruleId: null,
severity: 2,
source: "const element = <T/>"
}]
);
});

test("test.ts", () => {
const code = "const element = <T/>";
const config = {
parser: "typescript-eslint-parser"
};
const messages = linter.verify(code, config, { filename: "test.ts" });

assert.deepStrictEqual(
messages,
[{
column: 18,
fatal: true,
line: 1,
message: "Parsing error: '>' expected.",
ruleId: null,
severity: 2,
source: "const element = <T/>"
}]
);
});

test("test.ts with 'jsx' option", () => {
const code = "const element = <T/>";
const config = {
parser: "typescript-eslint-parser",
parserOptions: {
jsx: true
}
};
const messages = linter.verify(code, config, { filename: "test.ts" });

assert.deepStrictEqual(messages, []);
});

test("test.tsx", () => {
const code = "const element = <T/>";
const config = {
parser: "typescript-eslint-parser"
};
const messages = linter.verify(code, config, { filename: "test.tsx" });

assert.deepStrictEqual(messages, []);
});
});
});