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 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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
103 changes: 102 additions & 1 deletion tests/lib/tsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

//------------------------------------------------------------------------------
Expand All @@ -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 = <T/>";
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 = <T/>"
}]);
});

test("filePath was not provided and 'jsx:true' option", () => {
const code = "const element = <T/>";
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 = <T/>";
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 = <T/>"
}]);
});

test("test.ts with 'jsx:true' option", () => {
const code = "const element = <T/>";
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 = <T/>"
}]);
});

test("test.tsx", () => {
const code = "const element = <T/>";
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 = <T/>";
const config = {
parser: "typescript-eslint-parser",
parserOptions: {
jsx: false
}
};
const messages = linter.verify(code, config, { filename: "test.tsx" });

expect(messages).toStrictEqual([]);
});
});
});