-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): extend eslint semi rule #411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4bd088f
feat(eslint-plugin): extend eslint semi rule
aprilrd d7edb1e
fix(eslint-plugin): make semi handle class property
aprilrd 9d775d5
fix(eslint-plugin): make semi handle interface export default
aprilrd 2fa0a3a
Merge branch 'master' into type-aliase-semicolon
JamesHenry dc9e63f
docs(eslint-plugin): add semi rule doc
aprilrd 8d6897b
test(eslint-plugin): add eslint test cases for semi
aprilrd 2f4a5df
docs(eslint-plugin): prevent formatting in semi doc examples
aprilrd 8c5c1c9
docs(eslint-plugin): update per review
aprilrd 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
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,74 @@ | ||
# require or disallow semicolons instead of ASI (semi) | ||
|
||
This rule enforces consistent use of semicolons. | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [eslint/semi](https://eslint.org/docs/rules/semi) rule. | ||
It supports all options and features of the base rule plus TS type assertions. | ||
|
||
## How to use | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"semi": "off", | ||
"@typescript-eslint/semi": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [eslint/semi options](https://eslint.org/docs/rules/semi#options). | ||
|
||
### always | ||
|
||
Examples of **incorrect** TS code for this rule with the default `"always"` option: | ||
|
||
<!-- prettier-ignore --> | ||
```ts | ||
/*eslint @typescript-eslint/semi: "error"*/ | ||
type Foo = {} | ||
|
||
class PanCamera extends FreeCamera { | ||
public invertY: boolean = false | ||
} | ||
``` | ||
|
||
Examples of **correct** TS code for this rule with the default `"always"` option: | ||
|
||
<!-- prettier-ignore --> | ||
```ts | ||
/*eslint @typescript-eslint/semi: "error"*/ | ||
type Foo = {}; | ||
|
||
class PanCamera extends FreeCamera { | ||
public invertY: boolean = false; | ||
} | ||
``` | ||
|
||
### never | ||
|
||
Examples of **incorrect** TS code for this rule with the `"never"` option: | ||
|
||
<!-- prettier-ignore --> | ||
```ts | ||
/*eslint @typescript-eslint/semi: ["error", "never"]*/ | ||
type Foo = {}; | ||
|
||
class PanCamera extends FreeCamera { | ||
public invertY: boolean = false; | ||
} | ||
``` | ||
|
||
Examples of **correct** TS code for this rule with the `"never"` option: | ||
|
||
<!-- prettier-ignore --> | ||
```ts | ||
/*eslint @typescript-eslint/semi: ["error", "never"]*/ | ||
type Foo = {} | ||
|
||
class PanCamera extends FreeCamera { | ||
public invertY: boolean = false | ||
} | ||
``` |
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,64 @@ | ||
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/typescript-estree'; | ||
import baseRule from 'eslint/lib/rules/semi'; | ||
import * as util from '../util'; | ||
|
||
type Options = util.InferOptionsTypeFromRule<typeof baseRule>; | ||
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'semi', | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: 'Require or disallow semicolons instead of ASI', | ||
tslintRuleName: 'semi', | ||
category: 'Stylistic Issues', | ||
recommended: 'error', | ||
}, | ||
fixable: 'code', | ||
schema: baseRule.meta.schema, | ||
messages: baseRule.meta.messages, | ||
}, | ||
defaultOptions: ['always', { omitLastInOneLineBlock: false }] as Options, | ||
create(context, optionsWithDefaults) { | ||
// because we extend the base rule, have to update opts on the context | ||
// the context defines options as readonly though... | ||
const contextWithDefaults: typeof context = Object.create(context, { | ||
options: { | ||
writable: false, | ||
configurable: false, | ||
value: optionsWithDefaults, | ||
}, | ||
}); | ||
|
||
const rules = baseRule.create(contextWithDefaults); | ||
|
||
return Object.assign({}, rules, { | ||
TSTypeAliasDeclaration(node: TSESTree.TSTypeAliasDeclaration) { | ||
return rules.VariableDeclaration({ | ||
...node, | ||
type: AST_NODE_TYPES.VariableDeclaration, | ||
kind: 'const' as 'const', | ||
declarations: [], | ||
}); | ||
}, | ||
ClassProperty(node: TSESTree.ClassProperty) { | ||
return rules.VariableDeclaration({ | ||
...node, | ||
type: AST_NODE_TYPES.VariableDeclaration, | ||
kind: 'const' as 'const', | ||
declarations: [], | ||
}); | ||
}, | ||
ExportDefaultDeclaration(node: TSESTree.ExportDefaultDeclaration) { | ||
if ( | ||
!/(?:Class|Function|TSInterface)Declaration/.test( | ||
node.declaration.type, | ||
) | ||
) { | ||
rules.ExportDefaultDeclaration(node); | ||
} | ||
}, | ||
}); | ||
}, | ||
}); |
Oops, something went wrong.
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.