@bemoje/eslint 2.0.0 → 2.1.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/README.md CHANGED
@@ -30,6 +30,22 @@ npm install --save-dev @bemoje/eslint
30
30
 
31
31
  ## Usage
32
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
+ The entries fit ESLint's own config type, so a typed `eslint.config.ts` (`const config: Linter.Config[] = [...]`) takes them with no cast.
46
+
47
+ A rule's full name, for an `eslint-disable` comment, is `bemoje/<rule>`: `bemoje/split-imports`, `bemoje/no-blank-line-between-comment-and-declaration`.
48
+
33
49
  ### Plugin Setup (Flat Config)
34
50
 
35
51
  ```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
- node: program,
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 start = comment.range[0];
56
- const end = nextToken.range[1];
57
- const rangeCode = context.sourceCode.text.slice(start, end);
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
- start,
61
- end
62
- ], rangeCodeFixed);
61
+ comment.range[1],
62
+ nextToken.range[0]
63
+ ], lineBreak + indentation);
63
64
  }
64
65
  });
65
66
  });
@@ -68,7 +69,8 @@ var noBlankLineBetweenCommentAndDeclaration = createRule({
68
69
  }
69
70
  });
70
71
 
71
- // src/rules/splitImports.ts
72
+ // src/lib/forEslint.ts
73
+ var ruleForEslint = /* @__PURE__ */ __name((rule) => rule, "ruleForEslint");
72
74
  var splitImports = createRule({
73
75
  name: "split-imports",
74
76
  meta: {
@@ -87,32 +89,28 @@ var splitImports = createRule({
87
89
  ImportDeclaration(node) {
88
90
  if (node.specifiers.length > 1) {
89
91
  const sourceCode = context.sourceCode;
90
- const insideBraces = sourceCode.getText(node).match(/{([^{}]+)}/)?.[1]?.split(",").map((s) => {
91
- return s.trim();
92
- }).filter((s) => {
93
- return !!s;
94
- }) ?? [];
95
- const maybeWrapInBraces = /* @__PURE__ */ __name((code) => {
96
- return insideBraces.includes(code) ? `{ ${code} }` : code;
97
- }, "maybeWrapInBraces");
98
- const isType = /* @__PURE__ */ __name((code) => {
99
- return node.importKind === "type" || code.startsWith("type ");
100
- }, "isType");
101
- const stripTypeKeyword = /* @__PURE__ */ __name((code) => {
102
- return code.replace(/\btype\s+\b/, "").trim();
103
- }, "stripTypeKeyword");
104
- const importStrings = node.specifiers.map((specifier) => {
105
- const code = sourceCode.getText(specifier).trim();
106
- return [
107
- isType(code) ? "import type" : "import",
108
- maybeWrapInBraces(stripTypeKeyword(code)),
109
- `from "${node.source.value}";`
110
- ].join(" ");
111
- });
92
+ const fromModule = sourceCode.getText(node).slice(node.source.range[0] - node.range[0]);
93
+ const importClause = /* @__PURE__ */ __name((specifier) => {
94
+ const local = specifier.local.name;
95
+ if (specifier.type === utils.AST_NODE_TYPES.ImportDefaultSpecifier) {
96
+ return local;
97
+ }
98
+ if (specifier.type === utils.AST_NODE_TYPES.ImportNamespaceSpecifier) {
99
+ return `* as ${local}`;
100
+ }
101
+ const imported = sourceCode.getText(specifier.imported);
102
+ return imported === local ? `{ ${local} }` : `{ ${imported} as ${local} }`;
103
+ }, "importClause");
104
+ const isTypeImport = /* @__PURE__ */ __name((specifier) => node.importKind === "type" || specifier.type === utils.AST_NODE_TYPES.ImportSpecifier && specifier.importKind === "type", "isTypeImport");
105
+ const importStrings = node.specifiers.map((specifier) => [
106
+ isTypeImport(specifier) ? "import type" : "import",
107
+ importClause(specifier),
108
+ "from",
109
+ fromModule
110
+ ].join(" "));
111
+ const hasCommentInside = sourceCode.getCommentsInside(node).length > 0;
112
112
  context.report({
113
- fix: /* @__PURE__ */ __name((fixer) => {
114
- return fixer.replaceText(node, importStrings.join("\n"));
115
- }, "fix"),
113
+ fix: hasCommentInside ? null : (fixer) => fixer.replaceText(node, importStrings.join("\n")),
116
114
  messageId: "split-import",
117
115
  node
118
116
  });
@@ -122,12 +120,20 @@ var splitImports = createRule({
122
120
  }
123
121
  });
124
122
 
123
+ // src/lib/bemojePlugin.ts
124
+ var bemojePlugin = {
125
+ meta: {
126
+ name: "@bemoje/eslint"
127
+ },
128
+ rules: {
129
+ "no-blank-line-between-comment-and-declaration": ruleForEslint(noBlankLineBetweenCommentAndDeclaration),
130
+ "split-imports": ruleForEslint(splitImports)
131
+ }
132
+ };
133
+
125
134
  // src/lib/eslintPluginBemoje.ts
126
135
  function eslintPluginBemoje() {
127
- const rules = {
128
- "no-blank-line-between-comment-and-declaration": noBlankLineBetweenCommentAndDeclaration,
129
- "split-imports": splitImports
130
- };
136
+ const rules = bemojePlugin.rules;
131
137
  const plugins = {
132
138
  get "eslint-plugin-bemoje"() {
133
139
  return eslintPluginBemoje();
@@ -138,20 +144,48 @@ function eslintPluginBemoje() {
138
144
  get recommended() {
139
145
  return {
140
146
  plugins,
141
- rules
147
+ rules: Object.fromEntries(Object.keys(rules).map((name) => [
148
+ `eslint-plugin-bemoje/${name}`,
149
+ "error"
150
+ ]))
142
151
  };
143
152
  }
144
153
  },
145
154
  meta: {
146
155
  name: "eslint-plugin-bemoje",
147
- version: "1.2.0"
156
+ version: "2.1.1"
148
157
  },
149
158
  rules
150
159
  };
151
160
  }
152
161
  __name(eslintPluginBemoje, "eslintPluginBemoje");
153
162
 
163
+ // src/configs/enforceNoBlankLineBetweenCommentAndDeclaration.ts
164
+ var enforceNoBlankLineBetweenCommentAndDeclaration = {
165
+ name: "@bemoje/eslint/no-blank-line-between-comment-and-declaration",
166
+ plugins: {
167
+ bemoje: bemojePlugin
168
+ },
169
+ rules: {
170
+ "bemoje/no-blank-line-between-comment-and-declaration": "error"
171
+ }
172
+ };
173
+
174
+ // src/configs/enforceSplitImports.ts
175
+ var enforceSplitImports = {
176
+ name: "@bemoje/eslint/split-imports",
177
+ plugins: {
178
+ bemoje: bemojePlugin
179
+ },
180
+ rules: {
181
+ "bemoje/split-imports": "error"
182
+ }
183
+ };
184
+
185
+ exports.bemojePlugin = bemojePlugin;
154
186
  exports.createRule = createRule;
187
+ exports.enforceNoBlankLineBetweenCommentAndDeclaration = enforceNoBlankLineBetweenCommentAndDeclaration;
188
+ exports.enforceSplitImports = enforceSplitImports;
155
189
  exports.eslintPluginBemoje = eslintPluginBemoje;
156
190
  exports.noBlankLineBetweenCommentAndDeclaration = noBlankLineBetweenCommentAndDeclaration;
157
191
  exports.splitImports = splitImports;
package/lib/index.d.cts CHANGED
@@ -17,53 +17,47 @@ declare const createRule: <Options extends readonly unknown[], MessageIds extend
17
17
  };
18
18
 
19
19
  /**
20
- * ESLint plugin for Bemoje projects.
20
+ * The types this package's plugin and config entries are offered under. They say what ESLint's own config type
21
+ * (`Linter.Config`) asks for and nothing more, so an entry goes into a typed config with no cast. A rule keeps its
22
+ * full typescript-eslint type where it is declared: that type does not fit ESLint's, which is why presets written
23
+ * with typescript-eslint usually need `as Linter.Config[]` where they are used. ESLint itself is not imported here,
24
+ * so the package does not depend on a version of it.
21
25
  */
22
- declare function eslintPluginBemoje(): {
23
- configs: {
24
- readonly recommended: {
25
- plugins: {
26
- readonly 'eslint-plugin-bemoje': any;
27
- };
28
- rules: {
29
- 'no-blank-line-between-comment-and-declaration': _typescript_eslint_utils_ts_eslint.RuleModule<"no-blank-line-between-comment-and-declaration", [string], {
30
- description: string;
31
- recommended?: boolean;
32
- requiresTypeChecking?: boolean;
33
- }, _typescript_eslint_utils_ts_eslint.RuleListener> & {
34
- name: string;
35
- };
36
- 'split-imports': _typescript_eslint_utils_ts_eslint.RuleModule<"split-import", readonly unknown[], {
37
- description: string;
38
- recommended?: boolean;
39
- requiresTypeChecking?: boolean;
40
- }, _typescript_eslint_utils_ts_eslint.RuleListener> & {
41
- name: string;
42
- };
43
- };
44
- };
45
- };
26
+ /** A rule as ESLint's config type asks for it: what `create` is given, and what its listeners take, is ESLint's own. */
27
+ interface RuleForEslint {
28
+ meta?: object;
29
+ create: (context: never) => Record<string, ((...args: any[]) => void) | undefined>;
30
+ }
31
+ /** A plugin as ESLint's config type asks for it. */
32
+ interface PluginForEslint {
46
33
  meta: {
47
34
  name: string;
48
- version: string;
35
+ version?: string;
49
36
  };
50
- rules: {
51
- 'no-blank-line-between-comment-and-declaration': _typescript_eslint_utils_ts_eslint.RuleModule<"no-blank-line-between-comment-and-declaration", [string], {
52
- description: string;
53
- recommended?: boolean;
54
- requiresTypeChecking?: boolean;
55
- }, _typescript_eslint_utils_ts_eslint.RuleListener> & {
56
- name: string;
57
- };
58
- 'split-imports': _typescript_eslint_utils_ts_eslint.RuleModule<"split-import", readonly unknown[], {
59
- description: string;
60
- recommended?: boolean;
61
- requiresTypeChecking?: boolean;
62
- }, _typescript_eslint_utils_ts_eslint.RuleListener> & {
63
- name: string;
37
+ rules: Record<string, RuleForEslint>;
38
+ }
39
+ /** One entry of a flat config that registers a plugin and turns rules of it on. */
40
+ interface ConfigForEslint {
41
+ name?: string;
42
+ plugins: Record<string, PluginForEslint>;
43
+ rules: Record<string, 'error'>;
44
+ }
45
+
46
+ /** What `eslintPluginBemoje()` returns: the plugin, and a config that registers it and turns every rule on. */
47
+ interface EslintPluginBemoje extends PluginForEslint {
48
+ configs: {
49
+ recommended: {
50
+ plugins: {
51
+ 'eslint-plugin-bemoje': EslintPluginBemoje;
52
+ };
53
+ rules: Record<string, 'error'>;
64
54
  };
65
55
  };
66
- };
56
+ }
57
+ /**
58
+ * ESLint plugin for Bemoje projects.
59
+ */
60
+ declare function eslintPluginBemoje(): EslintPluginBemoje;
67
61
 
68
62
  /**
69
63
  * Enforce no blank lines between block comments and the next declaration.
@@ -77,7 +71,7 @@ declare const noBlankLineBetweenCommentAndDeclaration: _typescript_eslint_utils_
77
71
  };
78
72
 
79
73
  /**
80
- * Enforce no blank lines between block comments and the next declaration.
74
+ * Require one import declaration per imported name: `import { a, b } from 'mod'` becomes two declarations.
81
75
  */
82
76
  declare const splitImports: _typescript_eslint_utils_ts_eslint.RuleModule<"split-import", readonly unknown[], {
83
77
  description: string;
@@ -87,4 +81,24 @@ declare const splitImports: _typescript_eslint_utils_ts_eslint.RuleModule<"split
87
81
  name: string;
88
82
  };
89
83
 
90
- export { createRule, eslintPluginBemoje, noBlankLineBetweenCommentAndDeclaration, splitImports };
84
+ /**
85
+ * The one plugin object that every ready-made config entry of this package registers, under the name `bemoje`.
86
+ * ESLint accepts the same plugin name in several config entries only when it is the same object each time, so the
87
+ * entries share this one. A rule's full name is `bemoje/<rule>`, for an `eslint-disable` comment.
88
+ */
89
+ declare const bemojePlugin: PluginForEslint;
90
+
91
+ /**
92
+ * A ready-made ESLint flat config entry that turns the rule `no-blank-line-between-comment-and-declaration` on as an
93
+ * error: a block comment over several lines has no blank line between it and the code right after it. Put it in the
94
+ * list a config file exports, with no plugin to register and no rule name to type.
95
+ */
96
+ declare const enforceNoBlankLineBetweenCommentAndDeclaration: ConfigForEslint;
97
+
98
+ /**
99
+ * A ready-made ESLint flat config entry that turns the rule `split-imports` on as an error: one import declaration
100
+ * per imported name. Put it in the list a config file exports, with no plugin to register and no rule name to type.
101
+ */
102
+ declare const enforceSplitImports: ConfigForEslint;
103
+
104
+ export { bemojePlugin, createRule, enforceNoBlankLineBetweenCommentAndDeclaration, enforceSplitImports, eslintPluginBemoje, noBlankLineBetweenCommentAndDeclaration, splitImports };
package/lib/index.d.ts CHANGED
@@ -17,53 +17,47 @@ declare const createRule: <Options extends readonly unknown[], MessageIds extend
17
17
  };
18
18
 
19
19
  /**
20
- * ESLint plugin for Bemoje projects.
20
+ * The types this package's plugin and config entries are offered under. They say what ESLint's own config type
21
+ * (`Linter.Config`) asks for and nothing more, so an entry goes into a typed config with no cast. A rule keeps its
22
+ * full typescript-eslint type where it is declared: that type does not fit ESLint's, which is why presets written
23
+ * with typescript-eslint usually need `as Linter.Config[]` where they are used. ESLint itself is not imported here,
24
+ * so the package does not depend on a version of it.
21
25
  */
22
- declare function eslintPluginBemoje(): {
23
- configs: {
24
- readonly recommended: {
25
- plugins: {
26
- readonly 'eslint-plugin-bemoje': any;
27
- };
28
- rules: {
29
- 'no-blank-line-between-comment-and-declaration': _typescript_eslint_utils_ts_eslint.RuleModule<"no-blank-line-between-comment-and-declaration", [string], {
30
- description: string;
31
- recommended?: boolean;
32
- requiresTypeChecking?: boolean;
33
- }, _typescript_eslint_utils_ts_eslint.RuleListener> & {
34
- name: string;
35
- };
36
- 'split-imports': _typescript_eslint_utils_ts_eslint.RuleModule<"split-import", readonly unknown[], {
37
- description: string;
38
- recommended?: boolean;
39
- requiresTypeChecking?: boolean;
40
- }, _typescript_eslint_utils_ts_eslint.RuleListener> & {
41
- name: string;
42
- };
43
- };
44
- };
45
- };
26
+ /** A rule as ESLint's config type asks for it: what `create` is given, and what its listeners take, is ESLint's own. */
27
+ interface RuleForEslint {
28
+ meta?: object;
29
+ create: (context: never) => Record<string, ((...args: any[]) => void) | undefined>;
30
+ }
31
+ /** A plugin as ESLint's config type asks for it. */
32
+ interface PluginForEslint {
46
33
  meta: {
47
34
  name: string;
48
- version: string;
35
+ version?: string;
49
36
  };
50
- rules: {
51
- 'no-blank-line-between-comment-and-declaration': _typescript_eslint_utils_ts_eslint.RuleModule<"no-blank-line-between-comment-and-declaration", [string], {
52
- description: string;
53
- recommended?: boolean;
54
- requiresTypeChecking?: boolean;
55
- }, _typescript_eslint_utils_ts_eslint.RuleListener> & {
56
- name: string;
57
- };
58
- 'split-imports': _typescript_eslint_utils_ts_eslint.RuleModule<"split-import", readonly unknown[], {
59
- description: string;
60
- recommended?: boolean;
61
- requiresTypeChecking?: boolean;
62
- }, _typescript_eslint_utils_ts_eslint.RuleListener> & {
63
- name: string;
37
+ rules: Record<string, RuleForEslint>;
38
+ }
39
+ /** One entry of a flat config that registers a plugin and turns rules of it on. */
40
+ interface ConfigForEslint {
41
+ name?: string;
42
+ plugins: Record<string, PluginForEslint>;
43
+ rules: Record<string, 'error'>;
44
+ }
45
+
46
+ /** What `eslintPluginBemoje()` returns: the plugin, and a config that registers it and turns every rule on. */
47
+ interface EslintPluginBemoje extends PluginForEslint {
48
+ configs: {
49
+ recommended: {
50
+ plugins: {
51
+ 'eslint-plugin-bemoje': EslintPluginBemoje;
52
+ };
53
+ rules: Record<string, 'error'>;
64
54
  };
65
55
  };
66
- };
56
+ }
57
+ /**
58
+ * ESLint plugin for Bemoje projects.
59
+ */
60
+ declare function eslintPluginBemoje(): EslintPluginBemoje;
67
61
 
68
62
  /**
69
63
  * Enforce no blank lines between block comments and the next declaration.
@@ -77,7 +71,7 @@ declare const noBlankLineBetweenCommentAndDeclaration: _typescript_eslint_utils_
77
71
  };
78
72
 
79
73
  /**
80
- * Enforce no blank lines between block comments and the next declaration.
74
+ * Require one import declaration per imported name: `import { a, b } from 'mod'` becomes two declarations.
81
75
  */
82
76
  declare const splitImports: _typescript_eslint_utils_ts_eslint.RuleModule<"split-import", readonly unknown[], {
83
77
  description: string;
@@ -87,4 +81,24 @@ declare const splitImports: _typescript_eslint_utils_ts_eslint.RuleModule<"split
87
81
  name: string;
88
82
  };
89
83
 
90
- export { createRule, eslintPluginBemoje, noBlankLineBetweenCommentAndDeclaration, splitImports };
84
+ /**
85
+ * The one plugin object that every ready-made config entry of this package registers, under the name `bemoje`.
86
+ * ESLint accepts the same plugin name in several config entries only when it is the same object each time, so the
87
+ * entries share this one. A rule's full name is `bemoje/<rule>`, for an `eslint-disable` comment.
88
+ */
89
+ declare const bemojePlugin: PluginForEslint;
90
+
91
+ /**
92
+ * A ready-made ESLint flat config entry that turns the rule `no-blank-line-between-comment-and-declaration` on as an
93
+ * error: a block comment over several lines has no blank line between it and the code right after it. Put it in the
94
+ * list a config file exports, with no plugin to register and no rule name to type.
95
+ */
96
+ declare const enforceNoBlankLineBetweenCommentAndDeclaration: ConfigForEslint;
97
+
98
+ /**
99
+ * A ready-made ESLint flat config entry that turns the rule `split-imports` on as an error: one import declaration
100
+ * per imported name. Put it in the list a config file exports, with no plugin to register and no rule name to type.
101
+ */
102
+ declare const enforceSplitImports: ConfigForEslint;
103
+
104
+ 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
- node: program,
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 start = comment.range[0];
54
- const end = nextToken.range[1];
55
- const rangeCode = context.sourceCode.text.slice(start, end);
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
- start,
59
- end
60
- ], rangeCodeFixed);
59
+ comment.range[1],
60
+ nextToken.range[0]
61
+ ], lineBreak + indentation);
61
62
  }
62
63
  });
63
64
  });
@@ -66,7 +67,8 @@ var noBlankLineBetweenCommentAndDeclaration = createRule({
66
67
  }
67
68
  });
68
69
 
69
- // src/rules/splitImports.ts
70
+ // src/lib/forEslint.ts
71
+ var ruleForEslint = /* @__PURE__ */ __name((rule) => rule, "ruleForEslint");
70
72
  var splitImports = createRule({
71
73
  name: "split-imports",
72
74
  meta: {
@@ -85,32 +87,28 @@ var splitImports = createRule({
85
87
  ImportDeclaration(node) {
86
88
  if (node.specifiers.length > 1) {
87
89
  const sourceCode = context.sourceCode;
88
- const insideBraces = sourceCode.getText(node).match(/{([^{}]+)}/)?.[1]?.split(",").map((s) => {
89
- return s.trim();
90
- }).filter((s) => {
91
- return !!s;
92
- }) ?? [];
93
- const maybeWrapInBraces = /* @__PURE__ */ __name((code) => {
94
- return insideBraces.includes(code) ? `{ ${code} }` : code;
95
- }, "maybeWrapInBraces");
96
- const isType = /* @__PURE__ */ __name((code) => {
97
- return node.importKind === "type" || code.startsWith("type ");
98
- }, "isType");
99
- const stripTypeKeyword = /* @__PURE__ */ __name((code) => {
100
- return code.replace(/\btype\s+\b/, "").trim();
101
- }, "stripTypeKeyword");
102
- const importStrings = node.specifiers.map((specifier) => {
103
- const code = sourceCode.getText(specifier).trim();
104
- return [
105
- isType(code) ? "import type" : "import",
106
- maybeWrapInBraces(stripTypeKeyword(code)),
107
- `from "${node.source.value}";`
108
- ].join(" ");
109
- });
90
+ const fromModule = sourceCode.getText(node).slice(node.source.range[0] - node.range[0]);
91
+ const importClause = /* @__PURE__ */ __name((specifier) => {
92
+ const local = specifier.local.name;
93
+ if (specifier.type === AST_NODE_TYPES.ImportDefaultSpecifier) {
94
+ return local;
95
+ }
96
+ if (specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier) {
97
+ return `* as ${local}`;
98
+ }
99
+ const imported = sourceCode.getText(specifier.imported);
100
+ return imported === local ? `{ ${local} }` : `{ ${imported} as ${local} }`;
101
+ }, "importClause");
102
+ const isTypeImport = /* @__PURE__ */ __name((specifier) => node.importKind === "type" || specifier.type === AST_NODE_TYPES.ImportSpecifier && specifier.importKind === "type", "isTypeImport");
103
+ const importStrings = node.specifiers.map((specifier) => [
104
+ isTypeImport(specifier) ? "import type" : "import",
105
+ importClause(specifier),
106
+ "from",
107
+ fromModule
108
+ ].join(" "));
109
+ const hasCommentInside = sourceCode.getCommentsInside(node).length > 0;
110
110
  context.report({
111
- fix: /* @__PURE__ */ __name((fixer) => {
112
- return fixer.replaceText(node, importStrings.join("\n"));
113
- }, "fix"),
111
+ fix: hasCommentInside ? null : (fixer) => fixer.replaceText(node, importStrings.join("\n")),
114
112
  messageId: "split-import",
115
113
  node
116
114
  });
@@ -120,12 +118,20 @@ var splitImports = createRule({
120
118
  }
121
119
  });
122
120
 
121
+ // src/lib/bemojePlugin.ts
122
+ var bemojePlugin = {
123
+ meta: {
124
+ name: "@bemoje/eslint"
125
+ },
126
+ rules: {
127
+ "no-blank-line-between-comment-and-declaration": ruleForEslint(noBlankLineBetweenCommentAndDeclaration),
128
+ "split-imports": ruleForEslint(splitImports)
129
+ }
130
+ };
131
+
123
132
  // src/lib/eslintPluginBemoje.ts
124
133
  function eslintPluginBemoje() {
125
- const rules = {
126
- "no-blank-line-between-comment-and-declaration": noBlankLineBetweenCommentAndDeclaration,
127
- "split-imports": splitImports
128
- };
134
+ const rules = bemojePlugin.rules;
129
135
  const plugins = {
130
136
  get "eslint-plugin-bemoje"() {
131
137
  return eslintPluginBemoje();
@@ -136,17 +142,42 @@ function eslintPluginBemoje() {
136
142
  get recommended() {
137
143
  return {
138
144
  plugins,
139
- rules
145
+ rules: Object.fromEntries(Object.keys(rules).map((name) => [
146
+ `eslint-plugin-bemoje/${name}`,
147
+ "error"
148
+ ]))
140
149
  };
141
150
  }
142
151
  },
143
152
  meta: {
144
153
  name: "eslint-plugin-bemoje",
145
- version: "1.2.0"
154
+ version: "2.1.1"
146
155
  },
147
156
  rules
148
157
  };
149
158
  }
150
159
  __name(eslintPluginBemoje, "eslintPluginBemoje");
151
160
 
152
- export { createRule, eslintPluginBemoje, noBlankLineBetweenCommentAndDeclaration, splitImports };
161
+ // src/configs/enforceNoBlankLineBetweenCommentAndDeclaration.ts
162
+ var enforceNoBlankLineBetweenCommentAndDeclaration = {
163
+ name: "@bemoje/eslint/no-blank-line-between-comment-and-declaration",
164
+ plugins: {
165
+ bemoje: bemojePlugin
166
+ },
167
+ rules: {
168
+ "bemoje/no-blank-line-between-comment-and-declaration": "error"
169
+ }
170
+ };
171
+
172
+ // src/configs/enforceSplitImports.ts
173
+ var enforceSplitImports = {
174
+ name: "@bemoje/eslint/split-imports",
175
+ plugins: {
176
+ bemoje: bemojePlugin
177
+ },
178
+ rules: {
179
+ "bemoje/split-imports": "error"
180
+ }
181
+ };
182
+
183
+ export { bemojePlugin, createRule, enforceNoBlankLineBetweenCommentAndDeclaration, enforceSplitImports, eslintPluginBemoje, noBlankLineBetweenCommentAndDeclaration, splitImports };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bemoje/eslint",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "Custom ESLint plugin and rule utilities for consistent code style in TypeScript projects.",
5
5
  "type": "module",
6
6
  "sideEffects": false,