-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
52 lines (49 loc) · 1.65 KB
/
mod.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
import {
createIdentifier,
initialIdentifierIndex,
type TsIdentifier,
} from "./identifier.ts";
import type { UsedNameAndModulePathSet } from "./interface.ts";
import { collectInCode } from "./collect.ts";
import { toString } from "./toString.ts";
import type * as d from "./data.ts";
export * from "./identifier.ts";
export * from "./interface.ts";
export * as data from "./data.ts";
export const generateCodeAsString = (
code: d.JsTsCode,
codeType: d.CodeType,
): string => {
// グローバル空間にある名前とimportしたモジュールのパスを集める
const usedNameAndModulePath: UsedNameAndModulePathSet = collectInCode(code);
return toString(
code,
{
moduleMap: createImportedModuleName(usedNameAndModulePath),
usedNameSet: usedNameAndModulePath.usedNameSet,
codeType,
},
);
};
/**
* 使われている名前, モジュールのパスから, モジュールのパスとnamed importの識別子のMapを生成する
* @param usedNameAndModulePath
*/
const createImportedModuleName = (
usedNameAndModulePath: UsedNameAndModulePathSet,
): ReadonlyMap<string, TsIdentifier> => {
let identifierIndex = initialIdentifierIndex;
const importedModuleNameMap = new Map<string, TsIdentifier>();
for (const modulePath of usedNameAndModulePath.modulePathSet) {
const identifierAndNextIdentifierIndex = createIdentifier(
identifierIndex,
usedNameAndModulePath.usedNameSet,
);
importedModuleNameMap.set(
modulePath,
identifierAndNextIdentifierIndex.identifier,
);
identifierIndex = identifierAndNextIdentifierIndex.nextIdentifierIndex;
}
return importedModuleNameMap;
};