Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…#4184) Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>
- Loading branch information
1 parent
66501d6
commit 208b6d0
Showing
9 changed files
with
439 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,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 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,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( | ||
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 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.