-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(initial): setup rules * chore(final-touches): nothing
- Loading branch information
Showing
21 changed files
with
1,665 additions
and
4 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
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,18 @@ | ||
import { Config, PaddingType, StatementType, createPaddingRule } from "../utils/padding" | ||
import { URL } from "node:url" | ||
|
||
export const RULE_NAME = new URL('', import.meta.url).pathname | ||
|
||
export const config: Config[] = [ | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.Any, | ||
nextStatementType: StatementType.AfterAllToken | ||
}, | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.AfterAllToken, | ||
nextStatementType: StatementType.Any | ||
} | ||
] | ||
export default createPaddingRule(RULE_NAME, 'Enforce padding around `afterAll` blocks', config) |
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,19 @@ | ||
import { Config, PaddingType, StatementType, createPaddingRule } from "../utils/padding"; | ||
import { get_filename } from "../utils/msc"; | ||
|
||
export const RULE_NAME = get_filename(import.meta.url) | ||
|
||
export const config: Config[] = [ | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.Any, | ||
nextStatementType: StatementType.AfterEachToken | ||
}, | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.AfterEachToken, | ||
nextStatementType: StatementType.Any | ||
} | ||
] | ||
|
||
export default createPaddingRule(RULE_NAME, 'Enforce padding around `afterEach` blocks', config) |
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,26 @@ | ||
|
||
import { config as paddingAroundAfterAllBlocksConfig } from './padding-around-after-all-blocks'; | ||
import { config as paddingAroundAfterEachBlocksConfig } from './padding-around-after-each-blocks'; | ||
import { config as paddingAroundBeforeAllBlocksConfig } from './padding-around-before-all-blocks'; | ||
import { config as paddingAroundBeforeEachBlocksConfig } from './padding-around-before-each-blocks'; | ||
import { config as paddingAroundDescribeBlocksConfig } from './padding-around-describe-blocks'; | ||
import { config as paddingAroundExpectGroupsConfig } from './padding-around-expect-groups'; | ||
import { config as paddingAroundTestBlocksConfig } from './padding-around-test-blocks'; | ||
import { createPaddingRule } from '../utils/padding'; | ||
import { get_filename } from '../utils/msc'; | ||
|
||
export const RULE_NAME = get_filename(import.meta.url) | ||
|
||
export default createPaddingRule( | ||
RULE_NAME, | ||
'Enforce padding around vitest functions', | ||
[ | ||
...paddingAroundAfterAllBlocksConfig, | ||
...paddingAroundAfterEachBlocksConfig, | ||
...paddingAroundBeforeAllBlocksConfig, | ||
...paddingAroundBeforeEachBlocksConfig, | ||
...paddingAroundDescribeBlocksConfig, | ||
...paddingAroundExpectGroupsConfig, | ||
...paddingAroundTestBlocksConfig, | ||
], | ||
); |
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,19 @@ | ||
import { get_filename } from "../utils/msc"; | ||
import { Config, PaddingType, StatementType, createPaddingRule } from "../utils/padding"; | ||
|
||
export const RULE_NAME = get_filename(import.meta.url) | ||
|
||
export const config: Config[] = [ | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.Any, | ||
nextStatementType: StatementType.BeforeAllToken | ||
}, | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.BeforeAllToken, | ||
nextStatementType: StatementType.Any | ||
} | ||
] | ||
|
||
export default createPaddingRule(RULE_NAME, 'Enforce padding around `beforeAll` blocks', config) |
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,24 @@ | ||
|
||
import { get_filename } from '../utils/msc'; | ||
import { PaddingType, StatementType, createPaddingRule } from '../utils/padding'; | ||
|
||
export const RULE_NAME = get_filename(import.meta.url) | ||
|
||
export const config = [ | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.Any, | ||
nextStatementType: StatementType.BeforeEachToken, | ||
}, | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.BeforeEachToken, | ||
nextStatementType: StatementType.Any, | ||
}, | ||
]; | ||
|
||
export default createPaddingRule( | ||
RULE_NAME, | ||
'Enforce padding around `beforeEach` blocks', | ||
config, | ||
); |
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,32 @@ | ||
|
||
import { get_filename } from '../utils/msc'; | ||
import { Config, PaddingType, StatementType, createPaddingRule } from '../utils/padding'; | ||
|
||
export const RULE_NAME = get_filename(import.meta.url) | ||
|
||
export const config: Config[] = [ | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.Any, | ||
nextStatementType: [ | ||
StatementType.DescribeToken, | ||
StatementType.FdescribeToken, | ||
StatementType.XdescribeToken, | ||
], | ||
}, | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: [ | ||
StatementType.DescribeToken, | ||
StatementType.FdescribeToken, | ||
StatementType.XdescribeToken, | ||
], | ||
nextStatementType: StatementType.Any, | ||
}, | ||
]; | ||
|
||
export default createPaddingRule( | ||
RULE_NAME, | ||
'Enforce padding around `describe` blocks', | ||
config, | ||
); |
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,28 @@ | ||
import { get_filename } from "../utils/msc"; | ||
import { Config, PaddingType, StatementType, createPaddingRule } from "../utils/padding"; | ||
|
||
export const RULE_NAME = get_filename(import.meta.url) | ||
|
||
export const config: Config[] = [ | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.Any, | ||
nextStatementType: StatementType.ExpectToken, | ||
}, | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.ExpectToken, | ||
nextStatementType: StatementType.Any, | ||
}, | ||
{ | ||
paddingType: PaddingType.Any, | ||
prevStatementType: StatementType.ExpectToken, | ||
nextStatementType: StatementType.ExpectToken, | ||
}, | ||
]; | ||
|
||
export default createPaddingRule( | ||
RULE_NAME, | ||
'Enforce padding around `expect` groups', | ||
config, | ||
); |
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,36 @@ | ||
|
||
import { get_filename } from '../utils/msc'; | ||
import { PaddingType, StatementType, createPaddingRule } from '../utils/padding'; | ||
|
||
export const RULE_NAME = get_filename(import.meta.url) | ||
|
||
export const config = [ | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: StatementType.Any, | ||
nextStatementType: [ | ||
StatementType.TestToken, | ||
StatementType.ItToken, | ||
StatementType.FitToken, | ||
StatementType.XitToken, | ||
StatementType.XtestToken, | ||
], | ||
}, | ||
{ | ||
paddingType: PaddingType.Always, | ||
prevStatementType: [ | ||
StatementType.TestToken, | ||
StatementType.ItToken, | ||
StatementType.FitToken, | ||
StatementType.XitToken, | ||
StatementType.XtestToken, | ||
], | ||
nextStatementType: StatementType.Any, | ||
}, | ||
]; | ||
|
||
export default createPaddingRule( | ||
RULE_NAME, | ||
'Enforce padding around afterAll blocks', | ||
config, | ||
); |
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,78 @@ | ||
import { AST_NODE_TYPES, AST_TOKEN_TYPES, TSESLint, TSESTree } from "@typescript-eslint/utils"; | ||
import { createRequire } from "node:module" | ||
|
||
const eslintRequire = createRequire(require.resolve("eslint")) | ||
|
||
export const espreeParser = eslintRequire.resolve('espree'); | ||
|
||
|
||
// We'll only verify nodes with these parent types | ||
const STATEMENT_LIST_PARENTS = new Set([ | ||
AST_NODE_TYPES.Program, | ||
AST_NODE_TYPES.BlockStatement, | ||
AST_NODE_TYPES.SwitchCase, | ||
AST_NODE_TYPES.SwitchStatement, | ||
]); | ||
|
||
export const isValidParent = (parentType: AST_NODE_TYPES): boolean => { | ||
return STATEMENT_LIST_PARENTS.has(parentType) | ||
} | ||
|
||
export const isTokenASemicolon = (token: TSESTree.Token): boolean => | ||
token.value === ';' && token.type === AST_TOKEN_TYPES.Punctuator | ||
|
||
|
||
/** | ||
* Gets the actual last token. | ||
* | ||
* If a semicolon is semicolon-less style's semicolon, this ignores it. | ||
* For example: | ||
* | ||
* foo() | ||
* ;[1, 2, 3].forEach(bar) | ||
*/ | ||
export const getActualLastToken = (sourceCode: TSESLint.SourceCode, node: TSESTree.Node): TSESTree.Token => { | ||
|
||
const semiToken = sourceCode.getLastToken(node)!; | ||
const prevToken = sourceCode.getTokenBefore(semiToken)!; | ||
const nextToken = sourceCode.getTokenAfter(semiToken) | ||
|
||
const isSemicolonLessStyle = Boolean( | ||
prevToken && | ||
nextToken && | ||
prevToken.range[0] >= node.range[0] && | ||
isTokenASemicolon(semiToken) && | ||
semiToken.loc.start.line !== prevToken.loc.end.line && | ||
semiToken.loc.end.line === nextToken.loc.start.line, | ||
); | ||
|
||
return isSemicolonLessStyle ? prevToken : semiToken; | ||
|
||
} | ||
|
||
export const getPaddingLineSequences = (prevNode: TSESTree.Node, nextNode: TSESTree.Node, sourceCode: TSESLint.SourceCode) => { | ||
const pairs: TSESTree.Token[][] = [] | ||
const includeComments = true | ||
|
||
let prevToken = getActualLastToken(sourceCode, prevNode) | ||
|
||
if ((nextNode.loc.start.line - prevNode.loc.end.line) >= 2) { | ||
do { | ||
const token = sourceCode.getTokenAfter(prevToken, { includeComments }) as TSESTree.Token | ||
|
||
if ((token.loc.start.line - prevToken.loc.end.line) >= 2) { | ||
pairs.push([prevToken, token]) | ||
} | ||
|
||
prevToken = token | ||
} while (prevToken.range[0] < nextNode.range[0]) | ||
} | ||
|
||
return pairs | ||
} | ||
|
||
|
||
export const areTokensOnSameLine = ( | ||
left: TSESTree.Node | TSESTree.Token, | ||
right: TSESTree.Node | TSESTree.Token, | ||
): boolean => left.loc.end.line === right.loc.start.line; |
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.