Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add extension rule
keyword-spacing
(#1739)
- Loading branch information
Showing
7 changed files
with
312 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,22 @@ | ||
# Enforce consistent spacing before and after keywords (`keyword-spacing`) | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [`eslint/keyword-spacing`](https://eslint.org/docs/rules/keyword-spacing) rule. | ||
This version adds support for generic type parameters on function calls. | ||
|
||
## How to use | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"keyword-spacing": "off", | ||
"@typescript-eslint/keyword-spacing": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [`eslint/keyword-spacing` options](https://eslint.org/docs/rules/keyword-spacing#options). | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/keyword-spacing.md)</sup> |
This file contains 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 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 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,52 @@ | ||
import { AST_TOKEN_TYPES } from '@typescript-eslint/experimental-utils'; | ||
import baseRule from 'eslint/lib/rules/keyword-spacing'; | ||
import * as util from '../util'; | ||
|
||
export type Options = util.InferOptionsTypeFromRule<typeof baseRule>; | ||
export type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'keyword-spacing', | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: 'Enforce consistent spacing before and after keywords', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
extendsBaseRule: true, | ||
}, | ||
fixable: 'whitespace', | ||
schema: baseRule.meta.schema, | ||
messages: baseRule.meta.messages, | ||
}, | ||
defaultOptions: [{}], | ||
|
||
create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
const baseRules = baseRule.create(context); | ||
return { | ||
...baseRules, | ||
TSAsExpression(node): void { | ||
const asToken = util.nullThrows( | ||
sourceCode.getTokenAfter( | ||
node.expression, | ||
token => token.value === 'as', | ||
), | ||
util.NullThrowsReasons.MissingToken('as', node.type), | ||
); | ||
const oldTokenType = asToken.type; | ||
// as is a contextual keyword, so it's always reported as an Identifier | ||
// the rule looks for keyword tokens, so we temporarily override it | ||
// we mutate it at the token level because the rule calls sourceCode.getFirstToken, | ||
// so mutating a copy would not change the underlying copy returned by that method | ||
asToken.type = AST_TOKEN_TYPES.Keyword; | ||
|
||
// use this selector just because it is just a call to `checkSpacingAroundFirstToken` | ||
baseRules.DebuggerStatement(asToken as never); | ||
|
||
// make sure to reset the type afterward so we don't permanently mutate the AST | ||
asToken.type = oldTokenType; | ||
}, | ||
}; | ||
}, | ||
}); |
153 changes: 153 additions & 0 deletions
153
packages/eslint-plugin/tests/rules/keyword-spacing.test.ts
This file contains 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,153 @@ | ||
/* eslint-disable eslint-comments/no-use */ | ||
// this rule tests the spacing, which prettier will want to fix and break the tests | ||
/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ | ||
/* eslint-enable eslint-comments/no-use */ | ||
import { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import rule, { MessageIds, Options } from '../../src/rules/keyword-spacing'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
const BOTH = { before: true, after: true }; | ||
const NEITHER = { before: false, after: false }; | ||
|
||
/** | ||
* Creates an option object to test an 'overrides' option. | ||
* | ||
* e.g. | ||
* | ||
* override('as', BOTH) | ||
* | ||
* returns | ||
* | ||
* { | ||
* before: false, | ||
* after: false, | ||
* overrides: {as: {before: true, after: true}} | ||
* } | ||
* @param keyword A keyword to be overridden. | ||
* @param value A value to override. | ||
* @returns An option object to test an 'overrides' option. | ||
*/ | ||
function overrides(keyword: string, value: Options[0]): Options[0] { | ||
return { | ||
before: value.before === false, | ||
after: value.after === false, | ||
overrides: { [keyword]: value }, | ||
}; | ||
} | ||
|
||
/** | ||
* Gets an error message that expected space(s) before a specified keyword. | ||
* @param keyword A keyword. | ||
* @returns An error message. | ||
*/ | ||
function expectedBefore(keyword: string): TSESLint.TestCaseError<MessageIds>[] { | ||
return [{ messageId: 'expectedBefore', data: { value: keyword } }]; | ||
} | ||
|
||
/** | ||
* Gets an error message that expected space(s) after a specified keyword. | ||
* @param keyword A keyword. | ||
* @returns An error message. | ||
*/ | ||
function expectedAfter(keyword: string): TSESLint.TestCaseError<MessageIds>[] { | ||
return [{ messageId: 'expectedAfter', data: { value: keyword } }]; | ||
} | ||
|
||
/** | ||
* Gets an error message that unexpected space(s) before a specified keyword. | ||
* @param keyword A keyword. | ||
* @returns An error message. | ||
*/ | ||
function unexpectedBefore( | ||
keyword: string, | ||
): TSESLint.TestCaseError<MessageIds>[] { | ||
return [{ messageId: 'unexpectedBefore', data: { value: keyword } }]; | ||
} | ||
|
||
/** | ||
* Gets an error message that unexpected space(s) after a specified keyword. | ||
* @param keyword A keyword. | ||
* @returns An error message. | ||
*/ | ||
function unexpectedAfter( | ||
keyword: string, | ||
): TSESLint.TestCaseError<MessageIds>[] { | ||
return [{ messageId: 'unexpectedAfter', data: { value: keyword } }]; | ||
} | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('keyword-spacing', rule, { | ||
valid: [ | ||
//---------------------------------------------------------------------- | ||
// as (typing) | ||
//---------------------------------------------------------------------- | ||
{ | ||
code: 'const foo = {} as {};', | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
}, | ||
{ | ||
code: 'const foo = {}as{};', | ||
options: [NEITHER], | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
}, | ||
{ | ||
code: 'const foo = {} as {};', | ||
options: [overrides('as', BOTH)], | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
}, | ||
{ | ||
code: 'const foo = {}as{};', | ||
options: [overrides('as', NEITHER)], | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
}, | ||
{ | ||
code: 'const foo = {} as {};', | ||
options: [{ overrides: { as: {} } }], | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
}, | ||
], | ||
invalid: [ | ||
//---------------------------------------------------------------------- | ||
// as (typing) | ||
//---------------------------------------------------------------------- | ||
{ | ||
code: 'const foo = {}as {};', | ||
output: 'const foo = {} as {};', | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
errors: expectedBefore('as'), | ||
}, | ||
{ | ||
code: 'const foo = {} as{};', | ||
output: 'const foo = {}as{};', | ||
options: [NEITHER], | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
errors: unexpectedBefore('as'), | ||
}, | ||
{ | ||
code: 'const foo = {} as{};', | ||
output: 'const foo = {} as {};', | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
errors: expectedAfter('as'), | ||
}, | ||
{ | ||
code: 'const foo = {}as {};', | ||
output: 'const foo = {}as{};', | ||
options: [NEITHER], | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
errors: unexpectedAfter('as'), | ||
}, | ||
{ | ||
code: 'const foo = {} as{};', | ||
options: [{ overrides: { as: {} } }], | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
errors: expectedAfter('as'), | ||
}, | ||
], | ||
}); |
This file contains 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