Skip to content

Commit b239b71

Browse files
authored
refactor: merge block-spacing (#690)
1 parent 522e55f commit b239b71

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

‎packages/eslint-plugin/rules/block-spacing/block-spacing._ts_.ts‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import type { Tree } from '#types'
22
import type { MessageIds, RuleOptions } from './types'
3-
import { castRuleModule, createRule } from '#utils/create-rule'
3+
import { createRule } from '#utils/create-rule'
44
import { AST_TOKEN_TYPES } from '@typescript-eslint/utils'
55
import { isTokenOnSameLine } from '@typescript-eslint/utils/ast-utils'
6-
import _baseRule from './block-spacing._js_'
76

8-
const baseRule = /* @__PURE__ */ castRuleModule(_baseRule)
7+
type SupportedNodes = Tree.BlockStatement | Tree.StaticBlock | Tree.SwitchStatement | Tree.TSInterfaceBody | Tree.TSTypeLiteral | Tree.TSEnumDeclaration
98

109
export default createRule<RuleOptions, MessageIds>({
1110
name: 'block-spacing',
@@ -17,24 +16,25 @@ export default createRule<RuleOptions, MessageIds>({
1716
'Disallow or enforce spaces inside of blocks after opening block and before closing block',
1817
},
1918
fixable: 'whitespace',
20-
hasSuggestions: baseRule.meta.hasSuggestions,
21-
schema: baseRule.meta.schema,
22-
messages: baseRule.meta.messages,
19+
schema: [
20+
{ type: 'string', enum: ['always', 'never'] },
21+
],
22+
messages: {
23+
missing: 'Requires a space {{location}} \'{{token}}\'.',
24+
extra: 'Unexpected space(s) {{location}} \'{{token}}\'.',
25+
},
2326
},
2427
defaultOptions: ['always'],
2528

2629
create(context, [whenToApplyOption]) {
2730
const sourceCode = context.sourceCode
28-
const baseRules = baseRule.create(context)
2931
const always = whenToApplyOption !== 'never'
3032
const messageId = always ? 'missing' : 'extra'
3133
/**
3234
* Gets the open brace token from a given node.
3335
* @returns The token of the open brace.
3436
*/
35-
function getOpenBrace(
36-
node: Tree.TSEnumDeclaration,
37-
): Tree.PunctuatorToken {
37+
function getOpenBrace(node: SupportedNodes): Tree.PunctuatorToken {
3838
// guaranteed for enums
3939
// This is the only change made here from the base rule
4040
return sourceCode.getFirstToken(node, {
@@ -64,7 +64,7 @@ export default createRule<RuleOptions, MessageIds>({
6464
/**
6565
* Checks and reports invalid spacing style inside braces.
6666
*/
67-
function checkSpacingInsideBraces(node: Tree.TSEnumDeclaration): void {
67+
function checkSpacingInsideBraces(node: SupportedNodes): void {
6868
// Gets braces and the first/last token of content.
6969
const openBrace = getOpenBrace(node)
7070
const closeBrace = sourceCode.getLastToken(node)!
@@ -143,14 +143,16 @@ export default createRule<RuleOptions, MessageIds>({
143143
}
144144
}
145145
return {
146-
...baseRules,
146+
BlockStatement: checkSpacingInsideBraces,
147+
StaticBlock: checkSpacingInsideBraces,
148+
SwitchStatement: checkSpacingInsideBraces,
147149

148150
// This code worked "out of the box" for interface and type literal
149151
// Enums were very close to match as well, the only reason they are not is that was that enums don't have a body node in the parser
150152
// So the opening brace punctuator starts in the middle of the node - `getFirstToken` in
151153
// the base rule did not filter for the first opening brace punctuator
152-
TSInterfaceBody: baseRules.BlockStatement as never,
153-
TSTypeLiteral: baseRules.BlockStatement as never,
154+
TSInterfaceBody: checkSpacingInsideBraces,
155+
TSTypeLiteral: checkSpacingInsideBraces,
154156
TSEnumDeclaration: checkSpacingInsideBraces,
155157
}
156158
},

0 commit comments

Comments
 (0)