|
| 1 | +import { GetConfig } from './types' |
| 2 | +import { addObjectMethodResultInterceptors, findChildContainingExactPosition } from './utils' |
| 3 | + |
| 4 | +let currentSymbolName: string | undefined |
| 5 | + |
| 6 | +interface ParsedIgnoreSetting { |
| 7 | + module: string |
| 8 | + symbols: string[] |
| 9 | + isAnySymbol: boolean |
| 10 | + moduleCompare: 'startsWith' | 'strict' |
| 11 | +} |
| 12 | + |
| 13 | +// will be removed once I'm sure performance can't be improved |
| 14 | +const initIgnoreAutoImport = () => { |
| 15 | + addObjectMethodResultInterceptors(tsFull, { |
| 16 | + // todo |
| 17 | + createPackageJsonImportFilter(res, fromFile) { |
| 18 | + return { |
| 19 | + ...res, |
| 20 | + allowsImportingAmbientModule(moduleSymbol, moduleSpecifierResolutionHost) { |
| 21 | + return true |
| 22 | + // return isModuleShouldBeIgnored(moduleSymbol.name.slice(1, -1), '') |
| 23 | + }, |
| 24 | + // allowsImportingSourceFile(sourceFile, moduleSpecifierResolutionHost) { |
| 25 | + // return false |
| 26 | + // }, |
| 27 | + allowsImportingSpecifier(moduleSpecifier) { |
| 28 | + const result = res.allowsImportingSpecifier(moduleSpecifier) |
| 29 | + if (!result) return false |
| 30 | + return false |
| 31 | + }, |
| 32 | + } |
| 33 | + }, |
| 34 | + }) |
| 35 | + // addObjectMethodResultInterceptors(tsFull.codefix, { |
| 36 | + // getAllFixes(res) { |
| 37 | + // return res |
| 38 | + // }, |
| 39 | + // }) |
| 40 | +} |
| 41 | + |
| 42 | +export const getIgnoreAutoImportSetting = (c: GetConfig) => { |
| 43 | + return c('suggestions.ignoreAutoImports').map((spec): ParsedIgnoreSetting => { |
| 44 | + const hashIndex = spec.indexOf('#') |
| 45 | + let module = hashIndex === -1 ? spec : spec.slice(0, hashIndex) |
| 46 | + const moduleCompare = module.endsWith('/*') ? 'startsWith' : 'strict' |
| 47 | + if (moduleCompare === 'startsWith') { |
| 48 | + module = module.slice(0, -'/*'.length) |
| 49 | + } |
| 50 | + if (hashIndex === -1) { |
| 51 | + return { |
| 52 | + module, |
| 53 | + symbols: [], |
| 54 | + isAnySymbol: true, |
| 55 | + moduleCompare, |
| 56 | + } |
| 57 | + } |
| 58 | + const symbolsString = spec.slice(hashIndex + 1) |
| 59 | + // * (glob asterisk) is reserved for future ussage |
| 60 | + const isAnySymbol = symbolsString === '*' |
| 61 | + return { |
| 62 | + module, |
| 63 | + symbols: isAnySymbol ? [] : symbolsString.split(','), |
| 64 | + isAnySymbol, |
| 65 | + moduleCompare, |
| 66 | + } |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +export const isAutoImportEntryShouldBeIgnored = (ignoreAutoImportsSetting: ParsedIgnoreSetting[], targetModule: string, symbol: string) => { |
| 71 | + for (const { module, moduleCompare, isAnySymbol, symbols } of ignoreAutoImportsSetting) { |
| 72 | + const isIgnoreModule = moduleCompare === 'startsWith' ? targetModule.startsWith(module) : targetModule === module |
| 73 | + if (!isIgnoreModule) continue |
| 74 | + if (isAnySymbol) return true |
| 75 | + if (!symbols.includes(symbol)) continue |
| 76 | + return true |
| 77 | + } |
| 78 | + return false |
| 79 | +} |
| 80 | + |
| 81 | +export const shouldChangeSortingOfAutoImport = (symbolName: string, c: GetConfig) => { |
| 82 | + const arr = c('autoImport.changeSorting')[symbolName] |
| 83 | + return arr && arr.length > 0 |
| 84 | +} |
| 85 | + |
| 86 | +export const changeSortingOfAutoImport = (c: GetConfig, symbolName: string): ((module: string) => number) => { |
| 87 | + const arr = c('autoImport.changeSorting')[symbolName] |
| 88 | + if (!arr || !arr.length) return () => 0 |
| 89 | + const maxIndex = arr.length |
| 90 | + return module => { |
| 91 | + let actualIndex = arr.findIndex(x => { |
| 92 | + return x.endsWith('/*') ? module.startsWith(x.slice(0, -'/*'.length)) : module === x |
| 93 | + }) |
| 94 | + // . - index, don't treat some node_modules-ignored modules as local such as `.vite` |
| 95 | + const isLocal = module.startsWith('./') || module.startsWith('../') || module === '.' |
| 96 | + if (actualIndex === -1) actualIndex = arr.findIndex(x => (isLocal ? x === '.' : x === '*')) |
| 97 | + return actualIndex === -1 ? maxIndex : actualIndex |
| 98 | + } |
| 99 | +} |
0 commit comments