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

Commit a65c8ca

Browse files
committed
Breaking: Implement parseForESLint() function
1 parent 04e948f commit a65c8ca

13 files changed

+330
-3255
lines changed

README.md

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# TypeScript ESLint Parser (Experimental)
1+
# TypeScript ESLint Parser
22

3-
A parser that converts TypeScript into an [ESTree](https://github.com/estree/estree)-compatible form so it can be used in ESLint.
3+
A parser that converts TypeScript source code into an [ESTree](https://github.com/estree/estree)-compatible form.
44

55
**Important:** This parser is not fully compatible with all ESLint rules and plugins. Some rules will improperly mark source code as failing or not find problems where it should.
66

@@ -20,18 +20,10 @@ The following ESLint rules will fail on acceptable code:
2020
- no-undef [#77](https://github.com/eslint/typescript-eslint-parser/issues/77)
2121
- no-unused-vars [#77](https://github.com/eslint/typescript-eslint-parser/issues/77)
2222
- no-useless-constructor [#77](https://github.com/eslint/typescript-eslint-parser/issues/77)
23-
- space-infix-ops [#224](https://github.com/eslint/typescript-eslint-parser/issues/224)
2423

2524
The follow ESLint plugins have issues when used with this parser:
26-
- eslint-plugin-react [#213](https://github.com/eslint/typescript-eslint-parser/issues/213)
2725
- eslint-plugin-import
2826
- prefer-default-export - Will fail exports inside of Namespaces or Modules
29-
30-
The following TypeScript syntax will cause rules to fail or ESLint v3 to crash:
31-
- Empty body functions
32-
- Abstract methods
33-
- Function overloading
34-
- Declared functions
3527

3628
## Usage
3729

lib/ast-converter.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ module.exports = (ast, extra) => {
5858
ast,
5959
additionalOptions: {
6060
errorOnUnknownASTType: extra.errorOnUnknownASTType || false,
61-
useJSXTextNode: extra.useJSXTextNode || false
61+
useJSXTextNode: extra.useJSXTextNode || false,
62+
parseForESLint: extra.parseForESLint
6263
}
6364
});
6465

lib/convert.js

+19
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ module.exports = function convert(config) {
6969
});
7070
}
7171

72+
/**
73+
* If we are parsing for ESLint we need to perform a custom namespacing step
74+
* on functions which have no body so that we do not break any ESLint rules which
75+
* rely on them to have one.
76+
*
77+
* @param {ESTreeNode} functionNode the converted ESTreeNode
78+
* @returns {void}
79+
*/
80+
function namespaceEmptyBodyFunctionForESLint(functionNode) {
81+
if (!config.additionalOptions.parseForESLint || functionNode.body) {
82+
return;
83+
}
84+
functionNode.type = `TSEmptyBody${functionNode.type}`;
85+
}
86+
7287
/**
7388
* Converts a TypeScript node into an ESTree node.
7489
* @param {TSNode} child the child TSNode
@@ -634,6 +649,8 @@ module.exports = function convert(config) {
634649
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
635650
}
636651

652+
namespaceEmptyBodyFunctionForESLint(result);
653+
637654
// check for exports
638655
result = nodeUtils.fixExports(node, result, ast);
639656

@@ -954,6 +971,8 @@ module.exports = function convert(config) {
954971
method.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
955972
}
956973

974+
namespaceEmptyBodyFunctionForESLint(result.value);
975+
957976
break;
958977

959978
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"babel-code-frame": "6.26.0",
2222
"babylon": "7.0.0-beta.34",
2323
"dedent": "0.7.0",
24-
"eslint": "4.13.0",
24+
"eslint": "4.13.1",
2525
"eslint-config-eslint": "4.0.0",
2626
"eslint-plugin-node": "5.2.1",
2727
"eslint-release": "0.10.3",

parser.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @fileoverview Parser that converts TypeScript into ESTree format.
33
* @author Nicholas C. Zakas
4+
* @author James Henry <https://github.com/JamesHenry>
45
* @copyright jQuery Foundation and other contributors, https://jquery.org/
56
* MIT License
67
*/
@@ -104,6 +105,13 @@ function parse(code, options) {
104105
extra.log = Function.prototype;
105106
}
106107

108+
/**
109+
* Provide the context as to whether or not we are parsing for ESLint,
110+
* specifically
111+
*/
112+
if (options.parseForESLint) {
113+
extra.parseForESLint = true;
114+
}
107115
}
108116

109117
if (!isRunningSupportedTypeScriptVersion && !warnedAboutTSVersion) {
@@ -177,6 +185,12 @@ exports.version = require("./package.json").version;
177185

178186
exports.parse = parse;
179187

188+
exports.parseForESLint = function parseForESLint(code, options) {
189+
options.parseForESLint = true;
190+
const ast = parse(code, options);
191+
return { ast };
192+
};
193+
180194
// Deep copy.
181195
/* istanbul ignore next */
182196
exports.Syntax = (function() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
3+
declare namespace FF {
4+
class Foo extends Bar.Baz {
5+
far(): any;
6+
}
7+
}
8+
9+
declare module "FF" {
10+
class Foo extends Bar.Baz {
11+
far(): any;
12+
}
13+
}
14+
15+
declare class Foo extends Bar.Baz {
16+
far(): any;
17+
}
18+
19+
declare namespace d3 {
20+
export function select(selector: string): Selection<any>;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @a
3+
*/
4+
foo;
5+
6+
/**
7+
* @a
8+
*/
9+
/**
10+
* a
11+
*/
12+
foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export async function readFile(
2+
filename: string,
3+
options?: { flag?: string }
4+
): Promise<Buffer>
5+
export async function readFile(
6+
filename: string,
7+
options?: { encoding: BufferEncoding; flag?: string }
8+
): Promise<string>
9+
export async function readFile(
10+
filename: string,
11+
options?: { encoding?: string; flag?: string }
12+
): Promise<Buffer | string> {
13+
// ...
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class a {
2+
/** @b {} c */
3+
d = e => {}
4+
}

0 commit comments

Comments
 (0)