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 comma-spacing (#1495)
- Loading branch information
1 parent
b5730e0
commit 1fd86be
Showing
7 changed files
with
1,004 additions
and
2 deletions.
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 @@ | ||
# Enforces consistent spacing before and after commas (`comma-spacing`) | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [`eslint/comma-spacing`](https://eslint.org/docs/rules/comma-spacing) rule. | ||
It adds support for trailing comma in a types parameters list. | ||
|
||
## How to use | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"comma-spacing": "off", | ||
"@typescript-eslint/comma-spacing": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [`eslint/comma-spacing` options](https://eslint.org/docs/rules/comma-spacing#options). | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/comma-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
import { | ||
TSESTree, | ||
AST_TOKEN_TYPES, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import { isClosingParenToken, isCommaToken } from 'eslint-utils'; | ||
import { isTokenOnSameLine, createRule } from '../util'; | ||
|
||
type Options = [ | ||
{ | ||
before: boolean; | ||
after: boolean; | ||
}, | ||
]; | ||
type MessageIds = 'unexpected' | 'missing'; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'comma-spacing', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Enforces consistent spacing before and after commas', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
extendsBaseRule: true, | ||
}, | ||
fixable: 'whitespace', | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
before: { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
after: { | ||
type: 'boolean', | ||
default: true, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
unexpected: `There should be no space {{loc}} ','.`, | ||
missing: `A space is required {{loc}} ','.`, | ||
}, | ||
}, | ||
defaultOptions: [ | ||
{ | ||
before: false, | ||
after: true, | ||
}, | ||
], | ||
create(context, [{ before: spaceBefore, after: spaceAfter }]) { | ||
const sourceCode = context.getSourceCode(); | ||
const tokensAndComments = sourceCode.tokensAndComments; | ||
const ignoredTokens = new Set<TSESTree.Token>(); | ||
|
||
/** | ||
* Adds null elements of the ArrayExpression or ArrayPattern node to the ignore list | ||
* @param node node to evaluate | ||
*/ | ||
function addNullElementsToIgnoreList( | ||
node: TSESTree.ArrayExpression | TSESTree.ArrayPattern, | ||
): void { | ||
let previousToken = sourceCode.getFirstToken(node); | ||
for (const element of node.elements) { | ||
let token: TSESTree.Token | null; | ||
if (element === null) { | ||
token = sourceCode.getTokenAfter(previousToken as TSESTree.Token); | ||
if (token && isCommaToken(token)) { | ||
ignoredTokens.add(token); | ||
} | ||
} else { | ||
token = sourceCode.getTokenAfter(element); | ||
} | ||
|
||
previousToken = token; | ||
} | ||
} | ||
|
||
/** | ||
* Adds type parameters trailing comma token to the ignore list | ||
* @param node node to evaluate | ||
*/ | ||
function addTypeParametersTrailingCommaToIgnoreList( | ||
node: TSESTree.TSTypeParameterDeclaration, | ||
): void { | ||
const param = node.params[node.params.length - 1]; | ||
const afterToken = sourceCode.getTokenAfter(param); | ||
if (afterToken && isCommaToken(afterToken)) { | ||
ignoredTokens.add(afterToken); | ||
} | ||
} | ||
|
||
/** | ||
* Validates the spacing around a comma token. | ||
* @param commaToken The token representing the comma | ||
* @param prevToken The last token before the comma | ||
* @param nextToken The first token after the comma | ||
*/ | ||
function validateCommaSpacing( | ||
commaToken: TSESTree.Token, | ||
prevToken: TSESTree.Token | null, | ||
nextToken: TSESTree.Token | null, | ||
): void { | ||
if ( | ||
prevToken && | ||
isTokenOnSameLine(prevToken, commaToken) && | ||
spaceBefore !== sourceCode.isSpaceBetween(prevToken, commaToken) | ||
) { | ||
context.report({ | ||
node: commaToken, | ||
data: { | ||
loc: 'before', | ||
}, | ||
messageId: spaceBefore ? 'missing' : 'unexpected', | ||
fix: fixer => | ||
spaceBefore | ||
? fixer.insertTextBefore(commaToken, ' ') | ||
: fixer.replaceTextRange( | ||
[prevToken.range[1], commaToken.range[0]], | ||
'', | ||
), | ||
}); | ||
} | ||
|
||
if (nextToken && isClosingParenToken(nextToken)) { | ||
return; | ||
} | ||
|
||
if (!spaceAfter && nextToken && nextToken.type === AST_TOKEN_TYPES.Line) { | ||
return; | ||
} | ||
|
||
if ( | ||
nextToken && | ||
isTokenOnSameLine(commaToken, nextToken) && | ||
spaceAfter !== sourceCode.isSpaceBetween(commaToken, nextToken) | ||
) { | ||
context.report({ | ||
node: commaToken, | ||
data: { | ||
loc: 'after', | ||
}, | ||
messageId: spaceAfter ? 'missing' : 'unexpected', | ||
fix: fixer => | ||
spaceAfter | ||
? fixer.insertTextAfter(commaToken, ' ') | ||
: fixer.replaceTextRange( | ||
[commaToken.range[1], nextToken.range[0]], | ||
'', | ||
), | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
TSTypeParameterDeclaration: addTypeParametersTrailingCommaToIgnoreList, | ||
ArrayExpression: addNullElementsToIgnoreList, | ||
ArrayPattern: addNullElementsToIgnoreList, | ||
|
||
'Program:exit'(): void { | ||
tokensAndComments.forEach((token, i) => { | ||
if (!isCommaToken(token)) { | ||
return; | ||
} | ||
|
||
if (token.type === AST_TOKEN_TYPES.JSXText) { | ||
return; | ||
} | ||
|
||
const commaToken = token as TSESTree.Token; | ||
const prevToken = tokensAndComments[i - 1] as TSESTree.Token; | ||
const nextToken = tokensAndComments[i + 1] as TSESTree.Token; | ||
|
||
validateCommaSpacing( | ||
commaToken, | ||
isCommaToken(prevToken) || ignoredTokens.has(commaToken) | ||
? null | ||
: prevToken, | ||
isCommaToken(nextToken) || ignoredTokens.has(commaToken) | ||
? null | ||
: nextToken, | ||
); | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
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
Oops, something went wrong.