@angular-eslint/eslint-plugin 16.0.3-alpha.6 → 16.1.0-alpha.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.
@@ -28,6 +28,7 @@
28
28
  "@angular-eslint/prefer-on-push-component-change-detection": "error",
29
29
  "@angular-eslint/prefer-output-readonly": "error",
30
30
  "@angular-eslint/relative-url-prefix": "error",
31
+ "@angular-eslint/sort-imports-metadata": "error",
31
32
  "@angular-eslint/sort-ngmodule-metadata-arrays": "error",
32
33
  "@angular-eslint/use-component-selector": "error",
33
34
  "@angular-eslint/use-component-view-encapsulation": "error",
package/dist/index.js CHANGED
@@ -53,6 +53,7 @@ const pipe_prefix_1 = __importStar(require("./rules/pipe-prefix"));
53
53
  const prefer_on_push_component_change_detection_1 = __importStar(require("./rules/prefer-on-push-component-change-detection"));
54
54
  const prefer_output_readonly_1 = __importStar(require("./rules/prefer-output-readonly"));
55
55
  const relative_url_prefix_1 = __importStar(require("./rules/relative-url-prefix"));
56
+ const sort_imports_metadata_1 = __importStar(require("./rules/sort-imports-metadata"));
56
57
  const sort_ngmodule_metadata_arrays_1 = __importStar(require("./rules/sort-ngmodule-metadata-arrays"));
57
58
  const use_component_selector_1 = __importStar(require("./rules/use-component-selector"));
58
59
  const use_component_view_encapsulation_1 = __importStar(require("./rules/use-component-view-encapsulation"));
