@discourse/lint-configs 2.10.0 → 2.11.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.
@@ -18,12 +18,11 @@ export default {
18
18
  docs: {
19
19
  description:
20
20
  "replace deprecated resolver 'lookup' calls and modifyClass arguments with modern equivalents",
21
- category: "Best Practices",
22
- recommended: true,
23
21
  },
24
22
  fixable: "code",
25
23
  schema: [], // no options
26
24
  },
25
+
27
26
  create(context) {
28
27
  return {
29
28
  CallExpression(node) {
@@ -4,12 +4,11 @@ export default {
4
4
  docs: {
5
5
  description:
6
6
  "disallow imports from 'discourse-common' and replace with modern equivalents",
7
- category: "Best Practices",
8
- recommended: false,
9
7
  },
10
8
  fixable: "code",
11
9
  schema: [], // no options
12
10
  },
11
+
13
12
  create(context) {
14
13
  return {
15
14
  ImportDeclaration(node) {
@@ -6,12 +6,11 @@ export default {
6
6
  docs: {
7
7
  description:
8
8
  "disallow imports from 'i18n' and replace with 'discourse-i18n'",
9
- category: "Best Practices",
10
- recommended: false,
11
9
  },
12
10
  fixable: "code",
13
11
  schema: [], // no options
14
12
  },
13
+
15
14
  create(context) {
16
15
  return {
17
16
  ImportDeclaration(node) {
@@ -5,12 +5,11 @@ export default {
5
5
  type: "suggestion",
6
6
  docs: {
7
7
  description: "Use i18n(...) instead of 'I18n.t(...)'.",
8
- category: "Best Practices",
9
- recommended: false,
10
8
  },
11
9
  fixable: "code",
12
10
  schema: [], // no options
13
11
  },
12
+
14
13
  create(context) {
15
14
  const sourceCode = context.sourceCode ?? context.getSourceCode();
16
15
  let alreadyFixedImport = false;
@@ -53,7 +52,7 @@ export default {
53
52
  const fixes = [];
54
53
 
55
54
  // Replace I18n.t with i18n
56
- fixes.push(fixer.replaceText(node, `i18n`));
55
+ fixes.push(fixer.replaceText(node, "i18n"));
57
56
 
58
57
  if (!alreadyFixedImport) {
59
58
  const importDeclaration = i18nDefaultImport.node.parent;
@@ -0,0 +1,91 @@
1
+ import {
2
+ findFirstConsecutiveTokenBefore,
3
+ findLastConsecutiveTokenAfter,
4
+ getBoundaryTokens,
5
+ hasTokenOrCommentBetween,
6
+ } from "./utils/tokens.mjs";
7
+
8
+ function findLastIndexOfType(nodes, type) {
9
+ return nodes.findLastIndex((node) => node.type === type);
10
+ }
11
+
12
+ export default {
13
+ meta: {
14
+ type: "layout",
15
+ docs: {
16
+ description: "Require an empty line after the imports block",
17
+ },
18
+ fixable: "whitespace",
19
+ schema: [], // no options
20
+ },
21
+
22
+ create(context) {
23
+ const sourceCode = context.sourceCode;
24
+
25
+ return {
26
+ Program(node) {
27
+ const body = node.body;
28
+ const index = findLastIndexOfType(body, "ImportDeclaration");
29
+
30
+ if (index === -1) {
31
+ // No imports
32
+ return;
33
+ }
34
+
35
+ if (!body[index + 1]) {
36
+ // Nothing after imports
37
+ return;
38
+ }
39
+
40
+ const { curLast, nextFirst } = getBoundaryTokens(
41
+ sourceCode,
42
+ body[index],
43
+ body[index + 1]
44
+ );
45
+
46
+ const beforePadding = findLastConsecutiveTokenAfter(
47
+ sourceCode,
48
+ curLast,
49
+ nextFirst,
50
+ 1
51
+ );
52
+ const afterPadding = findFirstConsecutiveTokenBefore(
53
+ sourceCode,
54
+ nextFirst,
55
+ curLast,
56
+ 1
57
+ );
58
+ const isPadded =
59
+ afterPadding.loc.start.line - beforePadding.loc.end.line > 1;
60
+ const hasTokenInPadding = hasTokenOrCommentBetween(
61
+ sourceCode,
62
+ beforePadding,
63
+ afterPadding
64
+ );
65
+ const curLineLastToken = findLastConsecutiveTokenAfter(
66
+ sourceCode,
67
+ curLast,
68
+ nextFirst,
69
+ 0
70
+ );
71
+
72
+ if (isPadded) {
73
+ return;
74
+ }
75
+
76
+ context.report({
77
+ node: body[index],
78
+ message: "Expected blank line after imports.",
79
+
80
+ fix(fixer) {
81
+ if (hasTokenInPadding) {
82
+ return null;
83
+ }
84
+
85
+ return fixer.insertTextAfter(curLineLastToken, "\n");
86
+ },
87
+ });
88
+ },
89
+ };
90
+ },
91
+ };
@@ -0,0 +1,171 @@
1
+ // Based on `@stylistic/js/lines-between-class-members`
2
+ // See: https://github.com/eslint-stylistic/eslint-stylistic/blob/d6809c910510a4477e01ea248071f0701d0af4ed/packages/eslint-plugin/rules/lines-between-class-members/lines-between-class-members._js_.ts
3
+
4
+ import {
5
+ findFirstConsecutiveTokenBefore,
6
+ findLastConsecutiveTokenAfter,
7
+ getBoundaryTokens,
8
+ hasTokenOrCommentBetween,
9
+ isTokenOnSameLine,
10
+ } from "./utils/tokens.mjs";
11
+
12
+ export default {
13
+ meta: {
14
+ type: "layout",
15
+ docs: {
16
+ description: "Require an empty line between class members",
17
+ },
18
+ fixable: "whitespace",
19
+ schema: [], // no options
20
+ messages: {
21
+ never: "Unexpected blank line between class members.",
22
+ always: "Expected blank line between class members.",
23
+ },
24
+ },
25
+
26
+ create(context) {
27
+ const configureList = [
28
+ { blankLine: "always", prev: "service", next: "*" },
29
+ { blankLine: "always", prev: "*", next: "method" },
30
+ { blankLine: "always", prev: "method", next: "*" },
31
+ { blankLine: "always", prev: "*", next: "template" },
32
+ ];
33
+ const sourceCode = context.sourceCode;
34
+
35
+ /**
36
+ * Returns the type of the node.
37
+ * @param node The class member node to check.
38
+ * @returns The type string (see `configureList`)
39
+ * @private
40
+ */
41
+ function nodeType(node) {
42
+ if (
43
+ node.type === "PropertyDefinition" &&
44
+ ["service", "optionalService", "controller"].includes(
45
+ node.decorators?.[0]?.expression?.name ||
46
+ node.decorators?.[0]?.expression?.callee?.name
47
+ )
48
+ ) {
49
+ return "service";
50
+ } else if (node.type === "PropertyDefinition") {
51
+ return "field";
52
+ } else if (node.type === "MethodDefinition") {
53
+ return "method";
54
+ } else if (node.type === "GlimmerTemplate") {
55
+ return "template";
56
+ } else {
57
+ return "other";
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Checks whether the given node matches the given type.
63
+ * @param node The class member node to check.
64
+ * @param type The class member type to check.
65
+ * @returns `true` if the class member node matched the type.
66
+ * @private
67
+ */
68
+ function match(node, type) {
69
+ if (type === "*") {
70
+ return true;
71
+ }
72
+
73
+ return nodeType(node) === type;
74
+ }
75
+
76
+ /**
77
+ * Finds the last matched configuration from the configureList.
78
+ * @param prevNode The previous node to match.
79
+ * @param nextNode The current node to match.
80
+ * @returns Padding type or `null` if no matches were found.
81
+ * @private
82
+ */
83
+ function getPaddingType(prevNode, nextNode) {
84
+ for (let i = configureList.length - 1; i >= 0; --i) {
85
+ const configure = configureList[i];
86
+ const matched =
87
+ match(prevNode, configure.prev) && match(nextNode, configure.next);
88
+
89
+ if (matched) {
90
+ return configure.blankLine;
91
+ }
92
+ }
93
+ return null;
94
+ }
95
+
96
+ return {
97
+ ClassBody(node) {
98
+ const body = node.body;
99
+
100
+ for (let i = 0; i < body.length - 1; i++) {
101
+ const curFirst = sourceCode.getFirstToken(body[i]);
102
+ const { curLast, nextFirst } = getBoundaryTokens(
103
+ sourceCode,
104
+ body[i],
105
+ body[i + 1]
106
+ );
107
+ const singleLine = isTokenOnSameLine(curFirst, curLast);
108
+ const skip =
109
+ singleLine && nodeType(body[i]) === nodeType(body[i + 1]);
110
+ const beforePadding = findLastConsecutiveTokenAfter(
111
+ sourceCode,
112
+ curLast,
113
+ nextFirst,
114
+ 1
115
+ );
116
+ const afterPadding = findFirstConsecutiveTokenBefore(
117
+ sourceCode,
118
+ nextFirst,
119
+ curLast,
120
+ 1
121
+ );
122
+ const isPadded =
123
+ afterPadding.loc.start.line - beforePadding.loc.end.line > 1;
124
+ const hasTokenInPadding = hasTokenOrCommentBetween(
125
+ sourceCode,
126
+ beforePadding,
127
+ afterPadding
128
+ );
129
+ const curLineLastToken = findLastConsecutiveTokenAfter(
130
+ sourceCode,
131
+ curLast,
132
+ nextFirst,
133
+ 0
134
+ );
135
+ const paddingType = getPaddingType(body[i], body[i + 1]);
136
+
137
+ if (paddingType === "never" && isPadded) {
138
+ context.report({
139
+ node: body[i + 1],
140
+ messageId: "never",
141
+
142
+ fix(fixer) {
143
+ if (hasTokenInPadding) {
144
+ return null;
145
+ }
146
+
147
+ return fixer.replaceTextRange(
148
+ [beforePadding.range[1], afterPadding.range[0]],
149
+ "\n"
150
+ );
151
+ },
152
+ });
153
+ } else if (paddingType === "always" && !skip && !isPadded) {
154
+ context.report({
155
+ node: body[i + 1],
156
+ messageId: "always",
157
+
158
+ fix(fixer) {
159
+ if (hasTokenInPadding) {
160
+ return null;
161
+ }
162
+
163
+ return fixer.insertTextAfter(curLineLastToken, "\n");
164
+ },
165
+ });
166
+ }
167
+ }
168
+ },
169
+ };
170
+ },
171
+ };
@@ -1,16 +1,14 @@
1
- // no-queryselector-body-html.mjs
2
1
  export default {
3
2
  meta: {
4
3
  type: "problem",
5
4
  docs: {
6
5
  description:
7
6
  'disallow document.querySelector("body") and document.querySelector("html")',
8
- category: "Best Practices",
9
- recommended: false,
10
7
  },
11
8
  fixable: "code",
12
9
  schema: [], // no options
13
10
  },
11
+
14
12
  create(context) {
15
13
  return {
16
14
  CallExpression(node) {
@@ -3,12 +3,11 @@ export default {
3
3
  type: "suggestion",
4
4
  docs: {
5
5
  description: "Convert 'inject as service' to 'service'",
6
- category: "Best Practices",
7
- recommended: false,
8
6
  },
9
7
  fixable: "code",
10
8
  schema: [], // no options
11
9
  },
10
+
12
11
  create(context) {
13
12
  return {
14
13
  ImportDeclaration(node) {
@@ -0,0 +1,128 @@
1
+ export function isTokenOnSameLine(left, right) {
2
+ return left?.loc?.end.line === right?.loc?.start.line;
3
+ }
4
+
5
+ export function isSemicolonToken(token) {
6
+ return token.value === ";" && token.type === "Punctuator";
7
+ }
8
+
9
+ /**
10
+ * Gets a pair of tokens that should be used to check lines between two class member nodes.
11
+ *
12
+ * In most cases, this returns the very last token of the current node and
13
+ * the very first token of the next node.
14
+ * For example:
15
+ *
16
+ * class C {
17
+ * x = 1; // curLast: `;` nextFirst: `in`
18
+ * in = 2
19
+ * }
20
+ *
21
+ * There is only one exception. If the given node ends with a semicolon, and it looks like
22
+ * a semicolon-less style's semicolon - one that is not on the same line as the preceding
23
+ * token, but is on the line where the next class member starts - this returns the preceding
24
+ * token and the semicolon as boundary tokens.
25
+ * For example:
26
+ *
27
+ * class C {
28
+ * x = 1 // curLast: `1` nextFirst: `;`
29
+ * ;in = 2
30
+ * }
31
+ * When determining the desired layout of the code, we should treat this semicolon as
32
+ * a part of the next class member node instead of the one it technically belongs to.
33
+ * @param curNode Current class member node.
34
+ * @param nextNode Next class member node.
35
+ * @returns The actual last token of `node`.
36
+ * @private
37
+ */
38
+ export function getBoundaryTokens(sourceCode, curNode, nextNode) {
39
+ const lastToken = sourceCode.getLastToken(curNode);
40
+ const prevToken = sourceCode.getTokenBefore(lastToken);
41
+ const nextToken = sourceCode.getFirstToken(nextNode); // skip possible lone `;` between nodes
42
+
43
+ const isSemicolonLessStyle =
44
+ isSemicolonToken(lastToken) &&
45
+ !isTokenOnSameLine(prevToken, lastToken) &&
46
+ isTokenOnSameLine(lastToken, nextToken);
47
+
48
+ return isSemicolonLessStyle
49
+ ? { curLast: prevToken, nextFirst: lastToken }
50
+ : { curLast: lastToken, nextFirst: nextToken };
51
+ }
52
+
53
+ /**
54
+ * Return the last token among the consecutive tokens that have no exceed max line difference in between, before the first token in the next member.
55
+ * @param prevLastToken The last token in the previous member node.
56
+ * @param nextFirstToken The first token in the next member node.
57
+ * @param maxLine The maximum number of allowed line difference between consecutive tokens.
58
+ * @returns The last token among the consecutive tokens.
59
+ */
60
+ export function findLastConsecutiveTokenAfter(
61
+ sourceCode,
62
+ prevLastToken,
63
+ nextFirstToken,
64
+ maxLine
65
+ ) {
66
+ const after = sourceCode.getTokenAfter(prevLastToken, {
67
+ includeComments: true,
68
+ });
69
+
70
+ if (
71
+ after !== nextFirstToken &&
72
+ after.loc.start.line - prevLastToken.loc.end.line <= maxLine
73
+ ) {
74
+ return findLastConsecutiveTokenAfter(
75
+ sourceCode,
76
+ after,
77
+ nextFirstToken,
78
+ maxLine
79
+ );
80
+ }
81
+
82
+ return prevLastToken;
83
+ }
84
+
85
+ /**
86
+ * Return the first token among the consecutive tokens that have no exceed max line difference in between, after the last token in the previous member.
87
+ * @param nextFirstToken The first token in the next member node.
88
+ * @param prevLastToken The last token in the previous member node.
89
+ * @param maxLine The maximum number of allowed line difference between consecutive tokens.
90
+ * @returns The first token among the consecutive tokens.
91
+ */
92
+ export function findFirstConsecutiveTokenBefore(
93
+ sourceCode,
94
+ nextFirstToken,
95
+ prevLastToken,
96
+ maxLine
97
+ ) {
98
+ const before = sourceCode.getTokenBefore(nextFirstToken, {
99
+ includeComments: true,
100
+ });
101
+
102
+ if (
103
+ before !== prevLastToken &&
104
+ nextFirstToken.loc.start.line - before.loc.end.line <= maxLine
105
+ ) {
106
+ return findFirstConsecutiveTokenBefore(
107
+ sourceCode,
108
+ before,
109
+ prevLastToken,
110
+ maxLine
111
+ );
112
+ }
113
+
114
+ return nextFirstToken;
115
+ }
116
+
117
+ /**
118
+ * Checks if there is a token or comment between two tokens.
119
+ * @param before The token before.
120
+ * @param after The token after.
121
+ * @returns True if there is a token or comment between two tokens.
122
+ */
123
+ export function hasTokenOrCommentBetween(sourceCode, before, after) {
124
+ return (
125
+ sourceCode.getTokensBetween(before, after, { includeComments: true })
126
+ .length !== 0
127
+ );
128
+ }
package/eslint.config.mjs CHANGED
@@ -1,2 +1,3 @@
1
1
  import DiscourseRecommended from "./eslint.mjs";
2
+
2
3
  export default DiscourseRecommended;
package/eslint.mjs CHANGED
@@ -2,7 +2,6 @@ import { createConfigItem } from "@babel/core";
2
2
  import BabelParser from "@babel/eslint-parser";
3
3
  import PluginProposalDecorators from "@babel/plugin-proposal-decorators";
4
4
  import js from "@eslint/js";
5
- import stylisticJs from "@stylistic/eslint-plugin-js";
6
5
  import EmberESLintParser from "ember-eslint-parser";
7
6
  import DecoratorPosition from "eslint-plugin-decorator-position";
8
7
  import EmberPlugin from "eslint-plugin-ember";
@@ -17,7 +16,9 @@ import deprecatedLookups from "./eslint-rules/deprecated-lookups.mjs";
17
16
  import discourseCommonImports from "./eslint-rules/discourse-common-imports.mjs";
18
17
  import i18nImport from "./eslint-rules/i18n-import-location.mjs";
19
18
  import i18nT from "./eslint-rules/i18n-t.mjs";
20
- import noSimpleQueryselector from "./eslint-rules/no-simple-queryselector.mjs";
19
+ import lineAfterImports from "./eslint-rules/line-after-imports.mjs";
20
+ import linesBetweenClassMembers from "./eslint-rules/lines-between-class-members.mjs";
21
+ import noSimpleQuerySelector from "./eslint-rules/no-simple-query-selector.mjs";
21
22
  import serviceInjectImport from "./eslint-rules/service-inject-import.mjs";
22
23
 
23
24
  // Copied from "ember-template-imports/lib/utils"
@@ -96,7 +97,6 @@ export default [
96
97
  },
97
98
  },
98
99
  plugins: {
99
- "@stylistic/js": stylisticJs,
100
100
  ember: EmberPlugin,
101
101
  "sort-class-members": SortClassMembers,
102
102
  "decorator-position": DecoratorPosition,
@@ -108,9 +108,11 @@ export default [
108
108
  "i18n-import-location": i18nImport,
109
109
  "i18n-t": i18nT,
110
110
  "service-inject-import": serviceInjectImport,
111
- "no-simple-queryselector": noSimpleQueryselector,
111
+ "no-simple-query-selector": noSimpleQuerySelector,
112
112
  "deprecated-lookups": deprecatedLookups,
113
113
  "discourse-common-imports": discourseCommonImports,
114
+ "lines-between-class-members": linesBetweenClassMembers,
115
+ "line-after-imports": lineAfterImports,
114
116
  },
115
117
  },
116
118
  },
@@ -161,16 +163,6 @@ export default [
161
163
  "import/no-duplicates": "error",
162
164
  "object-shorthand": ["error", "properties"],
163
165
  "no-dupe-class-members": "error",
164
- "@stylistic/js/lines-between-class-members": [
165
- "error",
166
- {
167
- enforce: [
168
- { blankLine: "always", prev: "*", next: "method" },
169
- { blankLine: "always", prev: "method", next: "*" },
170
- ],
171
- },
172
- { exceptAfterSingleLine: true },
173
- ],
174
166
  "ember/no-classic-components": "off",
175
167
  "ember/no-component-lifecycle-hooks": "off",
176
168
  "ember/require-tagless-components": "off",
@@ -193,7 +185,6 @@ export default [
193
185
  "ember/classic-decorator-hooks": "off",
194
186
  "ember/classic-decorator-no-classic-methods": "off",
195
187
  "ember/no-actions-hash": "off",
196
- "ember/no-classic-classes": "off",
197
188
  "ember/no-tracked-properties-from-args": "off",
198
189
  "ember/no-jquery": "off",
199
190
  "ember/no-runloop": "off",
@@ -289,9 +280,11 @@ export default [
289
280
  "discourse/i18n-import-location": ["error"],
290
281
  "discourse/i18n-t": ["error"],
291
282
  "discourse/service-inject-import": ["error"],
292
- "discourse/no-simple-queryselector": ["error"],
283
+ "discourse/no-simple-query-selector": ["error"],
293
284
  "discourse/deprecated-lookups": ["error"],
294
285
  "discourse/discourse-common-imports": ["error"],
286
+ "discourse/lines-between-class-members": ["error"],
287
+ "discourse/line-after-imports": ["error"],
295
288
  },
296
289
  },
297
290
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discourse/lint-configs",
3
- "version": "2.10.0",
3
+ "version": "2.11.0",
4
4
  "description": "Shareable lint configs for Discourse core, plugins, and themes",
5
5
  "author": "Discourse",
6
6
  "license": "MIT",
@@ -33,9 +33,8 @@
33
33
  "@babel/core": "^7.26.9",
34
34
  "@babel/eslint-parser": "^7.26.8",
35
35
  "@babel/plugin-proposal-decorators": "^7.25.9",
36
- "@stylistic/eslint-plugin-js": "^4.2.0",
37
- "ember-template-lint": "^7.0.0",
38
- "eslint": "^9.21.0",
36
+ "ember-template-lint": "^7.0.1",
37
+ "eslint": "^9.22.0",
39
38
  "eslint-plugin-decorator-position": "^6.0.0",
40
39
  "eslint-plugin-ember": "^12.5.0",
41
40
  "eslint-plugin-import": "^2.31.0",
@@ -51,8 +50,8 @@
51
50
  "typescript": "^5.8.2"
52
51
  },
53
52
  "peerDependencies": {
54
- "ember-template-lint": "7.0.0",
55
- "eslint": "9.21.0",
53
+ "ember-template-lint": "7.0.1",
54
+ "eslint": "9.22.0",
56
55
  "prettier": "3.5.3",
57
56
  "stylelint": "16.15.0"
58
57
  }