Skip to content

Commit 58aace6

Browse files
authored
fix(ts/padding-line-between-statements): support cjs-import and cjs-export statement type (#162)
1 parent 9996a21 commit 58aace6

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

packages/eslint-plugin-ts/rules/padding-line-between-statements/padding-line-between-statements.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5124,5 +5124,13 @@ declare namespace Types {
51245124
options: [{ blankLine: 'always', prev: '*', next: 'block-like' }],
51255125
errors: [{ messageId: 'expectedBlankLine' }],
51265126
},
5127+
5128+
// https://github.com/eslint-stylistic/eslint-stylistic/issues/53
5129+
{
5130+
code: `const path = require('node:path');\nmodule.exports = {};`,
5131+
output: `const path = require('node:path');\n\nmodule.exports = {};`,
5132+
options: [{ blankLine: 'always', prev: 'cjs-import', next: 'cjs-export' }],
5133+
errors: [{ messageId: 'expectedBlankLine' }],
5134+
},
51275135
],
51285136
})

packages/eslint-plugin-ts/rules/padding-line-between-statements/padding-line-between-statements.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {
1313
createRule,
1414
} from '../../utils'
1515

16+
const CJS_EXPORT = /^(?:module\s*\.\s*)?exports(?:\s*\.|\s*\[|$)/u
17+
const CJS_IMPORT = /^require\(/u
18+
1619
/**
1720
* This rule is a replica of padding-line-between-statements.
1821
*
@@ -570,6 +573,18 @@ const StatementTypes: Record<string, NodeTestObject> = {
570573
),
571574
'with': newKeywordTester(AST_NODE_TYPES.WithStatement, 'with'),
572575

576+
'cjs-export': {
577+
test: (node, sourceCode) => node.type === 'ExpressionStatement'
578+
&& node.expression.type === 'AssignmentExpression'
579+
&& CJS_EXPORT.test(sourceCode.getText(node.expression.left)),
580+
},
581+
'cjs-import': {
582+
test: (node, sourceCode) => node.type === 'VariableDeclaration'
583+
&& node.declarations.length > 0
584+
&& Boolean(node.declarations[0].init)
585+
&& CJS_IMPORT.test(sourceCode.getText(node.declarations[0].init!)),
586+
},
587+
573588
// Additional Typescript constructs
574589
'interface': newKeywordTester(
575590
AST_NODE_TYPES.TSInterfaceDeclaration,

packages/eslint-plugin-ts/rules/padding-line-between-statements/types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export type StatementType =
4141
| 'var'
4242
| 'while'
4343
| 'with'
44+
| 'cjs-export'
45+
| 'cjs-import'
4446
| 'interface'
4547
| 'type'
4648
)
@@ -84,6 +86,8 @@ export type StatementType =
8486
| 'var'
8587
| 'while'
8688
| 'with'
89+
| 'cjs-export'
90+
| 'cjs-import'
8791
| 'interface'
8892
| 'type'
8993
),
@@ -126,6 +130,8 @@ export type StatementType =
126130
| 'var'
127131
| 'while'
128132
| 'with'
133+
| 'cjs-export'
134+
| 'cjs-import'
129135
| 'interface'
130136
| 'type'
131137
)[],

0 commit comments

Comments
 (0)