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

Commit

Permalink
Breaking: switch 'jsx' option by filename (fixes #517) (#543)
Browse files Browse the repository at this point in the history
* Breaking: switch 'jsx' option by filename (fixes #517)

* update README.md

* don't modify the option object

* update behavior

* use expect() instead of assert()
  • Loading branch information
mysticatea authored and kaicataldo committed Nov 9, 2018
1 parent 3b1ed17 commit 2d22321
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
7 changes: 5 additions & 2 deletions README.md
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
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
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([]);
});
});
});

0 comments on commit 2d22321

Please sign in to comment.