@k03mad/oxlint-config 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +12 -0
- package/.github/dependabot.yml +14 -0
- package/.github/workflows/lint.yml +20 -0
- package/.github/workflows/publish.yml +35 -0
- package/.husky/pre-commit +1 -0
- package/.nvmrc +1 -0
- package/.oxfmtrc.json +18 -0
- package/.oxlintrc.json +125 -0
- package/.vscode/settings.json +9 -0
- package/LICENSE +21 -0
- package/README.md +28 -0
- package/package.json +24 -0
- package/plugin/plugin.js +10 -0
- package/plugin/rules/padding-line-between-statements.js +532 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: Lint
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- master
|
|
7
|
+
pull_request:
|
|
8
|
+
branches:
|
|
9
|
+
- master
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
lint:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v6
|
|
16
|
+
- uses: actions/setup-node@v6
|
|
17
|
+
with:
|
|
18
|
+
node-version-file: '.nvmrc'
|
|
19
|
+
- run: npm i
|
|
20
|
+
- run: npm run lint
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- master
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
id-token: write
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
publish:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v6
|
|
17
|
+
with:
|
|
18
|
+
fetch-depth: 0
|
|
19
|
+
|
|
20
|
+
- uses: EndBug/version-check@v2
|
|
21
|
+
id: check_pkg_version
|
|
22
|
+
with:
|
|
23
|
+
diff-search: true
|
|
24
|
+
|
|
25
|
+
- if: steps.check_pkg_version.outputs.changed == 'true'
|
|
26
|
+
uses: actions/setup-node@v6
|
|
27
|
+
with:
|
|
28
|
+
node-version-file: '.nvmrc'
|
|
29
|
+
registry-url: 'https://registry.npmjs.org'
|
|
30
|
+
|
|
31
|
+
- if: steps.check_pkg_version.outputs.changed == 'true'
|
|
32
|
+
run: npm i
|
|
33
|
+
|
|
34
|
+
- if: steps.check_pkg_version.outputs.changed == 'true'
|
|
35
|
+
run: npm publish
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
./node_modules/.bin/run-p -c lint test
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
24
|
package/.oxfmtrc.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./node_modules/oxfmt/configuration_schema.json",
|
|
3
|
+
"ignorePatterns": [".allure/**", ".bin/**", "allure/**", "artifacts/**", "node_modules/**"],
|
|
4
|
+
"singleQuote": true,
|
|
5
|
+
"arrowParens": "avoid",
|
|
6
|
+
"bracketSpacing": false,
|
|
7
|
+
"quoteProps": "consistent",
|
|
8
|
+
"experimentalSortImports": {
|
|
9
|
+
"groups": [
|
|
10
|
+
["builtin"],
|
|
11
|
+
["external", "type-external"],
|
|
12
|
+
["internal", "type-internal"],
|
|
13
|
+
["parent", "type-parent"],
|
|
14
|
+
["sibling", "type-sibling"],
|
|
15
|
+
["index", "type-index"]
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
}
|
package/.oxlintrc.json
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./node_modules/oxlint/configuration_schema.json",
|
|
3
|
+
"ignorePatterns": [".allure/**", ".bin/**", "allure/**", "artifacts/**", "node_modules/**"],
|
|
4
|
+
"plugins": ["typescript", "eslint", "import", "jsdoc", "node", "oxc", "promise", "unicorn"],
|
|
5
|
+
"jsPlugins": ["./plugin/plugin.js"],
|
|
6
|
+
"categories": {
|
|
7
|
+
"correctness": "error",
|
|
8
|
+
"suspicious": "error",
|
|
9
|
+
"pedantic": "error",
|
|
10
|
+
"perf": "error",
|
|
11
|
+
"style": "error",
|
|
12
|
+
"restriction": "error",
|
|
13
|
+
"nursery": "error"
|
|
14
|
+
},
|
|
15
|
+
"rules": {
|
|
16
|
+
"import/extensions": [
|
|
17
|
+
"error",
|
|
18
|
+
"always",
|
|
19
|
+
{
|
|
20
|
+
"ignorePackages": true
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
"func-names": ["error", "never"],
|
|
24
|
+
"new-cap": [
|
|
25
|
+
"error",
|
|
26
|
+
{
|
|
27
|
+
"properties": false
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
"no-empty": [
|
|
31
|
+
"error",
|
|
32
|
+
{
|
|
33
|
+
"allowEmptyCatch": true
|
|
34
|
+
}
|
|
35
|
+
],
|
|
36
|
+
"prefer-destructuring": [
|
|
37
|
+
"error",
|
|
38
|
+
{
|
|
39
|
+
"array": false,
|
|
40
|
+
"object": true
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
"radix": ["error", "as-needed"],
|
|
44
|
+
"unicorn/catch-error-name": [
|
|
45
|
+
"error",
|
|
46
|
+
{
|
|
47
|
+
"name": "err"
|
|
48
|
+
}
|
|
49
|
+
],
|
|
50
|
+
|
|
51
|
+
"custom/padding-line-between-statements": [
|
|
52
|
+
"error",
|
|
53
|
+
{
|
|
54
|
+
"blankLine": "always",
|
|
55
|
+
"next": "multiline-block-like",
|
|
56
|
+
"prev": "*"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"blankLine": "always",
|
|
60
|
+
"next": "*",
|
|
61
|
+
"prev": "multiline-block-like"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
"blankLine": "always",
|
|
65
|
+
"next": "multiline-const",
|
|
66
|
+
"prev": "*"
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"blankLine": "always",
|
|
70
|
+
"next": "*",
|
|
71
|
+
"prev": "multiline-const"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"blankLine": "always",
|
|
75
|
+
"next": "multiline-expression",
|
|
76
|
+
"prev": "*"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"blankLine": "always",
|
|
80
|
+
"next": "*",
|
|
81
|
+
"prev": "multiline-expression"
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
"blankLine": "always",
|
|
85
|
+
"next": "multiline-let",
|
|
86
|
+
"prev": "*"
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"blankLine": "always",
|
|
90
|
+
"next": "*",
|
|
91
|
+
"prev": "multiline-let"
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
"blankLine": "always",
|
|
95
|
+
"next": "*",
|
|
96
|
+
"prev": "import"
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
"blankLine": "any",
|
|
100
|
+
"next": "import",
|
|
101
|
+
"prev": "import"
|
|
102
|
+
}
|
|
103
|
+
],
|
|
104
|
+
|
|
105
|
+
// OFF
|
|
106
|
+
|
|
107
|
+
"oxc/no-rest-spread-properties": "off",
|
|
108
|
+
"oxc/no-optional-chaining": "off",
|
|
109
|
+
|
|
110
|
+
"import/no-anonymous-default-export": "off",
|
|
111
|
+
"import/no-default-export": "off",
|
|
112
|
+
|
|
113
|
+
"unicorn/no-array-reduce": "off",
|
|
114
|
+
"unicorn/no-null": "off",
|
|
115
|
+
|
|
116
|
+
"max-lines": "off",
|
|
117
|
+
"no-plusplus": "off",
|
|
118
|
+
"max-lines-per-function": "off",
|
|
119
|
+
"max-params": "off",
|
|
120
|
+
"sort-keys": "off",
|
|
121
|
+
"no-magic-numbers": "off",
|
|
122
|
+
"id-length": "off",
|
|
123
|
+
"no-ternary": "off"
|
|
124
|
+
}
|
|
125
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Kirill Molchanov aka k03mad
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Install
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
npm i --save-dev --save-exact eslint @k03mad/eslint-config
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
## Use
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
// eslint.config.js
|
|
11
|
+
|
|
12
|
+
export {default} from '@k03mad/eslint-config';
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
// package.json
|
|
17
|
+
{
|
|
18
|
+
"scripts": {
|
|
19
|
+
"lint": "npm run lint:eslint",
|
|
20
|
+
"lint:eslint": "eslint ./ --cache"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```json
|
|
26
|
+
// gitignore
|
|
27
|
+
.eslintcache
|
|
28
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@k03mad/oxlint-config",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "My Oxlint config",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"maintainers": [
|
|
7
|
+
"Kirill Molchanov <k03.mad@gmail.com>"
|
|
8
|
+
],
|
|
9
|
+
"repository": {
|
|
10
|
+
"url": "git+https://github.com/k03mad/oxlint-config.git"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"lint": "oxlint --report-unused-disable-directives && oxfmt --check",
|
|
15
|
+
"prepare": "husky || true"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"oxfmt": "0.34.0",
|
|
19
|
+
"oxlint": "1.49.0"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=24"
|
|
23
|
+
}
|
|
24
|
+
}
|
package/plugin/plugin.js
ADDED
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
// oxlint-disable no-shadow
|
|
2
|
+
const isNotSemicolonToken = token => token.value !== ';' && token.type === 'Punctuator';
|
|
3
|
+
const isClosingBraceToken = token => token.value === '}' && token.type === 'Punctuator';
|
|
4
|
+
|
|
5
|
+
const isParenthesized = (node, sourceCode) => {
|
|
6
|
+
const previousToken = sourceCode.getTokenBefore(node);
|
|
7
|
+
const nextToken = sourceCode.getTokenAfter(node);
|
|
8
|
+
return (
|
|
9
|
+
Boolean(previousToken && nextToken) &&
|
|
10
|
+
previousToken.value === '(' &&
|
|
11
|
+
previousToken.range[1] <= node.range[0] &&
|
|
12
|
+
nextToken.value === ')' &&
|
|
13
|
+
nextToken.range[0] >= node.range[1]
|
|
14
|
+
);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const isSemicolonToken = token => token.value === ';' && token.type === 'Punctuator';
|
|
18
|
+
|
|
19
|
+
const isFunction = node =>
|
|
20
|
+
node.type === 'FunctionDeclaration' ||
|
|
21
|
+
node.type === 'FunctionExpression' ||
|
|
22
|
+
node.type === 'ArrowFunctionExpression';
|
|
23
|
+
|
|
24
|
+
const isTokenOnSameLine = (left, right) => left.loc.end.line === right.loc.start.line;
|
|
25
|
+
const LINEBREAKS = new Set(['\r\n', '\r', '\n', '\u2028', '\u2029']);
|
|
26
|
+
|
|
27
|
+
const T = {
|
|
28
|
+
BlockStatement: 'BlockStatement',
|
|
29
|
+
BreakStatement: 'BreakStatement',
|
|
30
|
+
CallExpression: 'CallExpression',
|
|
31
|
+
ClassDeclaration: 'ClassDeclaration',
|
|
32
|
+
ContinueStatement: 'ContinueStatement',
|
|
33
|
+
DebuggerStatement: 'DebuggerStatement',
|
|
34
|
+
DoWhileStatement: 'DoWhileStatement',
|
|
35
|
+
EmptyStatement: 'EmptyStatement',
|
|
36
|
+
ExportAllDeclaration: 'ExportAllDeclaration',
|
|
37
|
+
ExportDefaultDeclaration: 'ExportDefaultDeclaration',
|
|
38
|
+
ExportNamedDeclaration: 'ExportNamedDeclaration',
|
|
39
|
+
ExpressionStatement: 'ExpressionStatement',
|
|
40
|
+
ForInStatement: 'ForInStatement',
|
|
41
|
+
ForOfStatement: 'ForOfStatement',
|
|
42
|
+
ForStatement: 'ForStatement',
|
|
43
|
+
FunctionDeclaration: 'FunctionDeclaration',
|
|
44
|
+
IfStatement: 'IfStatement',
|
|
45
|
+
ImportDeclaration: 'ImportDeclaration',
|
|
46
|
+
LabeledStatement: 'LabeledStatement',
|
|
47
|
+
Literal: 'Literal',
|
|
48
|
+
Program: 'Program',
|
|
49
|
+
ReturnStatement: 'ReturnStatement',
|
|
50
|
+
SequenceExpression: 'SequenceExpression',
|
|
51
|
+
StaticBlock: 'StaticBlock',
|
|
52
|
+
SwitchCase: 'SwitchCase',
|
|
53
|
+
SwitchStatement: 'SwitchStatement',
|
|
54
|
+
TSDeclareFunction: 'TSDeclareFunction',
|
|
55
|
+
TSEnumDeclaration: 'TSEnumDeclaration',
|
|
56
|
+
TSInterfaceBody: 'TSInterfaceBody',
|
|
57
|
+
TSInterfaceDeclaration: 'TSInterfaceDeclaration',
|
|
58
|
+
TSMethodSignature: 'TSMethodSignature',
|
|
59
|
+
TSModuleBlock: 'TSModuleBlock',
|
|
60
|
+
TSTypeAliasDeclaration: 'TSTypeAliasDeclaration',
|
|
61
|
+
TSTypeLiteral: 'TSTypeLiteral',
|
|
62
|
+
ThrowStatement: 'ThrowStatement',
|
|
63
|
+
TryStatement: 'TryStatement',
|
|
64
|
+
UnaryExpression: 'UnaryExpression',
|
|
65
|
+
VariableDeclaration: 'VariableDeclaration',
|
|
66
|
+
WhileStatement: 'WhileStatement',
|
|
67
|
+
WithStatement: 'WithStatement',
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const skipChainExpression = node =>
|
|
71
|
+
node && node.type === 'ChainExpression' ? node.expression : node;
|
|
72
|
+
|
|
73
|
+
const isTopLevelExpressionStatement = node =>
|
|
74
|
+
node.type === 'ExpressionStatement' &&
|
|
75
|
+
(node.parent.type === 'Program' ||
|
|
76
|
+
(node.parent.type === 'BlockStatement' && isFunction(node.parent.parent)));
|
|
77
|
+
|
|
78
|
+
const isSingleLine = node => node.loc.start.line === node.loc.end.line;
|
|
79
|
+
const isObjectNotArray = obj => typeof obj === 'object' && obj !== null && !Array.isArray(obj);
|
|
80
|
+
|
|
81
|
+
const deepMerge = (first = {}, second = {}) => {
|
|
82
|
+
const keys = new Set([...Object.keys(first), ...Object.keys(second)]);
|
|
83
|
+
return [...keys].reduce((acc, key) => {
|
|
84
|
+
const firstHasKey = key in first;
|
|
85
|
+
const secondHasKey = key in second;
|
|
86
|
+
const firstValue = first[key];
|
|
87
|
+
const secondValue = second[key];
|
|
88
|
+
|
|
89
|
+
if (firstHasKey && secondHasKey) {
|
|
90
|
+
acc[key] =
|
|
91
|
+
isObjectNotArray(firstValue) && isObjectNotArray(secondValue)
|
|
92
|
+
? deepMerge(firstValue, secondValue)
|
|
93
|
+
: secondValue;
|
|
94
|
+
} else if (firstHasKey) {
|
|
95
|
+
acc[key] = firstValue;
|
|
96
|
+
} else {
|
|
97
|
+
acc[key] = secondValue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return acc;
|
|
101
|
+
}, {});
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const createRule = ({name, create, meta}) => ({
|
|
105
|
+
create: context => {
|
|
106
|
+
const {defaultOptions = []} = meta;
|
|
107
|
+
const optionsCount = Math.max(context.options.length, defaultOptions.length);
|
|
108
|
+
return create(
|
|
109
|
+
context,
|
|
110
|
+
Array.from({length: optionsCount}, (_, i) =>
|
|
111
|
+
isObjectNotArray(context.options[i]) && isObjectNotArray(defaultOptions[i])
|
|
112
|
+
? deepMerge(defaultOptions[i], context.options[i])
|
|
113
|
+
: (context.options[i] ?? defaultOptions[i]),
|
|
114
|
+
),
|
|
115
|
+
);
|
|
116
|
+
},
|
|
117
|
+
meta: {...meta, docs: {...meta.docs, url: `https://eslint.style/rules/${name}`}},
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const LT = `[${[...LINEBREAKS].join('')}]`;
|
|
121
|
+
const PADDING_LINE_SEQUENCE = new RegExp(String.raw`^(\s*?${LT})\s*${LT}(\s*;?)$`, 'u');
|
|
122
|
+
|
|
123
|
+
const newKeywordTester = (type, keyword) => ({
|
|
124
|
+
test(node, sourceCode) {
|
|
125
|
+
const isSameKeyword = sourceCode.getFirstToken(node)?.value === keyword;
|
|
126
|
+
const isSameType = Array.isArray(type) ? type.includes(node.type) : type === node.type;
|
|
127
|
+
return isSameKeyword && isSameType;
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const isIIFEStatement = node => {
|
|
132
|
+
if (node.type === T.ExpressionStatement) {
|
|
133
|
+
let expression = skipChainExpression(node.expression);
|
|
134
|
+
|
|
135
|
+
if (expression.type === T.UnaryExpression) {
|
|
136
|
+
expression = skipChainExpression(expression.argument);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (expression.type === T.CallExpression) {
|
|
140
|
+
let node = expression.callee;
|
|
141
|
+
|
|
142
|
+
while (node.type === T.SequenceExpression) {
|
|
143
|
+
node = node.expressions.at(-1);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return isFunction(node);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return false;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const isBlockLikeStatement = (node, sourceCode) => {
|
|
154
|
+
if (node.type === T.DoWhileStatement && node.body.type === T.BlockStatement) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (isIIFEStatement(node)) {
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const lastToken = sourceCode.getLastToken(node, isNotSemicolonToken);
|
|
163
|
+
|
|
164
|
+
const belongingNode =
|
|
165
|
+
lastToken && isClosingBraceToken(lastToken)
|
|
166
|
+
? sourceCode.getNodeByRangeIndex(lastToken.range[0])
|
|
167
|
+
: null;
|
|
168
|
+
|
|
169
|
+
return (
|
|
170
|
+
Boolean(belongingNode) &&
|
|
171
|
+
(belongingNode.type === T.BlockStatement || belongingNode.type === T.SwitchStatement)
|
|
172
|
+
);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const isDirective = (node, sourceCode) =>
|
|
176
|
+
isTopLevelExpressionStatement(node) &&
|
|
177
|
+
node.expression.type === T.Literal &&
|
|
178
|
+
typeof node.expression.value === 'string' &&
|
|
179
|
+
!isParenthesized(node.expression, sourceCode);
|
|
180
|
+
|
|
181
|
+
const isDirectivePrologue = (node, sourceCode) => {
|
|
182
|
+
if (
|
|
183
|
+
isDirective(node, sourceCode) &&
|
|
184
|
+
node.parent &&
|
|
185
|
+
'body' in node.parent &&
|
|
186
|
+
Array.isArray(node.parent.body)
|
|
187
|
+
) {
|
|
188
|
+
for (const sibling of node.parent.body) {
|
|
189
|
+
if (sibling === node) {
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (!isDirective(sibling, sourceCode)) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return false;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const isExpression = (node, sourceCode) =>
|
|
205
|
+
node.type === T.ExpressionStatement && !isDirectivePrologue(node, sourceCode);
|
|
206
|
+
|
|
207
|
+
const getActualLastToken = (node, sourceCode) => {
|
|
208
|
+
const semiToken = sourceCode.getLastToken(node);
|
|
209
|
+
const prevToken = sourceCode.getTokenBefore(semiToken);
|
|
210
|
+
const nextToken = sourceCode.getTokenAfter(semiToken);
|
|
211
|
+
return prevToken &&
|
|
212
|
+
nextToken &&
|
|
213
|
+
prevToken.range[0] >= node.range[0] &&
|
|
214
|
+
isSemicolonToken(semiToken) &&
|
|
215
|
+
!isTokenOnSameLine(prevToken, semiToken) &&
|
|
216
|
+
isTokenOnSameLine(semiToken, nextToken)
|
|
217
|
+
? prevToken
|
|
218
|
+
: semiToken;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const getReportLoc = (node, sourceCode) => {
|
|
222
|
+
if (isSingleLine(node)) {
|
|
223
|
+
return node.loc;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const {line} = node.loc.start;
|
|
227
|
+
return {start: node.loc.start, end: {line, column: sourceCode.lines[line - 1].length}};
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// oxlint-disable-next-line no-empty-function
|
|
231
|
+
const verifyForAny = () => {};
|
|
232
|
+
|
|
233
|
+
const verifyForNever = (context, _, nextNode, paddingLines) => {
|
|
234
|
+
if (paddingLines.length === 0) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
context.report({
|
|
239
|
+
node: nextNode,
|
|
240
|
+
messageId: 'unexpectedBlankLine',
|
|
241
|
+
loc: getReportLoc(nextNode, context.sourceCode),
|
|
242
|
+
fix(fixer) {
|
|
243
|
+
if (paddingLines.length >= 2) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const prevToken = paddingLines[0][0];
|
|
248
|
+
const nextToken = paddingLines[0][1];
|
|
249
|
+
const start = prevToken.range[1];
|
|
250
|
+
const end = nextToken.range[0];
|
|
251
|
+
|
|
252
|
+
const text = context.sourceCode.text
|
|
253
|
+
.slice(start, end)
|
|
254
|
+
.replace(
|
|
255
|
+
PADDING_LINE_SEQUENCE,
|
|
256
|
+
(_, trailingSpaces, indentSpaces) => trailingSpaces + indentSpaces,
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
return fixer.replaceTextRange([start, end], text);
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const verifyForAlways = (context, prevNode, nextNode, paddingLines) => {
|
|
265
|
+
if (paddingLines.length > 0) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
context.report({
|
|
270
|
+
node: nextNode,
|
|
271
|
+
messageId: 'expectedBlankLine',
|
|
272
|
+
loc: getReportLoc(nextNode, context.sourceCode),
|
|
273
|
+
fix(fixer) {
|
|
274
|
+
const {sourceCode} = context;
|
|
275
|
+
let prevToken = getActualLastToken(prevNode, sourceCode);
|
|
276
|
+
|
|
277
|
+
const nextToken =
|
|
278
|
+
sourceCode.getFirstTokenBetween(prevToken, nextNode, {
|
|
279
|
+
includeComments: true,
|
|
280
|
+
filter(token) {
|
|
281
|
+
if (isTokenOnSameLine(prevToken, token)) {
|
|
282
|
+
prevToken = token;
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return true;
|
|
287
|
+
},
|
|
288
|
+
}) || nextNode;
|
|
289
|
+
|
|
290
|
+
const insertText = isTokenOnSameLine(prevToken, nextToken) ? '\n\n' : '\n';
|
|
291
|
+
return fixer.insertTextAfter(prevToken, insertText);
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const PaddingTypes = {
|
|
297
|
+
any: {verify: verifyForAny},
|
|
298
|
+
never: {verify: verifyForNever},
|
|
299
|
+
always: {verify: verifyForAlways},
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const MaybeMultilineStatementType = {
|
|
303
|
+
'block-like': {test: isBlockLikeStatement},
|
|
304
|
+
'expression': {test: isExpression},
|
|
305
|
+
'return': newKeywordTester(T.ReturnStatement, 'return'),
|
|
306
|
+
'export': newKeywordTester(
|
|
307
|
+
[T.ExportAllDeclaration, T.ExportDefaultDeclaration, T.ExportNamedDeclaration],
|
|
308
|
+
'export',
|
|
309
|
+
),
|
|
310
|
+
'var': newKeywordTester(T.VariableDeclaration, 'var'),
|
|
311
|
+
'let': newKeywordTester(T.VariableDeclaration, 'let'),
|
|
312
|
+
'const': newKeywordTester(T.VariableDeclaration, 'const'),
|
|
313
|
+
'using': {
|
|
314
|
+
test: node =>
|
|
315
|
+
node.type === 'VariableDeclaration' &&
|
|
316
|
+
(node.kind === 'using' || node.kind === 'await using'),
|
|
317
|
+
},
|
|
318
|
+
'type': newKeywordTester(T.TSTypeAliasDeclaration, 'type'),
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
const StatementTypes = {
|
|
322
|
+
'*': {test: () => true},
|
|
323
|
+
'directive': {test: isDirectivePrologue},
|
|
324
|
+
'iife': {test: isIIFEStatement},
|
|
325
|
+
'block': {test: node => node.type === T.BlockStatement},
|
|
326
|
+
'empty': {test: node => node.type === T.EmptyStatement},
|
|
327
|
+
'function': {test: node => node.type === T.FunctionDeclaration},
|
|
328
|
+
'ts-method': {test: node => node.type === T.TSMethodSignature},
|
|
329
|
+
'break': newKeywordTester(T.BreakStatement, 'break'),
|
|
330
|
+
'case': newKeywordTester(T.SwitchCase, 'case'),
|
|
331
|
+
'class': newKeywordTester(T.ClassDeclaration, 'class'),
|
|
332
|
+
'continue': newKeywordTester(T.ContinueStatement, 'continue'),
|
|
333
|
+
'debugger': newKeywordTester(T.DebuggerStatement, 'debugger'),
|
|
334
|
+
'default': newKeywordTester([T.SwitchCase, T.ExportDefaultDeclaration], 'default'),
|
|
335
|
+
'do': newKeywordTester(T.DoWhileStatement, 'do'),
|
|
336
|
+
'for': newKeywordTester([T.ForStatement, T.ForInStatement, T.ForOfStatement], 'for'),
|
|
337
|
+
'if': newKeywordTester(T.IfStatement, 'if'),
|
|
338
|
+
'import': newKeywordTester(T.ImportDeclaration, 'import'),
|
|
339
|
+
'switch': newKeywordTester(T.SwitchStatement, 'switch'),
|
|
340
|
+
'throw': newKeywordTester(T.ThrowStatement, 'throw'),
|
|
341
|
+
'try': newKeywordTester(T.TryStatement, 'try'),
|
|
342
|
+
'while': newKeywordTester([T.WhileStatement, T.DoWhileStatement], 'while'),
|
|
343
|
+
'with': newKeywordTester(T.WithStatement, 'with'),
|
|
344
|
+
'enum': newKeywordTester(T.TSEnumDeclaration, 'enum'),
|
|
345
|
+
'interface': newKeywordTester(T.TSInterfaceDeclaration, 'interface'),
|
|
346
|
+
'function-overload': {test: node => node.type === T.TSDeclareFunction},
|
|
347
|
+
...Object.fromEntries(
|
|
348
|
+
Object.entries(MaybeMultilineStatementType).flatMap(([key, value]) => [
|
|
349
|
+
[key, value],
|
|
350
|
+
[
|
|
351
|
+
`singleline-${key}`,
|
|
352
|
+
{
|
|
353
|
+
...value,
|
|
354
|
+
test: (node, sourceCode) => value.test(node, sourceCode) && isSingleLine(node),
|
|
355
|
+
},
|
|
356
|
+
],
|
|
357
|
+
[
|
|
358
|
+
`multiline-${key}`,
|
|
359
|
+
{
|
|
360
|
+
...value,
|
|
361
|
+
test: (node, sourceCode) => value.test(node, sourceCode) && !isSingleLine(node),
|
|
362
|
+
},
|
|
363
|
+
],
|
|
364
|
+
]),
|
|
365
|
+
),
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
export default createRule({
|
|
369
|
+
name: 'padding-line-between-statements',
|
|
370
|
+
meta: {
|
|
371
|
+
type: 'layout',
|
|
372
|
+
docs: {description: 'Require or disallow padding lines between statements'},
|
|
373
|
+
fixable: 'whitespace',
|
|
374
|
+
hasSuggestions: false,
|
|
375
|
+
schema: {
|
|
376
|
+
$defs: {
|
|
377
|
+
paddingType: {type: 'string', enum: Object.keys(PaddingTypes)},
|
|
378
|
+
statementType: {type: 'string', enum: Object.keys(StatementTypes)},
|
|
379
|
+
statementOption: {
|
|
380
|
+
anyOf: [
|
|
381
|
+
{$ref: '#/$defs/statementType'},
|
|
382
|
+
{
|
|
383
|
+
type: 'array',
|
|
384
|
+
items: {$ref: '#/$defs/statementType'},
|
|
385
|
+
minItems: 1,
|
|
386
|
+
uniqueItems: true,
|
|
387
|
+
additionalItems: false,
|
|
388
|
+
},
|
|
389
|
+
],
|
|
390
|
+
},
|
|
391
|
+
},
|
|
392
|
+
type: 'array',
|
|
393
|
+
additionalItems: false,
|
|
394
|
+
items: {
|
|
395
|
+
type: 'object',
|
|
396
|
+
properties: {
|
|
397
|
+
blankLine: {$ref: '#/$defs/paddingType'},
|
|
398
|
+
prev: {$ref: '#/$defs/statementOption'},
|
|
399
|
+
next: {$ref: '#/$defs/statementOption'},
|
|
400
|
+
},
|
|
401
|
+
additionalProperties: false,
|
|
402
|
+
required: ['blankLine', 'prev', 'next'],
|
|
403
|
+
},
|
|
404
|
+
},
|
|
405
|
+
defaultOptions: [],
|
|
406
|
+
messages: {
|
|
407
|
+
unexpectedBlankLine: 'Unexpected blank line before this statement.',
|
|
408
|
+
expectedBlankLine: 'Expected blank line before this statement.',
|
|
409
|
+
},
|
|
410
|
+
},
|
|
411
|
+
create(context, options) {
|
|
412
|
+
const {sourceCode} = context;
|
|
413
|
+
const configureList = options;
|
|
414
|
+
let scopeInfo = null;
|
|
415
|
+
|
|
416
|
+
const enterScope = () => {
|
|
417
|
+
scopeInfo = {upper: scopeInfo, prevNode: null};
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
const exitScope = () => {
|
|
421
|
+
if (scopeInfo) {
|
|
422
|
+
scopeInfo = scopeInfo.upper;
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
const match = (node, type) => {
|
|
427
|
+
let innerStatementNode = node;
|
|
428
|
+
|
|
429
|
+
while (innerStatementNode.type === T.LabeledStatement) {
|
|
430
|
+
innerStatementNode = innerStatementNode.body;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (Array.isArray(type)) {
|
|
434
|
+
return type.some(match.bind(null, innerStatementNode));
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return StatementTypes[type].test(innerStatementNode, sourceCode);
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
const getPaddingType = (prevNode, nextNode) => {
|
|
441
|
+
for (let i = configureList.length - 1; i >= 0; --i) {
|
|
442
|
+
const configure = configureList[i];
|
|
443
|
+
|
|
444
|
+
if (match(prevNode, configure.prev) && match(nextNode, configure.next)) {
|
|
445
|
+
return PaddingTypes[configure.blankLine];
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return PaddingTypes.any;
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
const getPaddingLineSequences = (prevNode, nextNode) => {
|
|
453
|
+
const pairs = [];
|
|
454
|
+
let prevToken = getActualLastToken(prevNode, sourceCode);
|
|
455
|
+
|
|
456
|
+
if (nextNode.loc.start.line - prevToken.loc.end.line >= 2) {
|
|
457
|
+
do {
|
|
458
|
+
const token = sourceCode.getTokenAfter(prevToken, {includeComments: true});
|
|
459
|
+
|
|
460
|
+
if (token.loc.start.line - prevToken.loc.end.line >= 2) {
|
|
461
|
+
pairs.push([prevToken, token]);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
prevToken = token;
|
|
465
|
+
} while (prevToken.range[0] < nextNode.range[0]);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return pairs;
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
const verify = node => {
|
|
472
|
+
if (
|
|
473
|
+
!node.parent ||
|
|
474
|
+
![
|
|
475
|
+
T.BlockStatement,
|
|
476
|
+
T.Program,
|
|
477
|
+
T.StaticBlock,
|
|
478
|
+
T.SwitchCase,
|
|
479
|
+
T.SwitchStatement,
|
|
480
|
+
T.TSInterfaceBody,
|
|
481
|
+
T.TSModuleBlock,
|
|
482
|
+
T.TSTypeLiteral,
|
|
483
|
+
].includes(node.parent.type)
|
|
484
|
+
) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const {prevNode} = scopeInfo;
|
|
489
|
+
|
|
490
|
+
if (prevNode) {
|
|
491
|
+
const type = getPaddingType(prevNode, node);
|
|
492
|
+
const paddingLines = getPaddingLineSequences(prevNode, node);
|
|
493
|
+
type.verify(context, prevNode, node, paddingLines);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
scopeInfo.prevNode = node;
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
return {
|
|
500
|
+
'Program': enterScope,
|
|
501
|
+
'Program:exit': exitScope,
|
|
502
|
+
'BlockStatement': enterScope,
|
|
503
|
+
'BlockStatement:exit': exitScope,
|
|
504
|
+
'SwitchStatement': enterScope,
|
|
505
|
+
'SwitchStatement:exit': exitScope,
|
|
506
|
+
'SwitchCase': node => {
|
|
507
|
+
verify(node);
|
|
508
|
+
enterScope();
|
|
509
|
+
},
|
|
510
|
+
'SwitchCase:exit': exitScope,
|
|
511
|
+
'StaticBlock': enterScope,
|
|
512
|
+
'StaticBlock:exit': exitScope,
|
|
513
|
+
'TSInterfaceBody': enterScope,
|
|
514
|
+
'TSInterfaceBody:exit': exitScope,
|
|
515
|
+
'TSModuleBlock': enterScope,
|
|
516
|
+
'TSModuleBlock:exit': exitScope,
|
|
517
|
+
'TSTypeLiteral': enterScope,
|
|
518
|
+
'TSTypeLiteral:exit': exitScope,
|
|
519
|
+
'TSDeclareFunction': node => {
|
|
520
|
+
verify(node);
|
|
521
|
+
enterScope();
|
|
522
|
+
},
|
|
523
|
+
'TSDeclareFunction:exit': exitScope,
|
|
524
|
+
'TSMethodSignature': node => {
|
|
525
|
+
verify(node);
|
|
526
|
+
enterScope();
|
|
527
|
+
},
|
|
528
|
+
'TSMethodSignature:exit': exitScope,
|
|
529
|
+
':statement': verify,
|
|
530
|
+
};
|
|
531
|
+
},
|
|
532
|
+
});
|