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

Breaking: Implement parseForESLint() function #412

Merged
merged 2 commits into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 2 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TypeScript ESLint Parser (Experimental)
# TypeScript ESLint Parser

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

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

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

The follow ESLint plugins have issues when used with this parser:
- eslint-plugin-react [#213](https://github.com/eslint/typescript-eslint-parser/issues/213)
- eslint-plugin-import
- prefer-default-export - Will fail exports inside of Namespaces or Modules

The following TypeScript syntax will cause rules to fail or ESLint v3 to crash:
- Empty body functions
- Abstract methods
- Function overloading
- Declared functions

## Usage

Expand Down
3 changes: 2 additions & 1 deletion lib/ast-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ module.exports = (ast, extra) => {
ast,
additionalOptions: {
errorOnUnknownASTType: extra.errorOnUnknownASTType || false,
useJSXTextNode: extra.useJSXTextNode || false
useJSXTextNode: extra.useJSXTextNode || false,
parseForESLint: extra.parseForESLint
}
});

Expand Down
19 changes: 19 additions & 0 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ module.exports = function convert(config) {
});
}

/**
* If we are parsing for ESLint we need to perform a custom namespacing step
* on functions which have no body so that we do not break any ESLint rules which
* rely on them to have one.
*
* @param {ESTreeNode} functionNode the converted ESTreeNode
* @returns {void}
*/
function namespaceEmptyBodyFunctionForESLint(functionNode) {
if (!config.additionalOptions.parseForESLint || functionNode.body) {
return;
}
functionNode.type = `TSEmptyBody${functionNode.type}`;
}

/**
* Converts a TypeScript node into an ESTree node.
* @param {TSNode} child the child TSNode
Expand Down Expand Up @@ -634,6 +649,8 @@ module.exports = function convert(config) {
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
}

namespaceEmptyBodyFunctionForESLint(result);

// check for exports
result = nodeUtils.fixExports(node, result, ast);

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

namespaceEmptyBodyFunctionForESLint(result.value);

break;

}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"babel-code-frame": "6.26.0",
"babylon": "7.0.0-beta.34",
"dedent": "0.7.0",
"eslint": "4.13.0",
"eslint": "4.13.1",
"eslint-config-eslint": "4.0.0",
"eslint-plugin-node": "5.2.1",
"eslint-release": "0.10.3",
Expand Down
14 changes: 14 additions & 0 deletions parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @fileoverview Parser that converts TypeScript into ESTree format.
* @author Nicholas C. Zakas
* @author James Henry <https://github.com/JamesHenry>
* @copyright jQuery Foundation and other contributors, https://jquery.org/
* MIT License
*/
Expand Down Expand Up @@ -104,6 +105,13 @@ function parse(code, options) {
extra.log = Function.prototype;
}

/**
* Provide the context as to whether or not we are parsing for ESLint,
* specifically
*/
if (options.parseForESLint) {
extra.parseForESLint = true;
}
}

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

exports.parse = parse;

exports.parseForESLint = function parseForESLint(code, options) {
options.parseForESLint = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be best to avoid mutating the options object provided by the user. (This was also a problem in eslint/js#329.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case I would be forced to change the signature of parse() to accept an optional third argument. There's no reason that would be an issue, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would be an issue.

If you don't want the change to be visible to the public API, you could create a helper function, e.g. parseMaybeForESlint(code, options, internalOptions), and have parse call parseMaybeForESLint(code, options, { isForESLint: false }) and have parseForESLint call parseMaybeForESLint(code, options, { isForESLint: true }).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just defensively cloning the options so the user can't see the mutation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also do options = Object.assign({}, options, { parseForESLint: true }). Or insert options = Object.create(options) before this line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems strange to have parseForESLint be mixed with the user-provided options object. For example, if the user added parseForESLint: true to their options object for some reason, they could cause strange behavior.

const ast = parse(code, options);
return { ast };
};

// Deep copy.
/* istanbul ignore next */
exports.Syntax = (function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";

declare namespace FF {
class Foo extends Bar.Baz {
far(): any;
}
}

declare module "FF" {
class Foo extends Bar.Baz {
far(): any;
}
}

declare class Foo extends Bar.Baz {
far(): any;
}

declare namespace d3 {
export function select(selector: string): Selection<any>;
}
12 changes: 12 additions & 0 deletions tests/integration/external-fixtures/jsdoc-indent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @a
*/
foo;

/**
* @a
*/
/**
* a
*/
foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export async function readFile(
filename: string,
options?: { flag?: string }
): Promise<Buffer>
export async function readFile(
filename: string,
options?: { encoding: BufferEncoding; flag?: string }
): Promise<string>
export async function readFile(
filename: string,
options?: { encoding?: string; flag?: string }
): Promise<Buffer | string> {
// ...
}
4 changes: 4 additions & 0 deletions tests/integration/external-fixtures/range-error-indent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class a {
/** @b {} c */
d = e => {}
}
Loading