-
Notifications
You must be signed in to change notification settings - Fork 34
/
importReact.ts
64 lines (59 loc) · 2.52 KB
/
importReact.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import * as babel from '@babel/core';
import {NodePath} from '@babel/traverse';
import {findImportStatement} from './import.js';
type ImportDeclaration = babel.types.ImportDeclaration;
type MemberExpression = babel.types.MemberExpression;
type Program = babel.types.Program;
type Identifier = babel.types.Identifier;
const insertImportReact = (program: NodePath<Program>, specifier: string) => {
const expression = babel.types.importDeclaration(
[
babel.types.importSpecifier(
babel.types.identifier(specifier),
babel.types.identifier(specifier)
),
],
babel.types.stringLiteral('react')
);
const [inserted] = program.unshiftContainer('body', expression);
return inserted;
};
const constructCallee = (importStatement: NodePath<ImportDeclaration>, specifier: string) => {
const specifiers = importStatement.get('specifiers');
for (const currentSpecifier of specifiers) {
if (currentSpecifier.isImportNamespaceSpecifier()) {
const name = currentSpecifier.get('local').node.name;
return babel.types.memberExpression(
babel.types.identifier(name),
babel.types.identifier(specifier)
);
}
else if (currentSpecifier.isImportDefaultSpecifier()) {
// 不要问为什么代码和上面的一样,要问就问`babel`的类型去
const name = currentSpecifier.get('local').node.name;
return babel.types.memberExpression(
babel.types.identifier(name),
babel.types.identifier(specifier)
);
}
else if (currentSpecifier.isImportSpecifier()) {
const name = currentSpecifier.get('local').node.name;
if (name === specifier) {
return babel.types.identifier(specifier);
}
}
}
// 到这里的时候,应该是`import {xxx} from 'react'`但`xxx`不包含需要的东西,所以要上去追加一个
importStatement.pushContainer(
'specifiers',
babel.types.importSpecifier(
babel.types.identifier(specifier),
babel.types.identifier(specifier)
)
);
return babel.types.identifier(specifier);
};
export const prepareReactImport = (program: NodePath<Program>, specifier: string): Identifier | MemberExpression => {
const importStatement = findImportStatement(program, 'react') ?? insertImportReact(program, specifier);
return constructCallee(importStatement, specifier);
};