aboutsummaryrefslogtreecommitdiff
path: root/babel-import-promise/babel-import-promise.mjs
blob: 10aee6a05600d5de3fa0949cfa65b1c75d3a183a (plain)
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
export default function ({ types: t }) {
	return {
		visitor: {
			Program(path, state) {
				let shouldAdd = false
				const MyVisitor = {
					Function: {
						enter: function enter(path) {
							if (path.node.async) {
								shouldAdd = true
							}
						}
					},
					Identifier(path) {
						if (path.node.name === "Promise") {
							shouldAdd = true
						}
					}
				};
				path.traverse(MyVisitor)
				if (shouldAdd) {
					let depth = state.filename.replace(state.cwd, "").split(/[\\/]/g).length - 2
					const identifier = t.identifier('Promise');
					const importDefaultSpecifier = t.importDefaultSpecifier(identifier);
					const importDeclaration = t.importDeclaration([importDefaultSpecifier], t.stringLiteral("../".repeat(depth) + 'PromiseV2'));
					path.unshiftContainer('body', importDeclaration);
				}
			}
		}
	};
};