|
10 | 10 | * governing permissions and limitations under the License.
|
11 | 11 | */
|
12 | 12 |
|
13 |
| -module.exports = function (context) { |
14 |
| - const sourceCode = context.getSourceCode(); |
15 |
| - let previousDeclaration = null; |
| 13 | +module.exports = { |
| 14 | + meta: { |
| 15 | + fixable: 'code' |
| 16 | + }, |
| 17 | + create: function (context) { |
| 18 | + const sourceCode = context.getSourceCode(); |
| 19 | + let previousDeclaration = null; |
16 | 20 |
|
17 |
| - /** |
18 |
| - * Gets the local name of the first imported module. |
19 |
| - * @param {ASTNode} node - the ImportDeclaration node. |
20 |
| - * @returns {?string} the local name of the first imported module. |
21 |
| - */ |
22 |
| - function getFirstLocalMemberName(node) { |
23 |
| - if (node.specifiers[0]) { |
24 |
| - return node.specifiers[0].local.name.toLowerCase(); |
25 |
| - } |
26 |
| - return null; |
| 21 | + /** |
| 22 | + * Gets the local name of the first imported module. |
| 23 | + * @param {ASTNode} node - the ImportDeclaration node. |
| 24 | + * @returns {?string} the local name of the first imported module. |
| 25 | + */ |
| 26 | + function getFirstLocalMemberName(node) { |
| 27 | + if (node.specifiers[0]) { |
| 28 | + return node.specifiers[0].local.name.toLowerCase(); |
| 29 | + } |
| 30 | + return null; |
27 | 31 |
|
28 |
| - } |
| 32 | + } |
29 | 33 |
|
30 |
| - return { |
31 |
| - ImportDeclaration(node) { |
32 |
| - if (previousDeclaration) { |
33 |
| - let currentLocalMemberName = getFirstLocalMemberName(node), |
34 |
| - previousLocalMemberName = getFirstLocalMemberName(previousDeclaration); |
| 34 | + return { |
| 35 | + ImportDeclaration(node) { |
| 36 | + if (previousDeclaration) { |
| 37 | + let currentLocalMemberName = getFirstLocalMemberName(node), |
| 38 | + previousLocalMemberName = getFirstLocalMemberName(previousDeclaration); |
35 | 39 |
|
36 |
| - if (previousLocalMemberName && |
37 |
| - currentLocalMemberName && |
38 |
| - currentLocalMemberName < previousLocalMemberName |
39 |
| - ) { |
40 |
| - context.report({ |
41 |
| - node, |
42 |
| - message: "Imports should be sorted alphabetically." |
43 |
| - }); |
| 40 | + if (previousLocalMemberName && |
| 41 | + currentLocalMemberName && |
| 42 | + currentLocalMemberName < previousLocalMemberName |
| 43 | + ) { |
| 44 | + context.report({ |
| 45 | + node, |
| 46 | + message: 'Imports should be sorted alphabetically.' |
| 47 | + }); |
| 48 | + } |
44 | 49 | }
|
45 |
| - } |
46 | 50 |
|
47 |
| - const importSpecifiers = node.specifiers.filter(specifier => specifier.type === "ImportSpecifier"); |
48 |
| - const getSortableName = specifier => specifier.local.name.toLowerCase(); |
49 |
| - const firstUnsortedIndex = importSpecifiers.map(getSortableName).findIndex((name, index, array) => array[index - 1] > name); |
| 51 | + const importSpecifiers = node.specifiers.filter(specifier => specifier.type === 'ImportSpecifier'); |
| 52 | + const getSortableName = specifier => specifier.local.name.toLowerCase(); |
| 53 | + const firstUnsortedIndex = importSpecifiers.map(getSortableName).findIndex((name, index, array) => array[index - 1] > name); |
50 | 54 |
|
51 |
| - if (firstUnsortedIndex !== -1) { |
52 |
| - context.report({ |
53 |
| - node: importSpecifiers[firstUnsortedIndex], |
54 |
| - message: "Member '{{memberName}}' of the import declaration should be sorted alphabetically.", |
55 |
| - data: { memberName: importSpecifiers[firstUnsortedIndex].local.name }, |
56 |
| - fix(fixer) { |
57 |
| - return fixer.replaceTextRange( |
58 |
| - [importSpecifiers[0].range[0], importSpecifiers[importSpecifiers.length - 1].range[1]], |
59 |
| - importSpecifiers |
60 |
| - // Clone the importSpecifiers array to avoid mutating it |
61 |
| - .slice() |
| 55 | + if (firstUnsortedIndex !== -1) { |
| 56 | + context.report({ |
| 57 | + node: importSpecifiers[firstUnsortedIndex], |
| 58 | + message: "Member '{{memberName}}' of the import declaration should be sorted alphabetically.", |
| 59 | + data: {memberName: importSpecifiers[firstUnsortedIndex].local.name}, |
| 60 | + fix(fixer) { |
| 61 | + return fixer.replaceTextRange( |
| 62 | + [importSpecifiers[0].range[0], importSpecifiers[importSpecifiers.length - 1].range[1]], |
| 63 | + importSpecifiers |
| 64 | + // Clone the importSpecifiers array to avoid mutating it |
| 65 | + .slice() |
62 | 66 |
|
63 |
| - // Sort the array into the desired order |
64 |
| - .sort((specifierA, specifierB) => { |
65 |
| - const aName = getSortableName(specifierA); |
66 |
| - const bName = getSortableName(specifierB); |
67 |
| - return aName > bName ? 1 : -1; |
68 |
| - }) |
| 67 | + // Sort the array into the desired order |
| 68 | + .sort((specifierA, specifierB) => { |
| 69 | + const aName = getSortableName(specifierA); |
| 70 | + const bName = getSortableName(specifierB); |
| 71 | + return aName > bName ? 1 : -1; |
| 72 | + }) |
69 | 73 |
|
70 |
| - // Build a string out of the sorted list of import specifiers and the text between the originals |
71 |
| - .reduce((sourceText, specifier, index) => { |
72 |
| - const textAfterSpecifier = index === importSpecifiers.length - 1 |
73 |
| - ? "" |
74 |
| - : sourceCode.getText().slice(importSpecifiers[index].range[1], importSpecifiers[index + 1].range[0]); |
| 74 | + // Build a string out of the sorted list of import specifiers and the text between the originals |
| 75 | + .reduce((sourceText, specifier, index) => { |
| 76 | + const textAfterSpecifier = index === importSpecifiers.length - 1 |
| 77 | + ? '' |
| 78 | + : sourceCode.getText().slice(importSpecifiers[index].range[1], importSpecifiers[index + 1].range[0]); |
75 | 79 |
|
76 |
| - return sourceText + sourceCode.getText(specifier) + textAfterSpecifier; |
77 |
| - }, "") |
78 |
| - ); |
79 |
| - } |
80 |
| - }); |
81 |
| - } |
| 80 | + return sourceText + sourceCode.getText(specifier) + textAfterSpecifier; |
| 81 | + }, '') |
| 82 | + ); |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
82 | 86 |
|
83 |
| - previousDeclaration = node; |
84 |
| - } |
85 |
| - }; |
| 87 | + previousDeclaration = node; |
| 88 | + } |
| 89 | + }; |
| 90 | + } |
86 | 91 | };
|
0 commit comments