|
| 1 | +import docsUrl from '../docsUrl'; |
| 2 | + |
| 3 | +function isComma(token) { |
| 4 | + return token.type === 'Punctuator' && token.value === ','; |
| 5 | +} |
| 6 | + |
| 7 | +function removeSpecifiers(fixes, fixer, sourceCode, specifiers) { |
| 8 | + for (const specifier of specifiers) { |
| 9 | + // remove the trailing comma |
| 10 | + const comma = sourceCode.getTokenAfter(specifier, isComma); |
| 11 | + if (comma) { |
| 12 | + fixes.push(fixer.remove(comma)); |
| 13 | + } |
| 14 | + fixes.push(fixer.remove(specifier)); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function getImportText( |
| 19 | + node, |
| 20 | + sourceCode, |
| 21 | + specifiers, |
| 22 | + kind, |
| 23 | +) { |
| 24 | + const sourceString = sourceCode.getText(node.source); |
| 25 | + if (specifiers.length === 0) { |
| 26 | + return ''; |
| 27 | + } |
| 28 | + |
| 29 | + const names = specifiers.map(s => { |
| 30 | + if (s.imported.name === s.local.name) { |
| 31 | + return s.imported.name; |
| 32 | + } |
| 33 | + return `${s.imported.name} as ${s.local.name}`; |
| 34 | + }); |
| 35 | + // insert a fresh top-level import |
| 36 | + return `import ${kind} {${names.join(', ')}} from ${sourceString};`; |
| 37 | +} |
| 38 | + |
| 39 | +module.exports = { |
| 40 | + meta: { |
| 41 | + type: 'suggestion', |
| 42 | + docs: { |
| 43 | + description: 'Enforce or ban the use of inline type-only markers for named imports', |
| 44 | + url: docsUrl('consistent-type-specifier-style'), |
| 45 | + }, |
| 46 | + fixable: 'code', |
| 47 | + schema: [ |
| 48 | + { |
| 49 | + type: 'string', |
| 50 | + enum: ['prefer-inline', 'prefer-top-level'], |
| 51 | + default: 'prefer-inline', |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + |
| 56 | + create(context) { |
| 57 | + const sourceCode = context.getSourceCode(); |
| 58 | + |
| 59 | + if (context.options[0] === 'prefer-inline') { |
| 60 | + return { |
| 61 | + ImportDeclaration(node) { |
| 62 | + if (node.importKind === 'value' || node.importKind == null) { |
| 63 | + // top-level value / unknown is valid |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if ( |
| 68 | + // no specifiers (import type {} from '') have no specifiers to mark as inline |
| 69 | + node.specifiers.length === 0 || |
| 70 | + (node.specifiers.length === 1 && |
| 71 | + // default imports are both "inline" and "top-level" |
| 72 | + (node.specifiers[0].type === 'ImportDefaultSpecifier' || |
| 73 | + // namespace imports are both "inline" and "top-level" |
| 74 | + node.specifiers[0].type === 'ImportNamespaceSpecifier')) |
| 75 | + ) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + context.report({ |
| 80 | + node, |
| 81 | + message: 'Prefer using inline {{kind}} specifiers instead of a top-level {{kind}}-only import.', |
| 82 | + data: { |
| 83 | + kind: node.importKind, |
| 84 | + }, |
| 85 | + fix(fixer) { |
| 86 | + const kindToken = sourceCode.getFirstToken(node, { skip: 1 }); |
| 87 | + |
| 88 | + return [].concat( |
| 89 | + kindToken ? fixer.remove(kindToken) : [], |
| 90 | + node.specifiers.map((specifier) => fixer.insertTextBefore(specifier, `${node.importKind} `)), |
| 91 | + ); |
| 92 | + }, |
| 93 | + }); |
| 94 | + }, |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + // prefer-top-level |
| 99 | + return { |
| 100 | + ImportDeclaration(node) { |
| 101 | + if ( |
| 102 | + // already top-level is valid |
| 103 | + node.importKind === 'type' || |
| 104 | + node.importKind === 'typeof' || |
| 105 | + // no specifiers (import {} from '') cannot have inline - so is valid |
| 106 | + node.specifiers.length === 0 || |
| 107 | + (node.specifiers.length === 1 && |
| 108 | + // default imports are both "inline" and "top-level" |
| 109 | + (node.specifiers[0].type === 'ImportDefaultSpecifier' || |
| 110 | + // namespace imports are both "inline" and "top-level" |
| 111 | + node.specifiers[0].type === 'ImportNamespaceSpecifier')) |
| 112 | + ) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const typeSpecifiers = []; |
| 117 | + const typeofSpecifiers = []; |
| 118 | + const valueSpecifiers = []; |
| 119 | + let defaultSpecifier = null; |
| 120 | + for (const specifier of node.specifiers) { |
| 121 | + if (specifier.type === 'ImportDefaultSpecifier') { |
| 122 | + defaultSpecifier = specifier; |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + if (specifier.importKind === 'type') { |
| 127 | + typeSpecifiers.push(specifier); |
| 128 | + } else if (specifier.importKind === 'typeof') { |
| 129 | + typeofSpecifiers.push(specifier); |
| 130 | + } else if (specifier.importKind === 'value' || specifier.importKind == null) { |
| 131 | + valueSpecifiers.push(specifier); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + const typeImport = getImportText(node, sourceCode, typeSpecifiers, 'type'); |
| 136 | + const typeofImport = getImportText(node, sourceCode, typeofSpecifiers, 'typeof'); |
| 137 | + const newImports = `${typeImport}\n${typeofImport}`.trim(); |
| 138 | + |
| 139 | + if (typeSpecifiers.length + typeofSpecifiers.length === node.specifiers.length) { |
| 140 | + // all specifiers have inline specifiers - so we replace the entire import |
| 141 | + const kind = [].concat( |
| 142 | + typeSpecifiers.length > 0 ? 'type' : [], |
| 143 | + typeofSpecifiers.length > 0 ? 'typeof' : [], |
| 144 | + ); |
| 145 | + |
| 146 | + context.report({ |
| 147 | + node, |
| 148 | + message: 'Prefer using a top-level {{kind}}-only import instead of inline {{kind}} specifiers.', |
| 149 | + data: { |
| 150 | + kind: kind.join('/'), |
| 151 | + }, |
| 152 | + fix(fixer) { |
| 153 | + return fixer.replaceText(node, newImports); |
| 154 | + }, |
| 155 | + }); |
| 156 | + } else { |
| 157 | + // remove specific specifiers and insert new imports for them |
| 158 | + for (const specifier of typeSpecifiers.concat(typeofSpecifiers)) { |
| 159 | + context.report({ |
| 160 | + node: specifier, |
| 161 | + message: 'Prefer using a top-level {{kind}}-only import instead of inline {{kind}} specifiers.', |
| 162 | + data: { |
| 163 | + kind: specifier.importKind, |
| 164 | + }, |
| 165 | + fix(fixer) { |
| 166 | + const fixes = []; |
| 167 | + |
| 168 | + // if there are no value specifiers, then the other report fixer will be called, not this one |
| 169 | + |
| 170 | + if (valueSpecifiers.length > 0) { |
| 171 | + // import { Value, type Type } from 'mod'; |
| 172 | + |
| 173 | + // we can just remove the type specifiers |
| 174 | + removeSpecifiers(fixes, fixer, sourceCode, typeSpecifiers); |
| 175 | + removeSpecifiers(fixes, fixer, sourceCode, typeofSpecifiers); |
| 176 | + |
| 177 | + // make the import nicely formatted by also removing the trailing comma after the last value import |
| 178 | + // eg |
| 179 | + // import { Value, type Type } from 'mod'; |
| 180 | + // to |
| 181 | + // import { Value } from 'mod'; |
| 182 | + // not |
| 183 | + // import { Value, } from 'mod'; |
| 184 | + const maybeComma = sourceCode.getTokenAfter(valueSpecifiers[valueSpecifiers.length - 1]); |
| 185 | + if (isComma(maybeComma)) { |
| 186 | + fixes.push(fixer.remove(maybeComma)); |
| 187 | + } |
| 188 | + } else if (defaultSpecifier) { |
| 189 | + // import Default, { type Type } from 'mod'; |
| 190 | + |
| 191 | + // remove the entire curly block so we don't leave an empty one behind |
| 192 | + // NOTE - the default specifier *must* be the first specifier always! |
| 193 | + // so a comma exists that we also have to clean up or else it's bad syntax |
| 194 | + const comma = sourceCode.getTokenAfter(defaultSpecifier, isComma); |
| 195 | + const closingBrace = sourceCode.getTokenAfter( |
| 196 | + node.specifiers[node.specifiers.length - 1], |
| 197 | + token => token.type === 'Punctuator' && token.value === '}', |
| 198 | + ); |
| 199 | + fixes.push(fixer.removeRange([ |
| 200 | + comma.range[0], |
| 201 | + closingBrace.range[1], |
| 202 | + ])); |
| 203 | + } |
| 204 | + |
| 205 | + return fixes.concat( |
| 206 | + // insert the new imports after the old declaration |
| 207 | + fixer.insertTextAfter(node, `\n${newImports}`), |
| 208 | + ); |
| 209 | + }, |
| 210 | + }); |
| 211 | + } |
| 212 | + } |
| 213 | + }, |
| 214 | + }; |
| 215 | + }, |
| 216 | +}; |
0 commit comments