@@ -91,6 +92,7 @@ module.exports = {
91
92
  [prefer_on_push_component_change_detection_1.RULE_NAME]: prefer_on_push_component_change_detection_1.default,
92
93
  [prefer_output_readonly_1.RULE_NAME]: prefer_output_readonly_1.default,
93
94
  [relative_url_prefix_1.RULE_NAME]: relative_url_prefix_1.default,
95
+ [sort_imports_metadata_1.RULE_NAME]: sort_imports_metadata_1.default,
94
96
  [sort_ngmodule_metadata_arrays_1.RULE_NAME]: sort_ngmodule_metadata_arrays_1.default,
95
97
  [use_component_selector_1.RULE_NAME]: use_component_selector_1.default,
96
98
  [use_component_view_encapsulation_1.RULE_NAME]: use_component_view_encapsulation_1.default,
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RULE_NAME = void 0;
4
+ const utils_1 = require("@angular-eslint/utils");
5
+ const utils_2 = require("@typescript-eslint/utils");
6
+ const create_eslint_rule_1 = require("../utils/create-eslint-rule");
7
+ exports.RULE_NAME = 'sort-imports-metadata';
8
+ const DEFAULT_LOCALE = 'en-US';
9
+ exports.default = (0, create_eslint_rule_1.createESLintRule)({
10
+ name: exports.RULE_NAME,
11
+ meta: {
12
+ type: 'suggestion',
13
+ docs: {
14
+ description: 'Ensures ASC alphabetical order for import arrays for easy visual scanning',
15
+ recommended: false,
16
+ },
17
+ fixable: 'code',
18
+ schema: [
19
+ {
20
+ type: 'object',
21
+ properties: {
22
+ locale: {
23
+ type: 'string',
24
+ description: 'A string with a BCP 47 language tag.',
25
+ default: DEFAULT_LOCALE,
26
+ },
27
+ },
28
+ additionalProperties: false,
29
+ },
30
+ ],
31
+ messages: {
32
+ sortImportsMetadata: 'Imports should be sorted in ASC alphabetical order',
33
+ },
34
+ },
35
+ defaultOptions: [
36
+ {
37
+ locale: DEFAULT_LOCALE,
38
+ },
39
+ ],
40
+ create(context, [{ locale }]) {
41
+ const selectors = [
42
+ `${utils_1.Selectors.COMPONENT_OR_DIRECTIVE_CLASS_DECORATOR} Property[key.name="imports"] > ArrayExpression`,
43
+ `${utils_1.Selectors.MODULE_CLASS_DECORATOR} Property[key.name="imports"] > ArrayExpression`,
44
+ `${utils_1.Selectors.PIPE_CLASS_DECORATOR} Property[key.name="imports"] > ArrayExpression`,
45
+ ].join(',');
46
+ return {
47
+ [selectors]({ elements }) {
48
+ var _a, _b;
49
+ const imports = elements.filter(utils_2.ASTUtils.isIdentifier);
50
+ if (importsAreSorted(imports, locale)) {
51
+ return;
52
+ }
53
+ const firstOriginalElement = elements[0];
54
+ const lastOriginalElement = elements[elements.length - 1];
55
+ const nodeForFixer = firstOriginalElement.parent;
56
+ // IMPORTANT NOTE: Although we want to delegate formatting to the user's formatter of choice, this rule will perform basic formatting to ensure some presentability (especially around block comments)
57
+ // If all of the imports are on one line, indent the output 2 columns ahead of the `imports` keyword
58
+ // Else, indent the output to match the indentation of the first import
59
+ const whitespacePrefix = elements.every((element) => (element === null || element === void 0 ? void 0 : element.loc.start.line) === firstOriginalElement.loc.start.line)
60
+ ? ' '.repeat(((_a = nodeForFixer.parent) === null || _a === void 0 ? void 0 : _a.loc.start.column) + 2)
61
+ : ' '.repeat(firstOriginalElement.loc.start.column);
62
+ // For each import entry, get all related comments, stringify them, and group them with the import entry
63
+ const importEntriesWithComments = getImportEntriesWithComments(imports, context, whitespacePrefix);
64
+ // Sort the imports
65
+ importEntriesWithComments.sort((importA, importB) => importA.importName.localeCompare(importB.importName, locale));
66
+ const sortedImportsWithCommentsString = `[\n${getSortedImportsWithCommentsAsString(importEntriesWithComments, whitespacePrefix)}${' '.repeat((_b = nodeForFixer.parent) === null || _b === void 0 ? void 0 : _b.loc.start.column)}]`;
67
+ context.report({
68
+ messageId: 'sortImportsMetadata',
69
+ loc: {
70
+ start: firstOriginalElement.loc.start,
71
+ end: lastOriginalElement.loc.end,
72
+ },
73
+ fix: (fixer) => fixer.replaceText(nodeForFixer, sortedImportsWithCommentsString),
74
+ });
75
+ },
76
+ };
77
+ },
78
+ });
79
+ function getImportEntriesWithComments(imports, context, whitespacePrefix) {
80
+ const sourceCode = context.getSourceCode();
81
+ const importEntriesWithComments = [];
82
+ for (let i = 0; i < imports.length; i++) {
83
+ const importEntry = imports[i];
84
+ // Handle comments above the import entry
85
+ let commentsBeforeImport = sourceCode.getCommentsBefore(importEntry);
86
+ const previousImportEntry = imports[i - 1];
87
+ if (previousImportEntry) {
88
+ commentsBeforeImport = commentsBeforeImport.filter((comment) => comment.loc.start.line !== previousImportEntry.loc.start.line);
89
+ }
90
+ const commentsBeforeString = getCommentsAsString(commentsBeforeImport, whitespacePrefix);
91
+ // Handle comments on the same line as the import entry
92
+ // Same-line comments appear in the next import entry's `getCommentsBefore`
93
+ let commentsSameLineString = '';
94
+ const nextImportEntry = imports[i + 1];
95
+ if (nextImportEntry) {
96
+ commentsSameLineString = getCommentsAsString(sourceCode
97
+ .getCommentsBefore(nextImportEntry)
98
+ .filter((comment) => comment.loc.start.line === importEntry.loc.start.line));
99
+ }
100
+ // Handle comments after the import entry
101
+ const commentsAfterString = getCommentsAsString(sourceCode.getCommentsAfter(importEntry), whitespacePrefix);
102
+ const importEntryWithComments = {
103
+ commentsBefore: commentsBeforeString,
104
+ commentsSameLine: commentsSameLineString,
105
+ commentsAfter: commentsAfterString,
106
+ importName: importEntry.name,
107
+ };
108
+ importEntriesWithComments.push(importEntryWithComments);
109
+ }
110
+ return importEntriesWithComments;
111
+ }
112
+ function getSortedImportsWithCommentsAsString(sortedImportEntriesWithComments, whitespacePrefix) {
113
+ let sortedImportsWithCommentsString = '';
114
+ for (const importEntry of sortedImportEntriesWithComments) {
115
+ if (importEntry.commentsBefore) {
116
+ sortedImportsWithCommentsString += `${importEntry.commentsBefore}`;
117
+ }
118
+ sortedImportsWithCommentsString += `${whitespacePrefix}${importEntry.importName},`;
119
+ if (importEntry.commentsSameLine) {
120
+ sortedImportsWithCommentsString += ` ${importEntry.commentsSameLine}`;
121
+ }
122
+ else {
123
+ sortedImportsWithCommentsString += '\n';
124
+ }
125
+ if (importEntry.commentsAfter) {
126
+ sortedImportsWithCommentsString += `${importEntry.commentsAfter}`;
127
+ }
128
+ }
129
+ return sortedImportsWithCommentsString;
130
+ }
131
+ function getCommentsAsString(comments, whitespacePrefix) {
132
+ let commentsString = '';
133
+ if (comments) {
134
+ for (const comment of comments) {
135
+ if (comment.type === 'Line') {
136
+ commentsString += `${whitespacePrefix ? whitespacePrefix : ''}//${comment.value}\n`;
137
+ }
138
+ else if (comment.type === 'Block') {
139
+ commentsString += `${whitespacePrefix ? whitespacePrefix : ''}/*${comment.value}*/\n`;
140
+ }
141
+ }
142
+ }
143
+ return commentsString;
144
+ }
145
+ function importsAreSorted(imports, locale) {
146
+ const importNames = imports.map((importEntry) => importEntry.name);
147
+ let secondIndex;
148
+ for (let firstIndex = 0; firstIndex < importNames.length; firstIndex++) {
149
+ secondIndex = firstIndex + 1;
150
+ if (importNames[firstIndex].localeCompare(importNames[secondIndex], locale) >
151
+ 0) {
152
+ return false;
153
+ }
154
+ }
155
+ return true;
156
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@angular-eslint/eslint-plugin",
3
- "version": "16.0.3-alpha.6+578933e",
3
+ "version": "16.1.0-alpha.0",
4
4
  "description": "ESLint plugin for Angular applications, following angular.io/styleguide",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -17,12 +17,12 @@
17
17
  "LICENSE"
18
18
  ],
19
19
  "dependencies": {
20
- "@angular-eslint/utils": "16.0.3-alpha.6+578933e",
21
- "@typescript-eslint/utils": "5.59.2"
20
+ "@angular-eslint/utils": "16.1.0-alpha.0",
21
+ "@typescript-eslint/utils": "5.59.7"
22
22
  },
23
23
  "peerDependencies": {
24
24
  "eslint": "^7.20.0 || ^8.0.0",
25
25
  "typescript": "*"
26
26
  },
27
- "gitHead": "578933efea37595179d503d7a2178daed9a38faa"
27
+ "gitHead": "255cd39008e9e9a608704f8b62a08c9dc45f8e53"
28
28
  }