@bemoje/eslint 1.2.0 → 2.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/README.md +15 -0
- package/lib/index.cjs +76 -45
- package/lib/index.d.cts +68 -4
- package/lib/index.d.ts +68 -4
- package/lib/index.js +75 -47
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@ Custom ESLint plugin and rule utilities for consistent code style in TypeScript
|
|
|
11
11
|
- **createRule**: Helper to create an ESLint rule with a consistent format.
|
|
12
12
|
- **eslintPluginBemoje**: ESLint plugin for Bemoje projects.
|
|
13
13
|
- **noBlankLineBetweenCommentAndDeclaration**: Enforce no blank lines between block comments and the next declaration.
|
|
14
|
+
- **splitImports**: Enforce no blank lines between block comments and the next declaration.
|
|
14
15
|
|
|
15
16
|
<!-- EXPORTS_END -->
|
|
16
17
|
|
|
@@ -29,6 +30,20 @@ npm install --save-dev @bemoje/eslint
|
|
|
29
30
|
|
|
30
31
|
## Usage
|
|
31
32
|
|
|
33
|
+
### One rule at a time
|
|
34
|
+
|
|
35
|
+
Each rule has a ready-made config entry. Import the ones you want and put them in the list: there is no plugin to register and no rule name to type.
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
// eslint.config.mjs
|
|
39
|
+
import { enforceNoBlankLineBetweenCommentAndDeclaration } from '@bemoje/eslint'
|
|
40
|
+
import { enforceSplitImports } from '@bemoje/eslint'
|
|
41
|
+
|
|
42
|
+
export default [enforceSplitImports, enforceNoBlankLineBetweenCommentAndDeclaration]
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
A rule's full name, for an `eslint-disable` comment, is `bemoje/<rule>`: `bemoje/split-imports`, `bemoje/no-blank-line-between-comment-and-declaration`.
|
|
46
|
+
|
|
32
47
|
### Plugin Setup (Flat Config)
|
|
33
48
|
|
|
34
49
|
```js
|
package/lib/index.cjs
CHANGED
|
@@ -4,9 +4,7 @@ var utils = require('@typescript-eslint/utils');
|
|
|
4
4
|
|
|
5
5
|
var __defProp = Object.defineProperty;
|
|
6
6
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
-
var createRule = utils.ESLintUtils.RuleCreator((name) => {
|
|
8
|
-
return `https://github.com/bemoje/mono/blob/dev/libs/eslint/README.md#${name}`;
|
|
9
|
-
});
|
|
7
|
+
var createRule = utils.ESLintUtils.RuleCreator((name) => `https://github.com/bemoje/mono/blob/dev/libs/eslint/README.md#${name}`);
|
|
10
8
|
var noBlankLineBetweenCommentAndDeclaration = createRule({
|
|
11
9
|
name: "no-blank-line-between-comment-and-declaration",
|
|
12
10
|
meta: {
|
|
@@ -34,32 +32,35 @@ var noBlankLineBetweenCommentAndDeclaration = createRule({
|
|
|
34
32
|
if (Math.abs(comment.loc.start.line - comment.loc.end.line) < 2) {
|
|
35
33
|
return;
|
|
36
34
|
}
|
|
37
|
-
const nextToken = context.sourceCode.getTokenAfter(comment
|
|
35
|
+
const nextToken = context.sourceCode.getTokenAfter(comment, {
|
|
36
|
+
includeComments: true
|
|
37
|
+
});
|
|
38
38
|
if (!nextToken) {
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
|
+
if (nextToken.type === utils.AST_TOKEN_TYPES.Block || nextToken.type === utils.AST_TOKEN_TYPES.Line) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
41
44
|
const actualNextTokenStartLine = nextToken.loc.start.line;
|
|
42
45
|
const expectedTokenStartLine = comment.loc.end.line + 1;
|
|
43
46
|
if (!(actualNextTokenStartLine > expectedTokenStartLine)) {
|
|
44
47
|
return;
|
|
45
48
|
}
|
|
46
|
-
const lineBeforeNextToken = context.sourceCode.lines[nextToken.loc.start.line - 1 - 1];
|
|
47
|
-
const hasLineCommentBetween = lineBeforeNextToken.trim().startsWith("//");
|
|
48
|
-
if (hasLineCommentBetween) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
49
|
return context.report({
|
|
52
|
-
|
|
50
|
+
// Where the gap is: from the end of the comment to the start of the code after it
|
|
51
|
+
loc: {
|
|
52
|
+
start: comment.loc.end,
|
|
53
|
+
end: nextToken.loc.start
|
|
54
|
+
},
|
|
53
55
|
messageId: "no-blank-line-between-comment-and-declaration",
|
|
54
56
|
fix(fixer) {
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const rangeCodeFixed = rangeCode.replaceAll(/\*\/\n\n+/gi, "*/\n");
|
|
57
|
+
const gap = context.sourceCode.text.slice(comment.range[1], nextToken.range[0]);
|
|
58
|
+
const lineBreak = gap.includes("\r\n") ? "\r\n" : "\n";
|
|
59
|
+
const indentation = gap.slice(gap.lastIndexOf("\n") + 1);
|
|
59
60
|
return fixer.replaceTextRange([
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
],
|
|
61
|
+
comment.range[1],
|
|
62
|
+
nextToken.range[0]
|
|
63
|
+
], lineBreak + indentation);
|
|
63
64
|
}
|
|
64
65
|
});
|
|
65
66
|
});
|
|
@@ -67,8 +68,6 @@ var noBlankLineBetweenCommentAndDeclaration = createRule({
|
|
|
67
68
|
};
|
|
68
69
|
}
|
|
69
70
|
});
|
|
70
|
-
|
|
71
|
-
// src/rules/splitImports.ts
|
|
72
71
|
var splitImports = createRule({
|
|
73
72
|
name: "split-imports",
|
|
74
73
|
meta: {
|
|
@@ -87,32 +86,28 @@ var splitImports = createRule({
|
|
|
87
86
|
ImportDeclaration(node) {
|
|
88
87
|
if (node.specifiers.length > 1) {
|
|
89
88
|
const sourceCode = context.sourceCode;
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
].join(" ");
|
|
111
|
-
});
|
|
89
|
+
const fromModule = sourceCode.getText(node).slice(node.source.range[0] - node.range[0]);
|
|
90
|
+
const importClause = /* @__PURE__ */ __name((specifier) => {
|
|
91
|
+
const local = specifier.local.name;
|
|
92
|
+
if (specifier.type === utils.AST_NODE_TYPES.ImportDefaultSpecifier) {
|
|
93
|
+
return local;
|
|
94
|
+
}
|
|
95
|
+
if (specifier.type === utils.AST_NODE_TYPES.ImportNamespaceSpecifier) {
|
|
96
|
+
return `* as ${local}`;
|
|
97
|
+
}
|
|
98
|
+
const imported = sourceCode.getText(specifier.imported);
|
|
99
|
+
return imported === local ? `{ ${local} }` : `{ ${imported} as ${local} }`;
|
|
100
|
+
}, "importClause");
|
|
101
|
+
const isTypeImport = /* @__PURE__ */ __name((specifier) => node.importKind === "type" || specifier.type === utils.AST_NODE_TYPES.ImportSpecifier && specifier.importKind === "type", "isTypeImport");
|
|
102
|
+
const importStrings = node.specifiers.map((specifier) => [
|
|
103
|
+
isTypeImport(specifier) ? "import type" : "import",
|
|
104
|
+
importClause(specifier),
|
|
105
|
+
"from",
|
|
106
|
+
fromModule
|
|
107
|
+
].join(" "));
|
|
108
|
+
const hasCommentInside = sourceCode.getCommentsInside(node).length > 0;
|
|
112
109
|
context.report({
|
|
113
|
-
fix:
|
|
114
|
-
return fixer.replaceText(node, importStrings.join("\n"));
|
|
115
|
-
}, "fix"),
|
|
110
|
+
fix: hasCommentInside ? null : (fixer) => fixer.replaceText(node, importStrings.join("\n")),
|
|
116
111
|
messageId: "split-import",
|
|
117
112
|
node
|
|
118
113
|
});
|
|
@@ -144,14 +139,50 @@ function eslintPluginBemoje() {
|
|
|
144
139
|
},
|
|
145
140
|
meta: {
|
|
146
141
|
name: "eslint-plugin-bemoje",
|
|
147
|
-
version: "1.
|
|
142
|
+
version: "2.1.0"
|
|
148
143
|
},
|
|
149
144
|
rules
|
|
150
145
|
};
|
|
151
146
|
}
|
|
152
147
|
__name(eslintPluginBemoje, "eslintPluginBemoje");
|
|
153
148
|
|
|
149
|
+
// src/lib/bemojePlugin.ts
|
|
150
|
+
var bemojePlugin = {
|
|
151
|
+
meta: {
|
|
152
|
+
name: "@bemoje/eslint"
|
|
153
|
+
},
|
|
154
|
+
rules: {
|
|
155
|
+
"no-blank-line-between-comment-and-declaration": noBlankLineBetweenCommentAndDeclaration,
|
|
156
|
+
"split-imports": splitImports
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
// src/configs/enforceNoBlankLineBetweenCommentAndDeclaration.ts
|
|
161
|
+
var enforceNoBlankLineBetweenCommentAndDeclaration = {
|
|
162
|
+
name: "@bemoje/eslint/no-blank-line-between-comment-and-declaration",
|
|
163
|
+
plugins: {
|
|
164
|
+
bemoje: bemojePlugin
|
|
165
|
+
},
|
|
166
|
+
rules: {
|
|
167
|
+
"bemoje/no-blank-line-between-comment-and-declaration": "error"
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// src/configs/enforceSplitImports.ts
|
|
172
|
+
var enforceSplitImports = {
|
|
173
|
+
name: "@bemoje/eslint/split-imports",
|
|
174
|
+
plugins: {
|
|
175
|
+
bemoje: bemojePlugin
|
|
176
|
+
},
|
|
177
|
+
rules: {
|
|
178
|
+
"bemoje/split-imports": "error"
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
exports.bemojePlugin = bemojePlugin;
|
|
154
183
|
exports.createRule = createRule;
|
|
184
|
+
exports.enforceNoBlankLineBetweenCommentAndDeclaration = enforceNoBlankLineBetweenCommentAndDeclaration;
|
|
185
|
+
exports.enforceSplitImports = enforceSplitImports;
|
|
155
186
|
exports.eslintPluginBemoje = eslintPluginBemoje;
|
|
156
187
|
exports.noBlankLineBetweenCommentAndDeclaration = noBlankLineBetweenCommentAndDeclaration;
|
|
157
188
|
exports.splitImports = splitImports;
|
package/lib/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
1
|
+
import { ESLintUtils, TSESLint } from '@typescript-eslint/utils';
|
|
2
2
|
import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -23,7 +23,31 @@ declare function eslintPluginBemoje(): {
|
|
|
23
23
|
configs: {
|
|
24
24
|
readonly recommended: {
|
|
25
25
|
plugins: {
|
|
26
|
-
readonly 'eslint-plugin-bemoje':
|
|
26
|
+
readonly 'eslint-plugin-bemoje': {
|
|
27
|
+
configs: {
|
|
28
|
+
readonly recommended: /*elided*/ any;
|
|
29
|
+
};
|
|
30
|
+
meta: {
|
|
31
|
+
name: string;
|
|
32
|
+
version: string;
|
|
33
|
+
};
|
|
34
|
+
rules: {
|
|
35
|
+
'no-blank-line-between-comment-and-declaration': _typescript_eslint_utils_ts_eslint.RuleModule<"no-blank-line-between-comment-and-declaration", [string], {
|
|
36
|
+
description: string;
|
|
37
|
+
recommended?: boolean;
|
|
38
|
+
requiresTypeChecking?: boolean;
|
|
39
|
+
}, _typescript_eslint_utils_ts_eslint.RuleListener> & {
|
|
40
|
+
name: string;
|
|
41
|
+
};
|
|
42
|
+
'split-imports': _typescript_eslint_utils_ts_eslint.RuleModule<"split-import", readonly unknown[], {
|
|
43
|
+
description: string;
|
|
44
|
+
recommended?: boolean;
|
|
45
|
+
requiresTypeChecking?: boolean;
|
|
46
|
+
}, _typescript_eslint_utils_ts_eslint.RuleListener> & {
|
|
47
|
+
name: string;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
27
51
|
};
|
|
28
52
|
rules: {
|
|
29
53
|
'no-blank-line-between-comment-and-declaration': _typescript_eslint_utils_ts_eslint.RuleModule<"no-blank-line-between-comment-and-declaration", [string], {
|
|
@@ -77,7 +101,7 @@ declare const noBlankLineBetweenCommentAndDeclaration: _typescript_eslint_utils_
|
|
|
77
101
|
};
|
|
78
102
|
|
|
79
103
|
/**
|
|
80
|
-
*
|
|
104
|
+
* Require one import declaration per imported name: `import { a, b } from 'mod'` becomes two declarations.
|
|
81
105
|
*/
|
|
82
106
|
declare const splitImports: _typescript_eslint_utils_ts_eslint.RuleModule<"split-import", readonly unknown[], {
|
|
83
107
|
description: string;
|
|
@@ -87,4 +111,44 @@ declare const splitImports: _typescript_eslint_utils_ts_eslint.RuleModule<"split
|
|
|
87
111
|
name: string;
|
|
88
112
|
};
|
|
89
113
|
|
|
90
|
-
|
|
114
|
+
/**
|
|
115
|
+
* The one plugin object that every ready-made config entry of this package registers, under the name `bemoje`.
|
|
116
|
+
* ESLint accepts the same plugin name in several config entries only when it is the same object each time, so the
|
|
117
|
+
* entries share this one. A rule's full name is `bemoje/<rule>`, for an `eslint-disable` comment.
|
|
118
|
+
*/
|
|
119
|
+
declare const bemojePlugin: {
|
|
120
|
+
meta: {
|
|
121
|
+
name: string;
|
|
122
|
+
};
|
|
123
|
+
rules: {
|
|
124
|
+
'no-blank-line-between-comment-and-declaration': TSESLint.RuleModule<"no-blank-line-between-comment-and-declaration", [string], {
|
|
125
|
+
description: string;
|
|
126
|
+
recommended?: boolean;
|
|
127
|
+
requiresTypeChecking?: boolean;
|
|
128
|
+
}, TSESLint.RuleListener> & {
|
|
129
|
+
name: string;
|
|
130
|
+
};
|
|
131
|
+
'split-imports': TSESLint.RuleModule<"split-import", readonly unknown[], {
|
|
132
|
+
description: string;
|
|
133
|
+
recommended?: boolean;
|
|
134
|
+
requiresTypeChecking?: boolean;
|
|
135
|
+
}, TSESLint.RuleListener> & {
|
|
136
|
+
name: string;
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* A ready-made ESLint flat config entry that turns the rule `no-blank-line-between-comment-and-declaration` on as an
|
|
143
|
+
* error: a block comment over several lines has no blank line between it and the code right after it. Put it in the
|
|
144
|
+
* list a config file exports, with no plugin to register and no rule name to type.
|
|
145
|
+
*/
|
|
146
|
+
declare const enforceNoBlankLineBetweenCommentAndDeclaration: TSESLint.FlatConfig.Config;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* A ready-made ESLint flat config entry that turns the rule `split-imports` on as an error: one import declaration
|
|
150
|
+
* per imported name. Put it in the list a config file exports, with no plugin to register and no rule name to type.
|
|
151
|
+
*/
|
|
152
|
+
declare const enforceSplitImports: TSESLint.FlatConfig.Config;
|
|
153
|
+
|
|
154
|
+
export { bemojePlugin, createRule, enforceNoBlankLineBetweenCommentAndDeclaration, enforceSplitImports, eslintPluginBemoje, noBlankLineBetweenCommentAndDeclaration, splitImports };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
1
|
+
import { ESLintUtils, TSESLint } from '@typescript-eslint/utils';
|
|
2
2
|
import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -23,7 +23,31 @@ declare function eslintPluginBemoje(): {
|
|
|
23
23
|
configs: {
|
|
24
24
|
readonly recommended: {
|
|
25
25
|
plugins: {
|
|
26
|
-
readonly 'eslint-plugin-bemoje':
|
|
26
|
+
readonly 'eslint-plugin-bemoje': {
|
|
27
|
+
configs: {
|
|
28
|
+
readonly recommended: /*elided*/ any;
|
|
29
|
+
};
|
|
30
|
+
meta: {
|
|
31
|
+
name: string;
|
|
32
|
+
version: string;
|
|
33
|
+
};
|
|
34
|
+
rules: {
|
|
35
|
+
'no-blank-line-between-comment-and-declaration': _typescript_eslint_utils_ts_eslint.RuleModule<"no-blank-line-between-comment-and-declaration", [string], {
|
|
36
|
+
description: string;
|
|
37
|
+
recommended?: boolean;
|
|
38
|
+
requiresTypeChecking?: boolean;
|
|
39
|
+
}, _typescript_eslint_utils_ts_eslint.RuleListener> & {
|
|
40
|
+
name: string;
|
|
41
|
+
};
|
|
42
|
+
'split-imports': _typescript_eslint_utils_ts_eslint.RuleModule<"split-import", readonly unknown[], {
|
|
43
|
+
description: string;
|
|
44
|
+
recommended?: boolean;
|
|
45
|
+
requiresTypeChecking?: boolean;
|
|
46
|
+
}, _typescript_eslint_utils_ts_eslint.RuleListener> & {
|
|
47
|
+
name: string;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
27
51
|
};
|
|
28
52
|
rules: {
|
|
29
53
|
'no-blank-line-between-comment-and-declaration': _typescript_eslint_utils_ts_eslint.RuleModule<"no-blank-line-between-comment-and-declaration", [string], {
|
|
@@ -77,7 +101,7 @@ declare const noBlankLineBetweenCommentAndDeclaration: _typescript_eslint_utils_
|
|
|
77
101
|
};
|
|
78
102
|
|
|
79
103
|
/**
|
|
80
|
-
*
|
|
104
|
+
* Require one import declaration per imported name: `import { a, b } from 'mod'` becomes two declarations.
|
|
81
105
|
*/
|
|
82
106
|
declare const splitImports: _typescript_eslint_utils_ts_eslint.RuleModule<"split-import", readonly unknown[], {
|
|
83
107
|
description: string;
|
|
@@ -87,4 +111,44 @@ declare const splitImports: _typescript_eslint_utils_ts_eslint.RuleModule<"split
|
|
|
87
111
|
name: string;
|
|
88
112
|
};
|
|
89
113
|
|
|
90
|
-
|
|
114
|
+
/**
|
|
115
|
+
* The one plugin object that every ready-made config entry of this package registers, under the name `bemoje`.
|
|
116
|
+
* ESLint accepts the same plugin name in several config entries only when it is the same object each time, so the
|
|
117
|
+
* entries share this one. A rule's full name is `bemoje/<rule>`, for an `eslint-disable` comment.
|
|
118
|
+
*/
|
|
119
|
+
declare const bemojePlugin: {
|
|
120
|
+
meta: {
|
|
121
|
+
name: string;
|
|
122
|
+
};
|
|
123
|
+
rules: {
|
|
124
|
+
'no-blank-line-between-comment-and-declaration': TSESLint.RuleModule<"no-blank-line-between-comment-and-declaration", [string], {
|
|
125
|
+
description: string;
|
|
126
|
+
recommended?: boolean;
|
|
127
|
+
requiresTypeChecking?: boolean;
|
|
128
|
+
}, TSESLint.RuleListener> & {
|
|
129
|
+
name: string;
|
|
130
|
+
};
|
|
131
|
+
'split-imports': TSESLint.RuleModule<"split-import", readonly unknown[], {
|
|
132
|
+
description: string;
|
|
133
|
+
recommended?: boolean;
|
|
134
|
+
requiresTypeChecking?: boolean;
|
|
135
|
+
}, TSESLint.RuleListener> & {
|
|
136
|
+
name: string;
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* A ready-made ESLint flat config entry that turns the rule `no-blank-line-between-comment-and-declaration` on as an
|
|
143
|
+
* error: a block comment over several lines has no blank line between it and the code right after it. Put it in the
|
|
144
|
+
* list a config file exports, with no plugin to register and no rule name to type.
|
|
145
|
+
*/
|
|
146
|
+
declare const enforceNoBlankLineBetweenCommentAndDeclaration: TSESLint.FlatConfig.Config;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* A ready-made ESLint flat config entry that turns the rule `split-imports` on as an error: one import declaration
|
|
150
|
+
* per imported name. Put it in the list a config file exports, with no plugin to register and no rule name to type.
|
|
151
|
+
*/
|
|
152
|
+
declare const enforceSplitImports: TSESLint.FlatConfig.Config;
|
|
153
|
+
|
|
154
|
+
export { bemojePlugin, createRule, enforceNoBlankLineBetweenCommentAndDeclaration, enforceSplitImports, eslintPluginBemoje, noBlankLineBetweenCommentAndDeclaration, splitImports };
|
package/lib/index.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { ESLintUtils, AST_TOKEN_TYPES } from '@typescript-eslint/utils';
|
|
1
|
+
import { ESLintUtils, AST_TOKEN_TYPES, AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
2
|
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
-
var createRule = ESLintUtils.RuleCreator((name) => {
|
|
6
|
-
return `https://github.com/bemoje/mono/blob/dev/libs/eslint/README.md#${name}`;
|
|
7
|
-
});
|
|
5
|
+
var createRule = ESLintUtils.RuleCreator((name) => `https://github.com/bemoje/mono/blob/dev/libs/eslint/README.md#${name}`);
|
|
8
6
|
var noBlankLineBetweenCommentAndDeclaration = createRule({
|
|
9
7
|
name: "no-blank-line-between-comment-and-declaration",
|
|
10
8
|
meta: {
|
|
@@ -32,32 +30,35 @@ var noBlankLineBetweenCommentAndDeclaration = createRule({
|
|
|
32
30
|
if (Math.abs(comment.loc.start.line - comment.loc.end.line) < 2) {
|
|
33
31
|
return;
|
|
34
32
|
}
|
|
35
|
-
const nextToken = context.sourceCode.getTokenAfter(comment
|
|
33
|
+
const nextToken = context.sourceCode.getTokenAfter(comment, {
|
|
34
|
+
includeComments: true
|
|
35
|
+
});
|
|
36
36
|
if (!nextToken) {
|
|
37
37
|
return;
|
|
38
38
|
}
|
|
39
|
+
if (nextToken.type === AST_TOKEN_TYPES.Block || nextToken.type === AST_TOKEN_TYPES.Line) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
39
42
|
const actualNextTokenStartLine = nextToken.loc.start.line;
|
|
40
43
|
const expectedTokenStartLine = comment.loc.end.line + 1;
|
|
41
44
|
if (!(actualNextTokenStartLine > expectedTokenStartLine)) {
|
|
42
45
|
return;
|
|
43
46
|
}
|
|
44
|
-
const lineBeforeNextToken = context.sourceCode.lines[nextToken.loc.start.line - 1 - 1];
|
|
45
|
-
const hasLineCommentBetween = lineBeforeNextToken.trim().startsWith("//");
|
|
46
|
-
if (hasLineCommentBetween) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
47
|
return context.report({
|
|
50
|
-
|
|
48
|
+
// Where the gap is: from the end of the comment to the start of the code after it
|
|
49
|
+
loc: {
|
|
50
|
+
start: comment.loc.end,
|
|
51
|
+
end: nextToken.loc.start
|
|
52
|
+
},
|
|
51
53
|
messageId: "no-blank-line-between-comment-and-declaration",
|
|
52
54
|
fix(fixer) {
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
const rangeCodeFixed = rangeCode.replaceAll(/\*\/\n\n+/gi, "*/\n");
|
|
55
|
+
const gap = context.sourceCode.text.slice(comment.range[1], nextToken.range[0]);
|
|
56
|
+
const lineBreak = gap.includes("\r\n") ? "\r\n" : "\n";
|
|
57
|
+
const indentation = gap.slice(gap.lastIndexOf("\n") + 1);
|
|
57
58
|
return fixer.replaceTextRange([
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
],
|
|
59
|
+
comment.range[1],
|
|
60
|
+
nextToken.range[0]
|
|
61
|
+
], lineBreak + indentation);
|
|
61
62
|
}
|
|
62
63
|
});
|
|
63
64
|
});
|
|
@@ -65,8 +66,6 @@ var noBlankLineBetweenCommentAndDeclaration = createRule({
|
|
|
65
66
|
};
|
|
66
67
|
}
|
|
67
68
|
});
|
|
68
|
-
|
|
69
|
-
// src/rules/splitImports.ts
|
|
70
69
|
var splitImports = createRule({
|
|
71
70
|
name: "split-imports",
|
|
72
71
|
meta: {
|
|
@@ -85,32 +84,28 @@ var splitImports = createRule({
|
|
|
85
84
|
ImportDeclaration(node) {
|
|
86
85
|
if (node.specifiers.length > 1) {
|
|
87
86
|
const sourceCode = context.sourceCode;
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
].join(" ");
|
|
109
|
-
});
|
|
87
|
+
const fromModule = sourceCode.getText(node).slice(node.source.range[0] - node.range[0]);
|
|
88
|
+
const importClause = /* @__PURE__ */ __name((specifier) => {
|
|
89
|
+
const local = specifier.local.name;
|
|
90
|
+
if (specifier.type === AST_NODE_TYPES.ImportDefaultSpecifier) {
|
|
91
|
+
return local;
|
|
92
|
+
}
|
|
93
|
+
if (specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier) {
|
|
94
|
+
return `* as ${local}`;
|
|
95
|
+
}
|
|
96
|
+
const imported = sourceCode.getText(specifier.imported);
|
|
97
|
+
return imported === local ? `{ ${local} }` : `{ ${imported} as ${local} }`;
|
|
98
|
+
}, "importClause");
|
|
99
|
+
const isTypeImport = /* @__PURE__ */ __name((specifier) => node.importKind === "type" || specifier.type === AST_NODE_TYPES.ImportSpecifier && specifier.importKind === "type", "isTypeImport");
|
|
100
|
+
const importStrings = node.specifiers.map((specifier) => [
|
|
101
|
+
isTypeImport(specifier) ? "import type" : "import",
|
|
102
|
+
importClause(specifier),
|
|
103
|
+
"from",
|
|
104
|
+
fromModule
|
|
105
|
+
].join(" "));
|
|
106
|
+
const hasCommentInside = sourceCode.getCommentsInside(node).length > 0;
|
|
110
107
|
context.report({
|
|
111
|
-
fix:
|
|
112
|
-
return fixer.replaceText(node, importStrings.join("\n"));
|
|
113
|
-
}, "fix"),
|
|
108
|
+
fix: hasCommentInside ? null : (fixer) => fixer.replaceText(node, importStrings.join("\n")),
|
|
114
109
|
messageId: "split-import",
|
|
115
110
|
node
|
|
116
111
|
});
|
|
@@ -142,11 +137,44 @@ function eslintPluginBemoje() {
|
|
|
142
137
|
},
|
|
143
138
|
meta: {
|
|
144
139
|
name: "eslint-plugin-bemoje",
|
|
145
|
-
version: "1.
|
|
140
|
+
version: "2.1.0"
|
|
146
141
|
},
|
|
147
142
|
rules
|
|
148
143
|
};
|
|
149
144
|
}
|
|
150
145
|
__name(eslintPluginBemoje, "eslintPluginBemoje");
|
|
151
146
|
|
|
152
|
-
|
|
147
|
+
// src/lib/bemojePlugin.ts
|
|
148
|
+
var bemojePlugin = {
|
|
149
|
+
meta: {
|
|
150
|
+
name: "@bemoje/eslint"
|
|
151
|
+
},
|
|
152
|
+
rules: {
|
|
153
|
+
"no-blank-line-between-comment-and-declaration": noBlankLineBetweenCommentAndDeclaration,
|
|
154
|
+
"split-imports": splitImports
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// src/configs/enforceNoBlankLineBetweenCommentAndDeclaration.ts
|
|
159
|
+
var enforceNoBlankLineBetweenCommentAndDeclaration = {
|
|
160
|
+
name: "@bemoje/eslint/no-blank-line-between-comment-and-declaration",
|
|
161
|
+
plugins: {
|
|
162
|
+
bemoje: bemojePlugin
|
|
163
|
+
},
|
|
164
|
+
rules: {
|
|
165
|
+
"bemoje/no-blank-line-between-comment-and-declaration": "error"
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// src/configs/enforceSplitImports.ts
|
|
170
|
+
var enforceSplitImports = {
|
|
171
|
+
name: "@bemoje/eslint/split-imports",
|
|
172
|
+
plugins: {
|
|
173
|
+
bemoje: bemojePlugin
|
|
174
|
+
},
|
|
175
|
+
rules: {
|
|
176
|
+
"bemoje/split-imports": "error"
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export { bemojePlugin, createRule, enforceNoBlankLineBetweenCommentAndDeclaration, enforceSplitImports, eslintPluginBemoje, noBlankLineBetweenCommentAndDeclaration, splitImports };
|