-
Notifications
You must be signed in to change notification settings - Fork 3
/
transform.js
44 lines (38 loc) · 1.28 KB
/
transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const transformImports = require('transform-imports');
const tsParser = require('jscodeshift/parser/ts');
module.exports = (file, api, options) => {
const { currentImportSources, targetImportSource, onlyImportedExports, fuzzyMatch } = options.transformOptions;
try {
return transformImports(
file.source,
importDefs => {
importDefs.forEach(importDef => {
if (
importSourceMatches(importDef.source, currentImportSources, fuzzyMatch) &&
importedExportMatches(importDef.importedExport.name, onlyImportedExports)
) {
importDef.source = targetImportSource;
}
});
},
{
parser: tsParser()
}
);
} catch (e) {
console.error(e);
return file.source;
}
}
function importedExportMatches(value, validValues) {
if (validValues.length === 0) {
return true;
}
return validValues.includes(value);
}
function importSourceMatches(value, patterns, fuzzyMatch) {
if (fuzzyMatch) {
return patterns.some(pattern => value.match(pattern));
}
return patterns.some(pattern => value === pattern);
}