@ref0/workspace 0.0.0-stage → 0.1.0
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/LICENSE +201 -0
- package/README.md +69 -2
- package/dist/bin.d.mts +1 -0
- package/dist/bin.mjs +30 -0
- package/dist/docs-budget.mjs +122 -0
- package/dist/lint/anti-slop/LICENSE +21 -0
- package/dist/lint/anti-slop/UPSTREAM.md +11 -0
- package/dist/lint/anti-slop/effect/plugin.d.mts +3 -0
- package/dist/lint/anti-slop/effect/plugin.mjs +19 -0
- package/dist/lint/anti-slop/effect/rules/no-manual-effect-error-tag.mjs +33 -0
- package/dist/lint/anti-slop/effect/rules/no-manual-tag-comparison.mjs +32 -0
- package/dist/lint/anti-slop/effect/rules/no-manual-tagged-construction.mjs +22 -0
- package/dist/lint/anti-slop/effect/rules/no-service-constructor-imports.mjs +36 -0
- package/dist/lint/anti-slop/effect/rules/prefer-effect-match.mjs +40 -0
- package/dist/lint/anti-slop/effect/shared/tagged-values.mjs +40 -0
- package/dist/lint/anti-slop/plugin.d.mts +3 -0
- package/dist/lint/anti-slop/plugin.mjs +45 -0
- package/dist/lint/anti-slop/rules/no-array-filter-map.mjs +31 -0
- package/dist/lint/anti-slop/rules/no-chained-type-assertions.mjs +56 -0
- package/dist/lint/anti-slop/rules/no-conditional-empty-object-spread.mjs +32 -0
- package/dist/lint/anti-slop/rules/no-known-value-widening.mjs +236 -0
- package/dist/lint/anti-slop/rules/no-module-mocking.mjs +48 -0
- package/dist/lint/anti-slop/rules/no-object-parameters.mjs +48 -0
- package/dist/lint/anti-slop/rules/no-reduce-accumulator-copy.mjs +85 -0
- package/dist/lint/anti-slop/rules/no-reflect-apply.mjs +21 -0
- package/dist/lint/anti-slop/rules/no-reflect-get.mjs +21 -0
- package/dist/lint/anti-slop/rules/no-runtime-typeof.mjs +52 -0
- package/dist/lint/anti-slop/rules/no-shape-in-symbol-names.mjs +36 -0
- package/dist/lint/anti-slop/rules/no-unknown-parameters.mjs +44 -0
- package/dist/lint/anti-slop/rules/no-unknown-returns.mjs +47 -0
- package/dist/lint/anti-slop/rules/no-unknown-type-aliases.mjs +33 -0
- package/dist/lint/anti-slop/rules/no-unsafe-dictionary-type.mjs +121 -0
- package/dist/lint/anti-slop/rules/no-widen-then-assert.mjs +191 -0
- package/dist/lint/anti-slop/rules/require-readable-spacing.mjs +86 -0
- package/dist/lint/anti-slop/rules/require-safety-comment-for-type-assertion.mjs +82 -0
- package/dist/lint/anti-slop/shared/array-method.mjs +70 -0
- package/dist/lint/anti-slop/shared/child-nodes.mjs +24 -0
- package/dist/lint/anti-slop/shared/dictionary-types.mjs +248 -0
- package/dist/lint/anti-slop/shared/function-parameters.mjs +25 -0
- package/dist/lint/anti-slop/shared/lexical-type-parameters.mjs +21 -0
- package/dist/lint/anti-slop/shared/reflect-method.mjs +16 -0
- package/dist/lint/anti-slop/shared/scope.mjs +12 -0
- package/dist/lint/anti-slop/shared/string-literal.mjs +6 -0
- package/dist/lint/anti-slop/shared/type-alias-resolution.mjs +134 -0
- package/dist/lint/anti-slop/vendor/eslint-stylistic/LICENSE +22 -0
- package/dist/lint/anti-slop/vendor/eslint-stylistic/UPSTREAM.md +29 -0
- package/dist/lint/anti-slop/vendor/eslint-stylistic/padding-line-ast.mjs +32 -0
- package/dist/lint/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.mjs +608 -0
- package/dist/lint/config.d.mts +20 -0
- package/dist/lint/config.mjs +135 -0
- package/dist/lint/generic/effect-encoding.mjs +45 -0
- package/dist/lint/generic/entry-reexports.mjs +81 -0
- package/dist/lint/generic/env-at-entrypoints.mjs +65 -0
- package/dist/lint/generic/export-type-side-effects.mjs +22 -0
- package/dist/lint/generic/index-barrel.mjs +14 -0
- package/dist/lint/generic/log-messages.mjs +39 -0
- package/dist/lint/generic/plugin.d.mts +6 -0
- package/dist/lint/generic/plugin.mjs +26 -0
- package/dist/lint/generic/seeded-properties.mjs +78 -0
- package/dist/lint/plugins.d.mts +9 -0
- package/dist/lint/plugins.mjs +14 -0
- package/dist/lint/t3code/LICENSE +21 -0
- package/dist/lint/t3code/UPSTREAM.md +24 -0
- package/dist/lint/t3code/plugin.d.mts +2 -0
- package/dist/lint/t3code/plugin.mjs +20 -0
- package/dist/lint/t3code/rules/no-global-process-runtime.mjs +76 -0
- package/dist/lint/t3code/rules/no-inline-schema-compile.mjs +112 -0
- package/dist/lint/t3code/rules/no-manual-effect-runtime-in-tests.mjs +67 -0
- package/dist/lint/t3code/rules/no-native-title-tooltip.mjs +33 -0
- package/dist/lint/t3code/rules/no-test-in-loop.mjs +69 -0
- package/dist/lint/t3code/rules/no-unscoped-has.mjs +147 -0
- package/dist/lint/t3code/utils.mjs +21 -0
- package/package.json +52 -3
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright OpenJS Foundation and other contributors, <www.openjsf.org>
|
|
4
|
+
Copyright (c) 2023-PRESENT ESLint Stylistic contributors
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Vendored padding-line-between-statements
|
|
2
|
+
|
|
3
|
+
Source: [ESLint Stylistic](https://github.com/eslint-stylistic/eslint-stylistic), commit `435c3ea0fd26a5fef9042c4b36b6e165fbbf8d08`.
|
|
4
|
+
|
|
5
|
+
Copied files:
|
|
6
|
+
|
|
7
|
+
- `packages/eslint-plugin/rules/padding-line-between-statements/padding-line-between-statements.ts`
|
|
8
|
+
- `packages/eslint-plugin/rules/padding-line-between-statements/types.d.ts` → `padding-line-options.d.ts`
|
|
9
|
+
- Root `LICENSE`, retained verbatim. Both OpenJS Foundation and ESLint Stylistic notices apply.
|
|
10
|
+
|
|
11
|
+
The rule is MIT-licensed. Keep `LICENSE` with every redistributed copy, including skill assets. No Stylistic, ESLint, TypeScript-ESLint, or additional parser runtime dependency is required.
|
|
12
|
+
|
|
13
|
+
## Local adaptations
|
|
14
|
+
|
|
15
|
+
- Replace upstream type aliases with Oxlint's ESTree, context, token/comment, and rule types. Upstream's token type includes comments; Oxlint exposes those separately.
|
|
16
|
+
- Replace `AST_NODE_TYPES` enum members with identical string literals.
|
|
17
|
+
- Guard indexed reads for consuming repositories with `noUncheckedIndexedAccess`. Impossible missing AST/configuration entries raise explicit invariant errors rather than introducing new non-null assertions.
|
|
18
|
+
- Replace the repository-specific `createRule` factory with `createPaddingLineRule(options)`. The anti-slop wrapper supplies typed options directly; it exposes no user configuration options.
|
|
19
|
+
- Implement the small required AST helper surface in `padding-line-ast.ts` using Oxlint's public source-code/token API. `isParenthesized` only needs the one-pair check used to exclude parenthesized directive strings, not the upstream general-purpose overloads.
|
|
20
|
+
- Keep each statement's answer for each statement type, in `match` over upstream's matcher, now `matchUncached`. Each statement is matched as the next of one pair and the previous of the next, at `Program:exit`, after every selector has matched, so the answers can't change. It reports the same, faster.
|
|
21
|
+
- Retain the upstream statement matchers, scope tracking, comment-aware insertion/removal, selector support, and diagnostic text. Upstream naming and non-null assumptions remain localized here to keep future diffs reviewable; the file is not a model for new application code.
|
|
22
|
+
|
|
23
|
+
The opinionated policy lives outside this directory in `../../rules/require-readable-spacing.ts`. It adds spacing without collapsing existing blank lines. Short local bindings, consecutive imports, and adjacent overload signatures/implementation remain grouped. Spacing is syntactic, not an inference of business-logic boundaries.
|
|
24
|
+
|
|
25
|
+
## Updating and verification
|
|
26
|
+
|
|
27
|
+
Fetch an explicit upstream revision, compare the original rule and types against this revision, and port relevant fixes while retaining the adapters above. Update this record and preserve the license. Run `pnpm run check`.
|
|
28
|
+
|
|
29
|
+
The anti-slop source's focused RuleTester and CLI tests for `require-readable-spacing` weren't copied into this repository, and neither were upstream's JS/TS suites. Check a change here by linting files that break each spacing rule, before and after it.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/lint/anti-slop/vendor/eslint-stylistic/padding-line-ast.ts
|
|
2
|
+
/** Line terminators recognized by the upstream padding matcher. */
|
|
3
|
+
const LINEBREAKS = /* @__PURE__ */ new Set([
|
|
4
|
+
"\r\n",
|
|
5
|
+
"\r",
|
|
6
|
+
"\n",
|
|
7
|
+
"\u2028",
|
|
8
|
+
"\u2029"
|
|
9
|
+
]);
|
|
10
|
+
/** Test a closing brace without treating comment text as punctuation. */
|
|
11
|
+
const isClosingBraceToken = (token) => token.type === "Punctuator" && token.value === "}";
|
|
12
|
+
/** Test a semicolon without treating comment text as punctuation. */
|
|
13
|
+
const isSemicolonToken = (token) => token.type === "Punctuator" && token.value === ";";
|
|
14
|
+
/** Filter the optional final semicolon when identifying block-like statements. */
|
|
15
|
+
const isNotSemicolonToken = (token) => !isSemicolonToken(token);
|
|
16
|
+
/** Compare token/node boundaries, including attached comments. */
|
|
17
|
+
const isTokenOnSameLine = (left, right) => left.loc.end.line === right.loc.start.line;
|
|
18
|
+
/** Recognize declarations and expressions used by the upstream IIFE matcher. */
|
|
19
|
+
const isFunction = (node) => node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
20
|
+
/** Preserve the upstream multiline statement heuristic. */
|
|
21
|
+
const isSingleLine = (node) => node.loc.start.line === node.loc.end.line;
|
|
22
|
+
/** Unwrap optional chaining before checking IIFE syntax. */
|
|
23
|
+
const skipChainExpression = (node) => node.type === "ChainExpression" ? node.expression : node;
|
|
24
|
+
/** Only a program or function-body expression can begin a directive prologue. */
|
|
25
|
+
const isTopLevelExpressionStatement = (node) => node.type === "ExpressionStatement" && (node.parent.type === "Program" || node.parent.type === "BlockStatement" && isFunction(node.parent.parent));
|
|
26
|
+
/** A single wrapping pair suffices to exclude a string from directive syntax. */
|
|
27
|
+
function isParenthesized(node, sourceCode) {
|
|
28
|
+
const before = sourceCode.getTokenBefore(node);
|
|
29
|
+
const after = sourceCode.getTokenAfter(node);
|
|
30
|
+
return before?.value === "(" && after?.value === ")";
|
|
31
|
+
}
|
|
32
|
+
export { LINEBREAKS, isClosingBraceToken, isFunction, isNotSemicolonToken, isParenthesized, isSemicolonToken, isSingleLine, isTokenOnSameLine, isTopLevelExpressionStatement, skipChainExpression };
|
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
import { LINEBREAKS, isClosingBraceToken, isFunction, isNotSemicolonToken, isParenthesized, isSemicolonToken, isSingleLine, isTokenOnSameLine, isTopLevelExpressionStatement, skipChainExpression } from "./padding-line-ast.mjs";
|
|
2
|
+
// src/lint/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.ts
|
|
3
|
+
const CJS_EXPORT = /^(?:module\s*\.\s*)?exports(?:\s*\.|\s*\[|$)/u;
|
|
4
|
+
const CJS_IMPORT = /^require\(/u;
|
|
5
|
+
const LT = `[${Array.from(LINEBREAKS).join("")}]`;
|
|
6
|
+
const PADDING_LINE_SEQUENCE = new RegExp(String.raw`^(\s*?${LT})\s*${LT}(\s*;?)$`, "u");
|
|
7
|
+
function isSelectorOption(option) {
|
|
8
|
+
return typeof option === "object" && !Array.isArray(option);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates tester which check if a node starts with specific keyword with the
|
|
12
|
+
* appropriate AST_NODE_TYPES.
|
|
13
|
+
* @param keyword The keyword to test.
|
|
14
|
+
* @returns the created tester.
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
function newKeywordTester(type, keyword) {
|
|
18
|
+
return { test(node, sourceCode) {
|
|
19
|
+
const isSameKeyword = sourceCode.getFirstToken(node)?.value === keyword;
|
|
20
|
+
const isSameType = Array.isArray(type) ? type.includes(node.type) : type === node.type;
|
|
21
|
+
return isSameKeyword && isSameType;
|
|
22
|
+
} };
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Creates tester which check if a node is specific type.
|
|
26
|
+
* @param type The node type to test.
|
|
27
|
+
* @returns the created tester.
|
|
28
|
+
* @private
|
|
29
|
+
*/
|
|
30
|
+
function newNodeTypeTester(type) {
|
|
31
|
+
return { test: (node) => node.type === type };
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Checks the given node is an expression statement of IIFE.
|
|
35
|
+
* @param node The node to check.
|
|
36
|
+
* @returns `true` if the node is an expression statement of IIFE.
|
|
37
|
+
* @private
|
|
38
|
+
*/
|
|
39
|
+
function isIIFEStatement(node) {
|
|
40
|
+
if (node.type === "ExpressionStatement") {
|
|
41
|
+
let expression = skipChainExpression(node.expression);
|
|
42
|
+
if (expression.type === "UnaryExpression") expression = skipChainExpression(expression.argument);
|
|
43
|
+
if (expression.type === "CallExpression") {
|
|
44
|
+
let node = expression.callee;
|
|
45
|
+
while (node.type === "SequenceExpression") {
|
|
46
|
+
const lastExpression = node.expressions.at(-1);
|
|
47
|
+
if (lastExpression === void 0) throw new Error("Padding rule invariant: sequence expression is empty");
|
|
48
|
+
node = lastExpression;
|
|
49
|
+
}
|
|
50
|
+
return isFunction(node);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Checks the given node is a CommonJS require statement
|
|
57
|
+
* @param node The node to check.
|
|
58
|
+
* @returns `true` if the node is a CommonJS require statement.
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
61
|
+
function isCJSRequire(node) {
|
|
62
|
+
if (node.type === "VariableDeclaration") {
|
|
63
|
+
const declaration = node.declarations[0];
|
|
64
|
+
if (declaration?.init) {
|
|
65
|
+
let call = declaration?.init;
|
|
66
|
+
while (call.type === "MemberExpression") call = call.object;
|
|
67
|
+
if (call.type === "CallExpression" && call.callee.type === "Identifier") return call.callee.name === "require";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Checks whether the given node is a block-like statement.
|
|
74
|
+
* This checks the last token of the node is the closing brace of a block.
|
|
75
|
+
* @param sourceCode The source code to get tokens.
|
|
76
|
+
* @param node The node to check.
|
|
77
|
+
* @returns `true` if the node is a block-like statement.
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
function isBlockLikeStatement(node, sourceCode) {
|
|
81
|
+
if (node.type === "DoWhileStatement" && node.body.type === "BlockStatement") return true;
|
|
82
|
+
/**
|
|
83
|
+
* IIFE is a block-like statement specially from
|
|
84
|
+
* JSCS#disallowPaddingNewLinesAfterBlocks.
|
|
85
|
+
*/
|
|
86
|
+
if (isIIFEStatement(node)) return true;
|
|
87
|
+
const lastToken = sourceCode.getLastToken(node, isNotSemicolonToken);
|
|
88
|
+
const belongingNode = lastToken && isClosingBraceToken(lastToken) ? sourceCode.getNodeByRangeIndex(lastToken.range[0]) : null;
|
|
89
|
+
return !!belongingNode && (belongingNode.type === "BlockStatement" || belongingNode.type === "SwitchStatement");
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Check whether the given node is a directive or not.
|
|
93
|
+
* @param node The node to check.
|
|
94
|
+
* @param sourceCode The source code object to get tokens.
|
|
95
|
+
* @returns `true` if the node is a directive.
|
|
96
|
+
*/
|
|
97
|
+
function isDirective(node, sourceCode) {
|
|
98
|
+
return isTopLevelExpressionStatement(node) && node.expression.type === "Literal" && typeof node.expression.value === "string" && !isParenthesized(node.expression, sourceCode);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Check whether the given node is a part of directive prologue or not.
|
|
102
|
+
* @param node The node to check.
|
|
103
|
+
* @param sourceCode The source code object to get tokens.
|
|
104
|
+
* @returns `true` if the node is a part of directive prologue.
|
|
105
|
+
*/
|
|
106
|
+
function isDirectivePrologue(node, sourceCode) {
|
|
107
|
+
if (isDirective(node, sourceCode) && node.parent && "body" in node.parent && Array.isArray(node.parent.body)) {
|
|
108
|
+
for (const sibling of node.parent.body) {
|
|
109
|
+
if (sibling === node) break;
|
|
110
|
+
if (!isDirective(sibling, sourceCode)) return false;
|
|
111
|
+
}
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Checks the given node is a CommonJS export statement
|
|
118
|
+
* @param node The node to check.
|
|
119
|
+
* @returns `true` if the node is a CommonJS export statement.
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
function isCJSExport(node) {
|
|
123
|
+
if (node.type === "ExpressionStatement") {
|
|
124
|
+
const expression = node.expression;
|
|
125
|
+
if (expression.type === "AssignmentExpression") {
|
|
126
|
+
let left = expression.left;
|
|
127
|
+
if (left.type === "MemberExpression") {
|
|
128
|
+
while (left.object.type === "MemberExpression") left = left.object;
|
|
129
|
+
return left.object.type === "Identifier" && (left.object.name === "exports" || left.object.name === "module" && left.property.type === "Identifier" && left.property.name === "exports");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Check whether the given node is an expression
|
|
137
|
+
* @param node The node to check.
|
|
138
|
+
* @param sourceCode The source code object to get tokens.
|
|
139
|
+
* @returns `true` if the node is an expression
|
|
140
|
+
*/
|
|
141
|
+
function isExpression(node, sourceCode) {
|
|
142
|
+
return node.type === "ExpressionStatement" && !isDirectivePrologue(node, sourceCode);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Gets the actual last token.
|
|
146
|
+
*
|
|
147
|
+
* If a semicolon is semicolon-less style's semicolon, this ignores it.
|
|
148
|
+
* For example:
|
|
149
|
+
*
|
|
150
|
+
* foo()
|
|
151
|
+
* ;[1, 2, 3].forEach(bar)
|
|
152
|
+
* @param sourceCode The source code to get tokens.
|
|
153
|
+
* @param node The node to get.
|
|
154
|
+
* @returns The actual last token.
|
|
155
|
+
* @private
|
|
156
|
+
*/
|
|
157
|
+
function getActualLastToken(node, sourceCode) {
|
|
158
|
+
const semiToken = sourceCode.getLastToken(node);
|
|
159
|
+
const prevToken = sourceCode.getTokenBefore(semiToken);
|
|
160
|
+
const nextToken = sourceCode.getTokenAfter(semiToken);
|
|
161
|
+
return prevToken && nextToken && prevToken.range[0] >= node.range[0] && isSemicolonToken(semiToken) && !isTokenOnSameLine(prevToken, semiToken) && isTokenOnSameLine(semiToken, nextToken) ? prevToken : semiToken;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* This returns the concatenation of the first 2 captured strings.
|
|
165
|
+
* @param _ Unused. Whole matched string.
|
|
166
|
+
* @param trailingSpaces The trailing spaces of the first line.
|
|
167
|
+
* @param indentSpaces The indentation spaces of the last line.
|
|
168
|
+
* @returns The concatenation of trailingSpaces and indentSpaces.
|
|
169
|
+
* @private
|
|
170
|
+
*/
|
|
171
|
+
function replacerToRemovePaddingLines(_, trailingSpaces, indentSpaces) {
|
|
172
|
+
return trailingSpaces + indentSpaces;
|
|
173
|
+
}
|
|
174
|
+
function getReportLoc(node, sourceCode) {
|
|
175
|
+
if (isSingleLine(node)) return node.loc;
|
|
176
|
+
const line = node.loc.start.line;
|
|
177
|
+
const sourceLine = sourceCode.lines[line - 1];
|
|
178
|
+
if (sourceLine === void 0) throw new Error("Padding rule invariant: statement source line is missing");
|
|
179
|
+
return {
|
|
180
|
+
start: node.loc.start,
|
|
181
|
+
end: {
|
|
182
|
+
line,
|
|
183
|
+
column: sourceLine.length
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Check and report statements for `any` configuration.
|
|
189
|
+
* It does nothing.
|
|
190
|
+
*
|
|
191
|
+
* @private
|
|
192
|
+
*/
|
|
193
|
+
function verifyForAny() {}
|
|
194
|
+
/**
|
|
195
|
+
* Check and report statements for `never` configuration.
|
|
196
|
+
* This autofix removes blank lines between the given 2 statements.
|
|
197
|
+
* However, if comments exist between 2 blank lines, it does not remove those
|
|
198
|
+
* blank lines automatically.
|
|
199
|
+
* @param context The rule context to report.
|
|
200
|
+
* @param _ Unused. The previous node to check.
|
|
201
|
+
* @param nextNode The next node to check.
|
|
202
|
+
* @param paddingLines The array of token pairs that blank
|
|
203
|
+
* lines exist between the pair.
|
|
204
|
+
*
|
|
205
|
+
* @private
|
|
206
|
+
*/
|
|
207
|
+
function verifyForNever(context, _, nextNode, paddingLines) {
|
|
208
|
+
if (paddingLines.length === 0) return;
|
|
209
|
+
context.report({
|
|
210
|
+
node: nextNode,
|
|
211
|
+
messageId: "unexpectedBlankLine",
|
|
212
|
+
loc: getReportLoc(nextNode, context.sourceCode),
|
|
213
|
+
fix(fixer) {
|
|
214
|
+
if (paddingLines.length >= 2) return null;
|
|
215
|
+
const paddingPair = paddingLines[0];
|
|
216
|
+
if (paddingPair === void 0) throw new Error("Padding rule invariant: reported padding pair is missing");
|
|
217
|
+
const [prevToken, nextToken] = paddingPair;
|
|
218
|
+
const start = prevToken.range[1];
|
|
219
|
+
const end = nextToken.range[0];
|
|
220
|
+
const text = context.sourceCode.text.slice(start, end).replace(PADDING_LINE_SEQUENCE, replacerToRemovePaddingLines);
|
|
221
|
+
return fixer.replaceTextRange([start, end], text);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Check and report statements for `always` configuration.
|
|
227
|
+
* This autofix inserts a blank line between the given 2 statements.
|
|
228
|
+
* If the `prevNode` has trailing comments, it inserts a blank line after the
|
|
229
|
+
* trailing comments.
|
|
230
|
+
* @param context The rule context to report.
|
|
231
|
+
* @param prevNode The previous node to check.
|
|
232
|
+
* @param nextNode The next node to check.
|
|
233
|
+
* @param paddingLines The array of token pairs that blank
|
|
234
|
+
* lines exist between the pair.
|
|
235
|
+
*
|
|
236
|
+
* @private
|
|
237
|
+
*/
|
|
238
|
+
function verifyForAlways(context, prevNode, nextNode, paddingLines) {
|
|
239
|
+
if (paddingLines.length > 0) return;
|
|
240
|
+
context.report({
|
|
241
|
+
node: nextNode,
|
|
242
|
+
messageId: "expectedBlankLine",
|
|
243
|
+
loc: getReportLoc(nextNode, context.sourceCode),
|
|
244
|
+
fix(fixer) {
|
|
245
|
+
const sourceCode = context.sourceCode;
|
|
246
|
+
let prevToken = getActualLastToken(prevNode, sourceCode);
|
|
247
|
+
const nextToken = sourceCode.getFirstTokenBetween(prevToken, nextNode, {
|
|
248
|
+
includeComments: true,
|
|
249
|
+
/**
|
|
250
|
+
* Skip the trailing comments of the previous node.
|
|
251
|
+
* This inserts a blank line after the last trailing comment.
|
|
252
|
+
*
|
|
253
|
+
* For example:
|
|
254
|
+
*
|
|
255
|
+
* foo(); // trailing comment.
|
|
256
|
+
* // comment.
|
|
257
|
+
* bar();
|
|
258
|
+
*
|
|
259
|
+
* Get fixed to:
|
|
260
|
+
*
|
|
261
|
+
* foo(); // trailing comment.
|
|
262
|
+
*
|
|
263
|
+
* // comment.
|
|
264
|
+
* bar();
|
|
265
|
+
* @param token The token to check.
|
|
266
|
+
* @returns `true` if the token is not a trailing comment.
|
|
267
|
+
* @private
|
|
268
|
+
*/
|
|
269
|
+
filter(token) {
|
|
270
|
+
if (isTokenOnSameLine(prevToken, token)) {
|
|
271
|
+
prevToken = token;
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
}) || nextNode;
|
|
277
|
+
const insertText = isTokenOnSameLine(prevToken, nextToken) ? "\n\n" : "\n";
|
|
278
|
+
return fixer.insertTextAfter(prevToken, insertText);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Types of blank lines.
|
|
284
|
+
* `any`, `never`, and `always` are defined.
|
|
285
|
+
* Those have `verify` method to check and report statements.
|
|
286
|
+
* @private
|
|
287
|
+
*/
|
|
288
|
+
const PaddingTypes = {
|
|
289
|
+
any: { verify: verifyForAny },
|
|
290
|
+
never: { verify: verifyForNever },
|
|
291
|
+
always: { verify: verifyForAlways }
|
|
292
|
+
};
|
|
293
|
+
const MaybeMultilineStatementType = {
|
|
294
|
+
"block-like": { test: isBlockLikeStatement },
|
|
295
|
+
"expression": { test: isExpression },
|
|
296
|
+
"return": newKeywordTester("ReturnStatement", "return"),
|
|
297
|
+
"export": newKeywordTester([
|
|
298
|
+
"ExportAllDeclaration",
|
|
299
|
+
"ExportDefaultDeclaration",
|
|
300
|
+
"ExportNamedDeclaration"
|
|
301
|
+
], "export"),
|
|
302
|
+
"var": newKeywordTester("VariableDeclaration", "var"),
|
|
303
|
+
"let": newKeywordTester("VariableDeclaration", "let"),
|
|
304
|
+
"const": newKeywordTester("VariableDeclaration", "const"),
|
|
305
|
+
"using": { test: (node) => node.type === "VariableDeclaration" && (node.kind === "using" || node.kind === "await using") },
|
|
306
|
+
"type": newKeywordTester("TSTypeAliasDeclaration", "type")
|
|
307
|
+
};
|
|
308
|
+
/**
|
|
309
|
+
* Types of statements.
|
|
310
|
+
* Those have `test` method to check it matches to the given statement.
|
|
311
|
+
* @private
|
|
312
|
+
*/
|
|
313
|
+
const StatementTypes = {
|
|
314
|
+
"*": { test: () => true },
|
|
315
|
+
"exports": { test: isCJSExport },
|
|
316
|
+
"require": { test: isCJSRequire },
|
|
317
|
+
"directive": { test: isDirectivePrologue },
|
|
318
|
+
"iife": { test: isIIFEStatement },
|
|
319
|
+
"block": newNodeTypeTester("BlockStatement"),
|
|
320
|
+
"empty": newNodeTypeTester("EmptyStatement"),
|
|
321
|
+
"function": newNodeTypeTester("FunctionDeclaration"),
|
|
322
|
+
"ts-method": newNodeTypeTester("TSMethodSignature"),
|
|
323
|
+
"break": newKeywordTester("BreakStatement", "break"),
|
|
324
|
+
"case": newKeywordTester("SwitchCase", "case"),
|
|
325
|
+
"class": newKeywordTester("ClassDeclaration", "class"),
|
|
326
|
+
"continue": newKeywordTester("ContinueStatement", "continue"),
|
|
327
|
+
"debugger": newKeywordTester("DebuggerStatement", "debugger"),
|
|
328
|
+
"default": newKeywordTester(["SwitchCase", "ExportDefaultDeclaration"], "default"),
|
|
329
|
+
"do": newKeywordTester("DoWhileStatement", "do"),
|
|
330
|
+
"for": newKeywordTester([
|
|
331
|
+
"ForStatement",
|
|
332
|
+
"ForInStatement",
|
|
333
|
+
"ForOfStatement"
|
|
334
|
+
], "for"),
|
|
335
|
+
"if": newKeywordTester("IfStatement", "if"),
|
|
336
|
+
"import": newKeywordTester("ImportDeclaration", "import"),
|
|
337
|
+
"switch": newKeywordTester("SwitchStatement", "switch"),
|
|
338
|
+
"throw": newKeywordTester("ThrowStatement", "throw"),
|
|
339
|
+
"try": newKeywordTester("TryStatement", "try"),
|
|
340
|
+
"while": newKeywordTester(["WhileStatement", "DoWhileStatement"], "while"),
|
|
341
|
+
"with": newKeywordTester("WithStatement", "with"),
|
|
342
|
+
"cjs-export": { test: (node, sourceCode) => node.type === "ExpressionStatement" && node.expression.type === "AssignmentExpression" && CJS_EXPORT.test(sourceCode.getText(node.expression.left)) },
|
|
343
|
+
"cjs-import": { test: (node, sourceCode) => node.type === "VariableDeclaration" && node.declarations.length > 0 && node.declarations[0]?.init != null && CJS_IMPORT.test(sourceCode.getText(node.declarations[0].init)) },
|
|
344
|
+
"enum": newKeywordTester("TSEnumDeclaration", "enum"),
|
|
345
|
+
"interface": newKeywordTester("TSInterfaceDeclaration", "interface"),
|
|
346
|
+
"function-overload": newNodeTypeTester("TSDeclareFunction"),
|
|
347
|
+
...Object.fromEntries(Object.entries(MaybeMultilineStatementType).flatMap(([key, value]) => [
|
|
348
|
+
[key, value],
|
|
349
|
+
[`singleline-${key}`, {
|
|
350
|
+
...value,
|
|
351
|
+
test: (node, sourceCode) => value.test(node, sourceCode) && isSingleLine(node)
|
|
352
|
+
}],
|
|
353
|
+
[`multiline-${key}`, {
|
|
354
|
+
...value,
|
|
355
|
+
test: (node, sourceCode) => value.test(node, sourceCode) && !isSingleLine(node)
|
|
356
|
+
}]
|
|
357
|
+
]))
|
|
358
|
+
};
|
|
359
|
+
/** Build the vendored padding rule with caller-owned, typed policy options. */
|
|
360
|
+
function createPaddingLineRule(options) {
|
|
361
|
+
return {
|
|
362
|
+
meta: {
|
|
363
|
+
type: "layout",
|
|
364
|
+
docs: { description: "Require or disallow padding lines between statements" },
|
|
365
|
+
fixable: "whitespace",
|
|
366
|
+
hasSuggestions: false,
|
|
367
|
+
schema: {
|
|
368
|
+
$defs: {
|
|
369
|
+
paddingType: {
|
|
370
|
+
type: "string",
|
|
371
|
+
enum: Object.keys(PaddingTypes)
|
|
372
|
+
},
|
|
373
|
+
statementType: {
|
|
374
|
+
type: "string",
|
|
375
|
+
enum: Object.keys(StatementTypes)
|
|
376
|
+
},
|
|
377
|
+
selectorOption: {
|
|
378
|
+
type: "object",
|
|
379
|
+
properties: {
|
|
380
|
+
selector: { type: "string" },
|
|
381
|
+
lineMode: {
|
|
382
|
+
type: "string",
|
|
383
|
+
enum: [
|
|
384
|
+
"any",
|
|
385
|
+
"singleline",
|
|
386
|
+
"multiline"
|
|
387
|
+
]
|
|
388
|
+
}
|
|
389
|
+
},
|
|
390
|
+
required: ["selector"],
|
|
391
|
+
additionalProperties: false
|
|
392
|
+
},
|
|
393
|
+
statementMatcher: { anyOf: [{ $ref: "#/$defs/statementType" }, { $ref: "#/$defs/selectorOption" }] },
|
|
394
|
+
statementOption: { anyOf: [{ $ref: "#/$defs/statementMatcher" }, {
|
|
395
|
+
type: "array",
|
|
396
|
+
items: { $ref: "#/$defs/statementMatcher" },
|
|
397
|
+
minItems: 1,
|
|
398
|
+
uniqueItems: true,
|
|
399
|
+
additionalItems: false
|
|
400
|
+
}] }
|
|
401
|
+
},
|
|
402
|
+
type: "array",
|
|
403
|
+
additionalItems: false,
|
|
404
|
+
items: {
|
|
405
|
+
type: "object",
|
|
406
|
+
properties: {
|
|
407
|
+
blankLine: { $ref: "#/$defs/paddingType" },
|
|
408
|
+
prev: { $ref: "#/$defs/statementOption" },
|
|
409
|
+
next: { $ref: "#/$defs/statementOption" }
|
|
410
|
+
},
|
|
411
|
+
additionalProperties: false,
|
|
412
|
+
required: [
|
|
413
|
+
"blankLine",
|
|
414
|
+
"prev",
|
|
415
|
+
"next"
|
|
416
|
+
]
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
messages: {
|
|
420
|
+
unexpectedBlankLine: "Unexpected blank line before this statement.",
|
|
421
|
+
expectedBlankLine: "Expected blank line before this statement."
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
create(context) {
|
|
425
|
+
const sourceCode = context.sourceCode;
|
|
426
|
+
const selectorMatchedNodes = /* @__PURE__ */ new Map();
|
|
427
|
+
const pendingPairs = [];
|
|
428
|
+
function collectSelectorOption(option) {
|
|
429
|
+
if (Array.isArray(option)) {
|
|
430
|
+
for (const item of option) collectSelectorOption(item);
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
if (!isSelectorOption(option)) return;
|
|
434
|
+
selectorMatchedNodes.set(option.selector, /* @__PURE__ */ new Set());
|
|
435
|
+
}
|
|
436
|
+
for (const configure of options) {
|
|
437
|
+
collectSelectorOption(configure.prev);
|
|
438
|
+
collectSelectorOption(configure.next);
|
|
439
|
+
}
|
|
440
|
+
let scopeInfo = null;
|
|
441
|
+
/**
|
|
442
|
+
* Processes to enter to new scope.
|
|
443
|
+
* This manages the current previous statement.
|
|
444
|
+
*
|
|
445
|
+
* @private
|
|
446
|
+
*/
|
|
447
|
+
function enterScope() {
|
|
448
|
+
scopeInfo = {
|
|
449
|
+
upper: scopeInfo,
|
|
450
|
+
prevNode: null
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Processes to exit from the current scope.
|
|
455
|
+
*
|
|
456
|
+
* @private
|
|
457
|
+
*/
|
|
458
|
+
function exitScope() {
|
|
459
|
+
if (scopeInfo) scopeInfo = scopeInfo.upper;
|
|
460
|
+
}
|
|
461
|
+
const matched = /* @__PURE__ */ new Map();
|
|
462
|
+
/**
|
|
463
|
+
* Checks whether the given node matches the given type, once for each node and type.
|
|
464
|
+
* @param node The statement node to check.
|
|
465
|
+
* @param type The statement type to check.
|
|
466
|
+
* @returns `true` if the statement node matched the type.
|
|
467
|
+
* @private
|
|
468
|
+
*/
|
|
469
|
+
function match(node, type) {
|
|
470
|
+
const byType = matched.get(node) ?? /* @__PURE__ */ new Map();
|
|
471
|
+
const known = byType.get(type);
|
|
472
|
+
if (known !== void 0) return known;
|
|
473
|
+
const result = matchUncached(node, type);
|
|
474
|
+
byType.set(type, result);
|
|
475
|
+
matched.set(node, byType);
|
|
476
|
+
return result;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Checks whether the given node matches the given type.
|
|
480
|
+
* @param node The statement node to check.
|
|
481
|
+
* @param type The statement type to check.
|
|
482
|
+
* @returns `true` if the statement node matched the type.
|
|
483
|
+
* @private
|
|
484
|
+
*/
|
|
485
|
+
function matchUncached(node, type) {
|
|
486
|
+
let innerStatementNode = node;
|
|
487
|
+
while (innerStatementNode.type === "LabeledStatement") innerStatementNode = innerStatementNode.body;
|
|
488
|
+
if (Array.isArray(type)) return type.some(match.bind(null, innerStatementNode));
|
|
489
|
+
if (isSelectorOption(type)) {
|
|
490
|
+
if (!selectorMatchedNodes.get(type.selector)?.has(innerStatementNode)) return false;
|
|
491
|
+
const lineMode = type.lineMode;
|
|
492
|
+
if (lineMode === "singleline") return isSingleLine(innerStatementNode);
|
|
493
|
+
else if (lineMode === "multiline") return !isSingleLine(innerStatementNode);
|
|
494
|
+
return true;
|
|
495
|
+
} else {
|
|
496
|
+
const statementType = StatementTypes[type];
|
|
497
|
+
if (statementType === void 0) throw new Error(`Padding rule invariant: unsupported statement type ${type}`);
|
|
498
|
+
return statementType.test(innerStatementNode, sourceCode);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Finds the last matched configure from options.
|
|
503
|
+
* @param prevNode The previous statement to match.
|
|
504
|
+
* @param nextNode The current statement to match.
|
|
505
|
+
* @returns The tester of the last matched configure.
|
|
506
|
+
* @private
|
|
507
|
+
*/
|
|
508
|
+
function getPaddingType(prevNode, nextNode) {
|
|
509
|
+
for (let i = options.length - 1; i >= 0; --i) {
|
|
510
|
+
const configure = options[i];
|
|
511
|
+
if (configure === void 0) throw new Error("Padding rule invariant: configuration entry is missing");
|
|
512
|
+
if (match(prevNode, configure.prev) && match(nextNode, configure.next)) return PaddingTypes[configure.blankLine];
|
|
513
|
+
}
|
|
514
|
+
return PaddingTypes.any;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Gets padding line sequences between the given 2 statements.
|
|
518
|
+
* Comments are separators of the padding line sequences.
|
|
519
|
+
* @param prevNode The previous statement to count.
|
|
520
|
+
* @param nextNode The current statement to count.
|
|
521
|
+
* @returns The array of token pairs.
|
|
522
|
+
* @private
|
|
523
|
+
*/
|
|
524
|
+
function getPaddingLineSequences(prevNode, nextNode) {
|
|
525
|
+
const pairs = [];
|
|
526
|
+
let prevToken = getActualLastToken(prevNode, sourceCode);
|
|
527
|
+
if (nextNode.loc.start.line - prevToken.loc.end.line >= 2) do {
|
|
528
|
+
const token = sourceCode.getTokenAfter(prevToken, { includeComments: true });
|
|
529
|
+
if (token.loc.start.line - prevToken.loc.end.line >= 2) pairs.push([prevToken, token]);
|
|
530
|
+
prevToken = token;
|
|
531
|
+
} while (prevToken.range[0] < nextNode.range[0]);
|
|
532
|
+
return pairs;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Verify padding lines between the given node and the previous node.
|
|
536
|
+
* @param node The node to verify.
|
|
537
|
+
*
|
|
538
|
+
* @private
|
|
539
|
+
*/
|
|
540
|
+
function verify(node) {
|
|
541
|
+
if (!node.parent || ![
|
|
542
|
+
"BlockStatement",
|
|
543
|
+
"Program",
|
|
544
|
+
"StaticBlock",
|
|
545
|
+
"SwitchCase",
|
|
546
|
+
"SwitchStatement",
|
|
547
|
+
"TSInterfaceBody",
|
|
548
|
+
"TSModuleBlock",
|
|
549
|
+
"TSTypeLiteral"
|
|
550
|
+
].includes(node.parent.type)) return;
|
|
551
|
+
const prevNode = scopeInfo.prevNode;
|
|
552
|
+
if (prevNode) pendingPairs.push({
|
|
553
|
+
prevNode,
|
|
554
|
+
nextNode: node
|
|
555
|
+
});
|
|
556
|
+
scopeInfo.prevNode = node;
|
|
557
|
+
}
|
|
558
|
+
function verifyPendingPairs() {
|
|
559
|
+
for (const { prevNode, nextNode } of pendingPairs) {
|
|
560
|
+
const type = getPaddingType(prevNode, nextNode);
|
|
561
|
+
const paddingLines = getPaddingLineSequences(prevNode, nextNode);
|
|
562
|
+
type.verify(context, prevNode, nextNode, paddingLines);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Verify padding lines between the given node and the previous node.
|
|
567
|
+
* Then process to enter to new scope.
|
|
568
|
+
* @param node The node to verify.
|
|
569
|
+
*
|
|
570
|
+
* @private
|
|
571
|
+
*/
|
|
572
|
+
function verifyThenEnterScope(node) {
|
|
573
|
+
verify(node);
|
|
574
|
+
enterScope();
|
|
575
|
+
}
|
|
576
|
+
return {
|
|
577
|
+
"Program": enterScope,
|
|
578
|
+
"Program:exit": () => {
|
|
579
|
+
verifyPendingPairs();
|
|
580
|
+
exitScope();
|
|
581
|
+
},
|
|
582
|
+
"BlockStatement": enterScope,
|
|
583
|
+
"BlockStatement:exit": exitScope,
|
|
584
|
+
"SwitchStatement": enterScope,
|
|
585
|
+
"SwitchStatement:exit": exitScope,
|
|
586
|
+
"SwitchCase": verifyThenEnterScope,
|
|
587
|
+
"SwitchCase:exit": exitScope,
|
|
588
|
+
"StaticBlock": enterScope,
|
|
589
|
+
"StaticBlock:exit": exitScope,
|
|
590
|
+
"TSInterfaceBody": enterScope,
|
|
591
|
+
"TSInterfaceBody:exit": exitScope,
|
|
592
|
+
"TSModuleBlock": enterScope,
|
|
593
|
+
"TSModuleBlock:exit": exitScope,
|
|
594
|
+
"TSTypeLiteral": enterScope,
|
|
595
|
+
"TSTypeLiteral:exit": exitScope,
|
|
596
|
+
"TSDeclareFunction": verifyThenEnterScope,
|
|
597
|
+
"TSDeclareFunction:exit": exitScope,
|
|
598
|
+
"TSMethodSignature": verifyThenEnterScope,
|
|
599
|
+
"TSMethodSignature:exit": exitScope,
|
|
600
|
+
":statement": verify,
|
|
601
|
+
...Object.fromEntries(Array.from(selectorMatchedNodes.keys(), (selector) => [selector, (node) => {
|
|
602
|
+
selectorMatchedNodes.get(selector)?.add(node);
|
|
603
|
+
}]))
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
export { createPaddingLineRule as default };
|