-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): add extension rule space-before-blocks
(#1606)
#4184
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
JoshuaKGoldberg
merged 2 commits into
typescript-eslint:main
from
FDIM:feat/space-before-blocks
Feb 24, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,60 @@ | ||
# `space-before-blocks` | ||
|
||
Enforces consistent spacing before blocks. | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [`eslint/space-before-blocks`](https://eslint.org/docs/rules/space-before-blocks) rule. | ||
It adds support for interfaces and enums: | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
enum Breakpoint{ | ||
Large, Medium; | ||
} | ||
|
||
interface State{ | ||
currentBreakpoint: Breakpoint; | ||
} | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
enum Breakpoint { | ||
Large, Medium; | ||
} | ||
|
||
interface State { | ||
currentBreakpoint: Breakpoint; | ||
} | ||
``` | ||
|
||
In case a more specific options object is passed these blocks will follow `classes` configuration option. | ||
|
||
## How to Use | ||
|
||
```jsonc | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"space-before-blocks": "off", | ||
"@typescript-eslint/space-before-blocks": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [`eslint/space-before-blocks` options](https://eslint.org/docs/rules/space-before-blocks#options). | ||
|
||
<sup> | ||
|
||
Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/space-before-blocks.md) | ||
|
||
</sup> | ||
|
||
## Attributes | ||
|
||
- [ ] ✅ Recommended | ||
- [x] 🔧 Fixable | ||
- [ ] 💭 Requires type information |
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,90 @@ | ||
import { TSESTree } from '@typescript-eslint/utils'; | ||
import { getESLintCoreRule } from '../util/getESLintCoreRule'; | ||
import * as util from '../util'; | ||
|
||
const baseRule = getESLintCoreRule('space-before-blocks'); | ||
|
||
export type Options = util.InferOptionsTypeFromRule<typeof baseRule>; | ||
export type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'space-before-blocks', | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: 'Enforces consistent spacing before blocks', | ||
recommended: false, | ||
extendsBaseRule: true, | ||
}, | ||
fixable: baseRule.meta.fixable, | ||
hasSuggestions: baseRule.meta.hasSuggestions, | ||
schema: baseRule.meta.schema, | ||
messages: { | ||
// @ts-expect-error -- we report on this messageId so we need to ensure it's there in case ESLint changes in future | ||
unexpectedSpace: 'Unexpected space before opening brace.', | ||
// @ts-expect-error -- we report on this messageId so we need to ensure it's there in case ESLint changes in future | ||
missingSpace: 'Missing space before opening brace.', | ||
...baseRule.meta.messages, | ||
}, | ||
}, | ||
defaultOptions: ['always'], | ||
create(context) { | ||
const rules = baseRule.create(context); | ||
const config = context.options[0]; | ||
const sourceCode = context.getSourceCode(); | ||
|
||
let requireSpace = true; | ||
|
||
if (typeof config === 'object') { | ||
requireSpace = config.classes === 'always'; | ||
} else if (config === 'never') { | ||
requireSpace = false; | ||
} | ||
|
||
function checkPrecedingSpace( | ||
node: TSESTree.Token | TSESTree.TSInterfaceBody, | ||
): void { | ||
const precedingToken = sourceCode.getTokenBefore(node); | ||
if (precedingToken && util.isTokenOnSameLine(precedingToken, node)) { | ||
const hasSpace = sourceCode.isSpaceBetweenTokens( | ||
FDIM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
precedingToken, | ||
node as TSESTree.Token, | ||
); | ||
|
||
if (requireSpace && !hasSpace) { | ||
context.report({ | ||
node, | ||
messageId: 'missingSpace', | ||
fix(fixer) { | ||
return fixer.insertTextBefore(node, ' '); | ||
}, | ||
}); | ||
} else if (!requireSpace && hasSpace) { | ||
context.report({ | ||
node, | ||
messageId: 'unexpectedSpace', | ||
fix(fixer) { | ||
return fixer.removeRange([ | ||
precedingToken.range[1], | ||
node.range[0], | ||
]); | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
function checkSpaceAfterEnum(node: TSESTree.TSEnumDeclaration): void { | ||
const punctuator = sourceCode.getTokenAfter(node.id); | ||
if (punctuator) { | ||
checkPrecedingSpace(punctuator); | ||
} | ||
} | ||
|
||
return { | ||
...rules, | ||
TSEnumDeclaration: checkSpaceAfterEnum, | ||
TSInterfaceBody: checkPrecedingSpace, | ||
}; | ||
}, | ||
}); |
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
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.