This repository was archived by the owner on Jan 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
[FEAT] [no-semantic-errors] Add rule (Requires Type Info) #245
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
15c0573
Add rule `no-semantic-errors`
armano2 4e8b91b
correct handling messageText for nested errors and add documentation
armano2 fff4fe8
Merge branch 'master' into no-semantic-errors
armano2 b0cd079
Add util method to retrieve parserServices from context
armano2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Enforces that there is no semantic and syntactic errors (no-semantic-errors) | ||
|
||
This rule reports all semantic and type errors provided by diagnostics from typescript. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
interface Foo { | ||
hello: string; | ||
} | ||
const foo: string = ({ hello: 2 } as Foo)!.foo; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
interface Foo { | ||
hello: string; | ||
} | ||
const foo: string = ({ hello: "Bar" } as Foo).hello; | ||
``` | ||
|
||
### Options | ||
|
||
```json | ||
{ | ||
"typescript/no-this-alias": "no-semantic-errors" | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @fileoverview Enforces that there is no semantic and syntactic errors | ||
* @author Armano <https://github.com/armano2> | ||
*/ | ||
"use strict"; | ||
|
||
const util = require("../util"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: | ||
"Enforces that there is no semantic and syntactic errors", | ||
category: "TypeScript", | ||
url: util.metaDocsUrl("no-semantic-errors"), | ||
}, | ||
schema: [], | ||
}, | ||
|
||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
const program = util.getParserServices(context).program; | ||
|
||
return { | ||
Program(node) { | ||
const semantic = program.getSemanticDiagnostics() || []; | ||
const syntactic = program.getSyntacticDiagnostics() || []; | ||
|
||
const errors = semantic | ||
.concat(syntactic) | ||
// DiagnosticCategory.Error = 1, | ||
.filter(error => error.category === 1); | ||
|
||
for (const error of errors) { | ||
const errorNode = error.start | ||
? sourceCode.getNodeByRangeIndex(error.start) | ||
: node; | ||
|
||
context.report({ | ||
node: errorNode, | ||
message: | ||
typeof error.messageText === "object" | ||
? error.messageText.messageText | ||
: error.messageText, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default interface Foo { | ||
name: string | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "commonjs", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"lib": ["es2015", "es2017"] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* @fileoverview Enforces that there is no semantic and syntactic errors | ||
* @author Armano <https://github.com/armano2> | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
const path = require("path"); | ||
|
||
const rule = require("../../../lib/rules/no-semantic-errors"), | ||
RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const rootPath = path.join(process.cwd(), "tests/lib/fixtures/empty"); | ||
// valid filePath is required to get access to lib | ||
// @see https://github.com/JamesHenry/typescript-estree/issues/50 | ||
const filePath = path.join(rootPath, "empty.ts"); | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: "typescript-eslint-parser", | ||
parserOptions: { | ||
generateServices: true, | ||
tsconfigRootDir: rootPath, | ||
project: "./tsconfig.json", | ||
}, | ||
}); | ||
|
||
ruleTester.run("no-errors", rule, { | ||
valid: [ | ||
{ | ||
filename: filePath, | ||
code: ` | ||
import Foo from './import'; | ||
var foo: Foo = { | ||
name: 'test' | ||
}; | ||
`, | ||
}, | ||
{ | ||
filename: filePath, | ||
code: ` | ||
interface Foo { | ||
hello: string; | ||
} | ||
const foo: string = ({ hello: 'Bar' } as Foo).hello | ||
`, | ||
}, | ||
{ | ||
filename: filePath, | ||
code: `var foo: number = parseInt("5.5", 10) + 10;`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
filename: filePath, | ||
code: `var foo: string = parseInt("5.5", 10) + 10;`, | ||
errors: [ | ||
{ | ||
message: | ||
"Type 'number' is not assignable to type 'string'.", | ||
line: 1, | ||
column: 5, | ||
type: "Identifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
filename: filePath, | ||
code: ` | ||
import Foo from './import'; | ||
var foo: Foo = { | ||
name: 2 | ||
}; | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
"Type 'number' is not assignable to type 'string'.", | ||
line: 4, | ||
column: 5, | ||
type: "Identifier", | ||
}, | ||
], | ||
}, | ||
{ | ||
filename: filePath, | ||
code: ` | ||
import Foo from './not-found'; | ||
var foo: Foo = { | ||
name: 2 | ||
}; | ||
`, | ||
errors: [ | ||
{ | ||
message: "Cannot find module './not-found'.", | ||
line: 2, | ||
column: 17, | ||
type: "Literal", | ||
}, | ||
], | ||
}, | ||
{ | ||
filename: filePath, | ||
code: ` | ||
interface Foo { | ||
hello: string; | ||
} | ||
const foo: string = ({ hello: 2 } as Foo)!.foo | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
"Conversion of type '{ hello: number; }' to type 'Foo' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.", | ||
line: 5, | ||
column: 22, | ||
type: "ObjectExpression", | ||
}, | ||
{ | ||
message: "Property 'foo' does not exist on type 'Foo'.", | ||
line: 5, | ||
column: 44, | ||
type: "Identifier", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2996,15 +2996,6 @@ typedarray@^0.0.6: | |
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" | ||
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= | ||
|
||
[email protected]: | ||
version "21.0.2" | ||
resolved "https://registry.yarnpkg.com/typescript-eslint-parser/-/typescript-eslint-parser-21.0.2.tgz#270af10e4724528677fbcf34ea495284bec3a894" | ||
integrity sha512-u+pj4RVJBr4eTzj0n5npoXD/oRthvfUCjSKndhNI714MG0mQq2DJw5WP7qmonRNIFgmZuvdDOH3BHm9iOjIAfg== | ||
dependencies: | ||
eslint-scope "^4.0.0" | ||
eslint-visitor-keys "^1.0.0" | ||
typescript-estree "5.3.0" | ||
|
||
typescript-eslint-parser@^16.0.0: | ||
version "16.0.1" | ||
resolved "https://registry.yarnpkg.com/typescript-eslint-parser/-/typescript-eslint-parser-16.0.1.tgz#b40681c7043b222b9772748b700a000b241c031b" | ||
|
@@ -3013,6 +3004,14 @@ typescript-eslint-parser@^16.0.0: | |
lodash.unescape "4.0.1" | ||
semver "5.5.0" | ||
|
||
"typescript-eslint-parser@git+https://github.com/uniqueiniquity/typescript-eslint-parser.git#0625f29a9721c6f10a7522de6c9d236a671bd1ac": | ||
version "21.0.2" | ||
resolved "git+https://github.com/uniqueiniquity/typescript-eslint-parser.git#0625f29a9721c6f10a7522de6c9d236a671bd1ac" | ||
dependencies: | ||
eslint-scope "^4.0.0" | ||
eslint-visitor-keys "^1.0.0" | ||
typescript-estree "5.3.0" | ||
|
||
[email protected]: | ||
version "5.3.0" | ||
resolved "https://registry.yarnpkg.com/typescript-estree/-/typescript-estree-5.3.0.tgz#fb6c977b5e21073eb16cbdc0338a7f752da99ff5" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.