-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): add extension rule space-infix-ops
#2593
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
Merged
Merged
Changes from all commits
Commits
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,26 @@ | ||
# This rule is aimed at ensuring there are spaces around infix operators. (`space-infix-ops`) | ||
|
||
This rule extends the base [`eslint/space-infix-ops`](https://eslint.org/docs/rules/space-infix-ops) rule. | ||
|
||
It also add support for enum members | ||
|
||
```ts | ||
enum MyEnum { | ||
KEY = 'value', | ||
} | ||
``` | ||
|
||
## How to use | ||
|
||
```jsonc | ||
{ | ||
"space-infix-ops": "off", | ||
"@typescript-eslint/space-infix-ops": ["error", { "int32Hint": false }] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [`eslint/space-infix-ops` options](https://eslint.org/docs/rules/space-infix-ops#options). | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/space-infix-ops.md)</sup> |
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,102 @@ | ||
import { | ||
AST_TOKEN_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import baseRule from 'eslint/lib/rules/space-infix-ops'; | ||
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: 'space-infix-ops', | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: | ||
'This rule is aimed at ensuring there are spaces around infix operators.', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
extendsBaseRule: true, | ||
}, | ||
fixable: baseRule.meta.fixable, | ||
schema: baseRule.meta.schema, | ||
messages: baseRule.meta.messages ?? { | ||
missingSpace: "Operator '{{operator}}' must be spaced.", | ||
}, | ||
}, | ||
defaultOptions: [ | ||
{ | ||
int32Hint: false, | ||
}, | ||
], | ||
create(context) { | ||
const rules = baseRule.create(context); | ||
const sourceCode = context.getSourceCode(); | ||
|
||
/** | ||
* Check if it has an assignment char and report if it's faulty | ||
* @param node The node to report | ||
*/ | ||
function checkForAssignmentSpace(node: TSESTree.TSEnumMember): void { | ||
if (!node.initializer) { | ||
return; | ||
} | ||
|
||
const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!; | ||
const rightNode = sourceCode.getTokenByRangeStart( | ||
node.initializer.range[0], | ||
)!; | ||
|
||
if (!rightNode) { | ||
return; | ||
} | ||
|
||
const operator = sourceCode.getFirstTokenBetween( | ||
leftNode, | ||
rightNode, | ||
token => | ||
token.type === AST_TOKEN_TYPES.Punctuator && token.value === '=', | ||
); | ||
const prev = sourceCode.getTokenBefore(operator!); | ||
const next = sourceCode.getTokenAfter(operator!); | ||
|
||
if ( | ||
operator && | ||
(!sourceCode.isSpaceBetweenTokens(prev!, operator) || | ||
!sourceCode.isSpaceBetweenTokens(operator, next!)) | ||
) { | ||
context.report({ | ||
node: node, | ||
loc: operator.loc, | ||
messageId: 'missingSpace', | ||
data: { | ||
operator: operator.value, | ||
}, | ||
fix(fixer) { | ||
const previousToken = sourceCode.getTokenBefore(operator); | ||
const afterToken = sourceCode.getTokenAfter(operator); | ||
let fixString = ''; | ||
|
||
if (operator.range[0] - previousToken!.range[1] === 0) { | ||
fixString = ' '; | ||
} | ||
|
||
fixString += operator.value; | ||
|
||
if (afterToken!.range[0] - operator.range[1] === 0) { | ||
fixString += ' '; | ||
} | ||
|
||
return fixer.replaceText(operator, fixString); | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
...rules, | ||
TSEnumMember: checkForAssignmentSpace, | ||
}; | ||
}, | ||
}); |
102 changes: 102 additions & 0 deletions
102
packages/eslint-plugin/tests/rules/space-infix-ops.test.ts
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,102 @@ | ||
/* eslint-disable eslint-comments/no-use */ | ||
// this rule tests 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 rule from '../../src/rules/space-infix-ops'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('space-infix-ops', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
enum Test { | ||
KEY1 = 2, | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
enum Test { | ||
KEY1 = "value", | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
enum Test { | ||
KEY1, | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
enum Test { | ||
A= 2, | ||
B = 1, | ||
} | ||
`, | ||
output: ` | ||
enum Test { | ||
A = 2, | ||
B = 1, | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missingSpace', | ||
column: 12, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
enum Test { | ||
KEY1= "value1", | ||
KEY2 = "value2", | ||
} | ||
`, | ||
output: ` | ||
enum Test { | ||
KEY1 = "value1", | ||
KEY2 = "value2", | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missingSpace', | ||
column: 15, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
enum Test { | ||
A =2, | ||
B = 1, | ||
} | ||
`, | ||
output: ` | ||
enum Test { | ||
A = 2, | ||
B = 1, | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'missingSpace', | ||
column: 13, | ||
line: 3, | ||
}, | ||
], | ||
}, | ||
], | ||
bradzacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); |
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
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.