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

Chore: Powerful integration tests and improved README #483

Merged
merged 1 commit into from
Jun 10, 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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/integration/*
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
sudo: required

services:
- docker

language: node_js
sudo: false

node_js:
- "6"
- "8"
- "9"
- "10"

after_success:
- npm run coveralls
75 changes: 54 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,87 @@

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.
## Usage

## Supported TypeScript Version
This parser is actually somewhat generic and robust - it could be used to power any use-case which requires taking TypeScript source code and producing an ESTree-compatiable AST.

We will always endeavor to support the latest stable version of TypeScript.
In fact, that is exactly what it is used for in the popular open-source code formatter, [Prettier](https://prettier.io), to power its TypeScript support.

The version of TypeScript currently supported by this parser is `~2.9.1`. This is reflected in the `devDependency` requirement within the package.json file, and it is what the tests will be run against. We have an open `peerDependency` requirement in order to allow for experimentation on newer/beta versions of TypeScript.
Nevertheless, the parser does have a special appreciation for ESLint-specific use-cases built in, and can even produce a slightly different AST for ESLint if needed (using the special `parseForESLint()` method).

If you use a non-supported version of TypeScript, the parser will log a warning to the console.
The majority of users of this parser use it to enable them to use ESLint on their TypeScript source files, so they will not actually be interacting with the parser directly. Instead they will configure ESLint to use it instead of its default parser, [espree](https://github.com/eslint/espree), which does not understand TypeScript.

**Please ensure that you are using a supported version before submitting any issues/bug reports.**
## Usage with ESLint

## Known Issues
There is sometimes an incorrect assumption that the parser itself is what does everything necessary to facilitate the use of ESLint with TypeScript. In actuality, it is the combination of the parser _and_ one or more plugins which allow you to maximize your usage of ESLint with TypeScript.

The following ESLint rules will fail on acceptable code:
- no-undef [#416](https://github.com/eslint/typescript-eslint-parser/issues/416)

The follow ESLint plugins have issues when used with this parser:
- eslint-plugin-import
- prefer-default-export - Will fail exports inside of Namespaces or Modules
For example, once this parser successfully produces an AST for the TypeScript source code, it might well contain some information which simply does not exist in a standard JavaScript context, such as the data for a TypeScript-specific construct, like an `interface`.

## Usage
The core rules built into ESLint, such as `indent` have no knowledge of such constructs, so it is impossible to expect them to work out of the box with them.

Instead, you also need to make use of one more plugins which will add or extend rules with TypeScript-specific features.

By far the most common case will be installing the [eslint-plugin-typescript](https://github.com/nzakas/eslint-plugin-typescript) plugin, but there are also other relevant options available such a [eslint-plugin-tslint](https://github.com/JamesHenry/eslint-plugin-tslint).

Install:

```
npm i typescript-eslint-parser --save-dev
```sh
npm install --save-dev typescript-eslint-parser
```

And in your ESLint configuration file:

```
```json
"parser": "typescript-eslint-parser"
```

## Supported TypeScript Version

We will always endeavor to support the latest stable version of TypeScript.

The version of TypeScript currently supported by this parser is `~2.9.1`. This is reflected in the `devDependency` requirement within the package.json file, and it is what the tests will be run against. We have an open `peerDependency` requirement in order to allow for experimentation on newer/beta versions of TypeScript.

If you use a non-supported version of TypeScript, the parser will log a warning to the console.

**Please ensure that you are using a supported version before submitting any issues/bug reports.**

## Help Wanted!

If you're familiar with TypeScript and ESLint, and you'd like to see this project progress, please consider contributing. We need people with a good knowledge of TypeScript to ensure this parser is useful.

## Reporting Bugs
## Reporting Issues

Please check the current list of open and known issues and ensure the bug has not been reported before. When creating a new issue provide as much information about your environment as possible. This includes:
The vast majority of issues which are submitted here are not actually parsing bugs at all. They are integration issues with the ESLint ecosystem.

This is not ideal, but users need a place to be able to report those things, so it has become accepted that that will also be done in this repo.

Please check the current list of open and known issues and ensure the issue has not been reported before. When creating a new issue provide as much information about your environment as possible. This includes:
- ESLint Version
- TypeScript version
- TypeScript parser version
- The `typescript-eslint-parser` version
- ESLint config or rules and plugins currently enabled

As well include a small code sample that can be used to reproduce the issue.
## Integration Tests

We have a very flexible way of running integration tests which connects all of the moving parts of the usage of this parser in the ESLint ecosystem.

We run each test within its own docker container, and so each one has complete autonomy over what dependencies/plugins are installed and what versions are used. This also has the benefit of not bloating the `package.json` and `node_modules` of the parser project itself.

> If you are going to submit an issue related to the usage of this parser with ESLint, please consider creating a failing integration which clearly demonstrates the behavior. It's honestly super quick!

You just need to duplicate on of the existing test sub-directories found in `tests/integration/`, tweak the dependencies and ESLint config to match what you need, and add a new entry to the docker-compose.yml file which matches the format of the existing ones.

Then run:

```sh
npm run integration-tests
```

If you ever need to change what gets installed when the docker images are built by docker-compose, you will first need to kill the existing containers by running:

```sh
npm run kill-integration-test-containers
```

## Contributing

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"devDependencies": {
"babel-code-frame": "6.26.0",
"babylon": "7.0.0-beta.39",
"dedent": "0.7.0",
"eslint": "4.19.1",
"eslint-config-eslint": "4.0.0",
"eslint-plugin-node": "6.0.1",
Expand All @@ -46,7 +45,8 @@
"test": "node Makefile.js test && npm run integration-tests && npm run ast-alignment-tests",
"jest": "jest",
"ast-alignment-tests": "jest --config=./tests/ast-alignment/jest.config.js",
"integration-tests": "jest --config=./tests/integration/jest.config.js",
"integration-tests": "docker-compose -f tests/integration/docker-compose.yml up",
"kill-integration-test-containers": "docker-compose -f tests/integration/docker-compose.yml down -v --rmi local",
"lint": "node Makefile.js lint",
"release": "eslint-release",
"ci-release": "eslint-ci-release",
Expand Down
2 changes: 0 additions & 2 deletions tests/integration/.eslintrc.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root: true

# typescript-eslint-parser
parser: /usr/parser/parser.js

extends: eslint:all
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM node:carbon

# Copy the package.json into the container so that we can
# run the install. Every other file will be linked, rather
# than copied to allow for changes without rebuilds
WORKDIR /usr
COPY ./package.json /usr/

# Create file which will be executed by jest
# to assert that the lint output is what we expect
RUN echo "const expectedLintOutput = require('./linked/expected-lint-output.json');\n" \
"const actualLintOutput = require('./lint-output.json');\n" \
"\n" \
"test('it should produce the expected lint ouput', () => {\n" \
" expect(actualLintOutput).toEqual(expectedLintOutput);\n" \
"});\n" > test.js

# Install dependencies
RUN npm install

# Run ESLint and assert that the output matches our expectations
CMD [ "npm", "start" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
[{
"filePath": "/usr/linked/index.ts",
"messages": [{
"ruleId": "strict",
"severity": 2,
"message": "Use the function form of 'use strict'.",
"line": 1,
"column": 1,
"nodeType": "ExpressionStatement",
"source": "\"use strict\";",
"endLine": 1,
"endColumn": 14
}, {
"ruleId": "no-undef",
"severity": 2,
"message": "'FF' is not defined.",
"line": 3,
"column": 19,
"nodeType": "Identifier",
"source": "declare namespace FF {",
"endLine": 3,
"endColumn": 21
}, {
"ruleId": "no-unused-vars",
"severity": 2,
"message": "'Foo' is defined but never used.",
"line": 4,
"column": 11,
"nodeType": "Identifier",
"source": " class Foo extends Bar.Baz {",
"endLine": 4,
"endColumn": 14
}, {
"ruleId": "no-undef",
"severity": 2,
"message": "'Bar' is not defined.",
"line": 4,
"column": 23,
"nodeType": "Identifier",
"source": " class Foo extends Bar.Baz {",
"endLine": 4,
"endColumn": 26
}, {
"ruleId": "padded-blocks",
"severity": 2,
"message": "Block must be padded by blank lines.",
"line": 4,
"column": 31,
"nodeType": "ClassBody",
"source": " class Foo extends Bar.Baz {",
"fix": {
"range": [69, 69],
"text": "\n"
}
}, {
"ruleId": "padded-blocks",
"severity": 2,
"message": "Block must be padded by blank lines.",
"line": 6,
"column": 5,
"nodeType": "ClassBody",
"source": " }",
"fix": {
"range": [94, 94],
"text": "\n"
}
}, {
"ruleId": "no-redeclare",
"severity": 2,
"message": "'Foo' is already defined.",
"line": 10,
"column": 11,
"nodeType": "Identifier",
"source": " class Foo extends Bar.Baz {",
"endLine": 10,
"endColumn": 14
}, {
"ruleId": "no-undef",
"severity": 2,
"message": "'Bar' is not defined.",
"line": 10,
"column": 23,
"nodeType": "Identifier",
"source": " class Foo extends Bar.Baz {",
"endLine": 10,
"endColumn": 26
}, {
"ruleId": "padded-blocks",
"severity": 2,
"message": "Block must be padded by blank lines.",
"line": 10,
"column": 31,
"nodeType": "ClassBody",
"source": " class Foo extends Bar.Baz {",
"fix": {
"range": [152, 152],
"text": "\n"
}
}, {
"ruleId": "padded-blocks",
"severity": 2,
"message": "Block must be padded by blank lines.",
"line": 12,
"column": 5,
"nodeType": "ClassBody",
"source": " }",
"fix": {
"range": [177, 177],
"text": "\n"
}
}, {
"ruleId": "no-redeclare",
"severity": 2,
"message": "'Foo' is already defined.",
"line": 15,
"column": 15,
"nodeType": "Identifier",
"source": "declare class Foo extends Bar.Baz {",
"endLine": 15,
"endColumn": 18
}, {
"ruleId": "no-undef",
"severity": 2,
"message": "'Bar' is not defined.",
"line": 15,
"column": 27,
"nodeType": "Identifier",
"source": "declare class Foo extends Bar.Baz {",
"endLine": 15,
"endColumn": 30
}, {
"ruleId": "padded-blocks",
"severity": 2,
"message": "Block must be padded by blank lines.",
"line": 15,
"column": 35,
"nodeType": "ClassBody",
"source": "declare class Foo extends Bar.Baz {",
"fix": {
"range": [217, 217],
"text": "\n"
}
}, {
"ruleId": "padded-blocks",
"severity": 2,
"message": "Block must be padded by blank lines.",
"line": 17,
"column": 1,
"nodeType": "ClassBody",
"source": "}",
"fix": {
"range": [234, 234],
"text": "\n"
}
}, {
"ruleId": "no-undef",
"severity": 2,
"message": "'d3' is not defined.",
"line": 19,
"column": 19,
"nodeType": "Identifier",
"source": "declare namespace d3 {",
"endLine": 19,
"endColumn": 21
}, {
"ruleId": "no-undef",
"severity": 2,
"message": "'select' is not defined.",
"line": 20,
"column": 21,
"nodeType": "Identifier",
"source": " export function select(selector: string): Selection<any>;",
"endLine": 20,
"endColumn": 27
}, {
"ruleId": "no-undef",
"severity": 2,
"message": "'selector' is not defined.",
"line": 20,
"column": 28,
"nodeType": "Identifier",
"source": " export function select(selector: string): Selection<any>;",
"endLine": 20,
"endColumn": 44
}, {
"ruleId": "no-undef",
"severity": 2,
"message": "'Selection' is not defined.",
"line": 20,
"column": 47,
"nodeType": "Identifier",
"source": " export function select(selector: string): Selection<any>;",
"endLine": 20,
"endColumn": 56
}, {
"ruleId": "eol-last",
"severity": 2,
"message": "Newline required at end of file but not found.",
"line": 21,
"column": 2,
"nodeType": "Program",
"source": "}",
"messageId": "missing",
"fix": {
"range": [323, 323],
"text": "\n"
}
}],
"errorCount": 19,
"warningCount": 0,
"fixableErrorCount": 7,
"fixableWarningCount": 0,
"source": "\"use strict\";\n\ndeclare namespace FF {\n class Foo extends Bar.Baz {\n far(): any;\n }\n}\n\ndeclare module \"FF\" {\n class Foo extends Bar.Baz {\n far(): any;\n }\n}\n\ndeclare class Foo extends Bar.Baz {\n far(): any;\n}\n\ndeclare namespace d3 {\n export function select(selector: string): Selection<any>;\n}"
}]
Loading