Skip to content

Commit f1f2994

Browse files
Further cleanups to import specifiers
1 parent 0921fdd commit f1f2994

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/parsing/import-specifiers.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ export interface Specifiers {
2626
export function Specifiers(
2727
specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>,
2828
): Specifiers {
29-
let defaultName: Specifiers['default'] = null;
30-
let specificNames: Specifiers['specific'] = [];
31-
let localNames: Specifiers['local'] = [];
29+
const returnable: Specifiers = {
30+
default: null,
31+
specific: [],
32+
local: [],
33+
};
3234

3335
for (const specifier of specifiers) {
34-
localNames.push(specifier.local.name);
36+
returnable.local.push(specifier.local.name);
3537

3638
switch (specifier.type) {
3739
case IMPORT_SPECIFIER:
@@ -40,34 +42,31 @@ export function Specifiers(
4042
const { name: imported } = (specifier as ImportSpecifier).imported;
4143

4244
if (local === imported) {
43-
specificNames.push(local);
45+
returnable.specific.push(local);
4446
} else {
45-
specificNames.push(`${imported} as ${local}`);
47+
returnable.specific.push(`${imported} as ${local}`);
4648
}
4749
break;
4850
case IMPORT_DEFAULT_SPECIFIER:
49-
defaultName = specifier.local.name;
51+
returnable.default = specifier.local.name;
5052
break;
5153
}
5254
}
5355

54-
return {
55-
default: defaultName,
56-
specific: specificNames,
57-
local: localNames,
58-
};
56+
return returnable;
5957
}
6058

6159
export function FormatSpecifiers(specifiers: Specifiers, name: string): string {
6260
let formatted: string = 'import ';
61+
let values: Array<string> = [];
6362

6463
if (specifiers.default !== null) {
65-
formatted += specifiers.default;
64+
values.push(specifiers.default);
6665
}
6766
if (specifiers.specific.length > 0) {
68-
formatted += `${specifiers.default !== null ? ',' : ''}{${specifiers.specific.join(',')}}`;
67+
values.push(`{${specifiers.specific.join(',')}}`);
6968
}
70-
formatted += ` from '${name}';`;
69+
formatted += `${values.join(',')} from '${name}';`;
7170

7271
return formatted;
7372
}

0 commit comments

Comments
 (0)