|
| 1 | +import {isClosingBraceToken} from '@eslint-community/eslint-utils'; |
| 2 | + |
| 3 | +const MESSAGE_ID_ERROR = 'error'; |
| 4 | +const MESSAGE_ID_SUGGESTION_REMOVE_DECLARATION = 'suggestion/remove-declaration'; |
| 5 | +const MESSAGE_ID_SUGGESTION_TO_SIDE_EFFECT_IMPORT = 'suggestion/to-side-effect-import'; |
| 6 | +const messages = { |
| 7 | + [MESSAGE_ID_ERROR]: '{{type}} statement without specifiers is not allowed.', |
| 8 | + [MESSAGE_ID_SUGGESTION_REMOVE_DECLARATION]: 'Remove this {{type}} statement.', |
| 9 | + [MESSAGE_ID_SUGGESTION_TO_SIDE_EFFECT_IMPORT]: 'Switch to side effect import.', |
| 10 | +}; |
| 11 | + |
| 12 | +const isFromToken = token => token.type === 'Identifier' && token.value === 'from'; |
| 13 | + |
| 14 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 15 | +const create = context => { |
| 16 | + const {sourceCode} = context; |
| 17 | + |
| 18 | + context.on('ImportDeclaration', importDeclaration => { |
| 19 | + const {specifiers} = importDeclaration; |
| 20 | + |
| 21 | + if (specifiers.some(node => node.type === 'ImportSpecifier' || node.type === 'ImportNamespaceSpecifier')) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + const {source, importKind} = importDeclaration; |
| 26 | + const fromToken = sourceCode.getTokenBefore(source); |
| 27 | + if (!isFromToken(fromToken)) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const closingBraceToken = sourceCode.getTokenBefore(fromToken); |
| 32 | + if (!isClosingBraceToken(closingBraceToken)) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const openingBraceToken = sourceCode.getTokenBefore(closingBraceToken); |
| 37 | + |
| 38 | + const problem = { |
| 39 | + node: importDeclaration, |
| 40 | + loc: { |
| 41 | + start: sourceCode.getLoc(openingBraceToken).start, |
| 42 | + end: sourceCode.getLoc(closingBraceToken).end, |
| 43 | + }, |
| 44 | + messageId: MESSAGE_ID_ERROR, |
| 45 | + data: { |
| 46 | + type: 'import', |
| 47 | + }, |
| 48 | + }; |
| 49 | + |
| 50 | + // If there is a `ImportDefaultSpecifier`, it has to be the first. |
| 51 | + const importDefaultSpecifier = specifiers.length === 1 ? specifiers[0] : undefined; |
| 52 | + if (importKind === 'type' && !importDefaultSpecifier) { |
| 53 | + problem.fix = fixer => fixer.remove(importDeclaration); |
| 54 | + return problem; |
| 55 | + } |
| 56 | + |
| 57 | + if (importDefaultSpecifier) { |
| 58 | + problem.fix = function * (fixer) { |
| 59 | + yield fixer.remove(closingBraceToken); |
| 60 | + yield fixer.remove(openingBraceToken); |
| 61 | + |
| 62 | + const commaToken = sourceCode.getTokenBefore(openingBraceToken); |
| 63 | + yield fixer.remove(commaToken); |
| 64 | + |
| 65 | + if (sourceCode.getRange(closingBraceToken)[1] === sourceCode.getRange(fromToken)[0]) { |
| 66 | + yield fixer.insertTextBefore(fromToken, ' '); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + return problem; |
| 71 | + } |
| 72 | + |
| 73 | + problem.suggest = [ |
| 74 | + { |
| 75 | + messageId: MESSAGE_ID_SUGGESTION_REMOVE_DECLARATION, |
| 76 | + fix: fixer => fixer.remove(importDeclaration), |
| 77 | + }, |
| 78 | + { |
| 79 | + messageId: MESSAGE_ID_SUGGESTION_TO_SIDE_EFFECT_IMPORT, |
| 80 | + * fix(fixer) { |
| 81 | + yield fixer.remove(openingBraceToken); |
| 82 | + yield fixer.remove(closingBraceToken); |
| 83 | + yield fixer.remove(fromToken); |
| 84 | + }, |
| 85 | + }, |
| 86 | + ]; |
| 87 | + |
| 88 | + return problem; |
| 89 | + }); |
| 90 | + |
| 91 | + context.on('ExportNamedDeclaration', exportDeclaration => { |
| 92 | + const {specifiers, declaration} = exportDeclaration; |
| 93 | + |
| 94 | + if (declaration || specifiers.length > 0) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const {source, exportKind} = exportDeclaration; |
| 99 | + const fromToken = source ? sourceCode.getTokenBefore(source) : undefined; |
| 100 | + const closingBraceToken = fromToken |
| 101 | + ? sourceCode.getTokenBefore(fromToken) |
| 102 | + : sourceCode.getLastToken(exportDeclaration, isClosingBraceToken); |
| 103 | + const openingBraceToken = sourceCode.getTokenBefore(closingBraceToken); |
| 104 | + |
| 105 | + const problem = { |
| 106 | + node: exportDeclaration, |
| 107 | + loc: { |
| 108 | + start: sourceCode.getLoc(openingBraceToken).start, |
| 109 | + end: sourceCode.getLoc(closingBraceToken).end, |
| 110 | + }, |
| 111 | + messageId: MESSAGE_ID_ERROR, |
| 112 | + data: { |
| 113 | + type: 'export', |
| 114 | + }, |
| 115 | + }; |
| 116 | + |
| 117 | + if (!source || exportKind === 'type') { |
| 118 | + problem.fix = fixer => fixer.remove(exportDeclaration); |
| 119 | + return problem; |
| 120 | + } |
| 121 | + |
| 122 | + problem.suggest = [ |
| 123 | + { |
| 124 | + messageId: MESSAGE_ID_SUGGESTION_REMOVE_DECLARATION, |
| 125 | + fix: fixer => fixer.remove(exportDeclaration), |
| 126 | + }, |
| 127 | + { |
| 128 | + messageId: MESSAGE_ID_SUGGESTION_TO_SIDE_EFFECT_IMPORT, |
| 129 | + * fix(fixer) { |
| 130 | + const exportToken = sourceCode.getFirstToken(exportDeclaration); |
| 131 | + yield fixer.replaceText(exportToken, 'import'); |
| 132 | + yield fixer.remove(openingBraceToken); |
| 133 | + yield fixer.remove(closingBraceToken); |
| 134 | + yield fixer.remove(fromToken); |
| 135 | + }, |
| 136 | + }, |
| 137 | + ]; |
| 138 | + |
| 139 | + return problem; |
| 140 | + }); |
| 141 | +}; |
| 142 | + |
| 143 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 144 | +const config = { |
| 145 | + create, |
| 146 | + meta: { |
| 147 | + type: 'suggestion', |
| 148 | + docs: { |
| 149 | + description: 'Require non-empty specifier list in import and export statements.', |
| 150 | + recommended: true, |
| 151 | + }, |
| 152 | + fixable: 'code', |
| 153 | + hasSuggestions: true, |
| 154 | + messages, |
| 155 | + }, |
| 156 | +}; |
| 157 | + |
| 158 | +export default config; |
0 commit comments