Skip to content

Commit 0921fdd

Browse files
Further cleanup for import transform
1 parent 588a9e6 commit 0921fdd

File tree

2 files changed

+79
-56
lines changed

2 files changed

+79
-56
lines changed

src/parsing/import-specifiers.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { IMPORT_SPECIFIER, IMPORT_NAMESPACE_SPECIFIER, IMPORT_DEFAULT_SPECIFIER } from '../types';
18+
import { ImportSpecifier, ImportDefaultSpecifier, ImportNamespaceSpecifier } from 'estree';
19+
20+
export interface Specifiers {
21+
default: string | null;
22+
specific: Array<string>;
23+
local: Array<string>;
24+
}
25+
26+
export function Specifiers(
27+
specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>,
28+
): Specifiers {
29+
let defaultName: Specifiers['default'] = null;
30+
let specificNames: Specifiers['specific'] = [];
31+
let localNames: Specifiers['local'] = [];
32+
33+
for (const specifier of specifiers) {
34+
localNames.push(specifier.local.name);
35+
36+
switch (specifier.type) {
37+
case IMPORT_SPECIFIER:
38+
case IMPORT_NAMESPACE_SPECIFIER:
39+
const { name: local } = (specifier as ImportSpecifier).local;
40+
const { name: imported } = (specifier as ImportSpecifier).imported;
41+
42+
if (local === imported) {
43+
specificNames.push(local);
44+
} else {
45+
specificNames.push(`${imported} as ${local}`);
46+
}
47+
break;
48+
case IMPORT_DEFAULT_SPECIFIER:
49+
defaultName = specifier.local.name;
50+
break;
51+
}
52+
}
53+
54+
return {
55+
default: defaultName,
56+
specific: specificNames,
57+
local: localNames,
58+
};
59+
}
60+
61+
export function FormatSpecifiers(specifiers: Specifiers, name: string): string {
62+
let formatted: string = 'import ';
63+
64+
if (specifiers.default !== null) {
65+
formatted += specifiers.default;
66+
}
67+
if (specifiers.specific.length > 0) {
68+
formatted += `${specifiers.default !== null ? ',' : ''}{${specifiers.specific.join(',')}}`;
69+
}
70+
formatted += ` from '${name}';`;
71+
72+
return formatted;
73+
}

src/transformers/imports.ts

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {
18-
Transform,
19-
Range,
20-
IMPORT_SPECIFIER,
21-
IMPORT_NAMESPACE_SPECIFIER,
22-
IMPORT_DEFAULT_SPECIFIER,
23-
} from '../types';
17+
import { Transform, Range } from '../types';
2418
import { literalName } from '../parsing/literal-name';
19+
import { FormatSpecifiers, Specifiers } from '../parsing/import-specifiers';
2520
import { TransformSourceDescription } from 'rollup';
2621
import MagicString from 'magic-string';
27-
import { ImportDeclaration, Identifier, ImportSpecifier } from 'estree';
22+
import { ImportDeclaration, Identifier } from 'estree';
2823
import { parse, walk } from '../acorn';
2924

3025
const DYNAMIC_IMPORT_KEYWORD = 'import';
@@ -42,19 +37,6 @@ interface RangedImport {
4237
range: Range;
4338
}
4439

45-
const VALID_SPECIFIERS = [IMPORT_SPECIFIER, IMPORT_NAMESPACE_SPECIFIER, IMPORT_DEFAULT_SPECIFIER];
46-
function importLocalNames(declaration: ImportDeclaration): Array<string> {
47-
const returnableSpecifiers: Array<string> = [];
48-
49-
for (const specifier of declaration.specifiers || []) {
50-
if (VALID_SPECIFIERS.includes(specifier.type)) {
51-
returnableSpecifiers.push(specifier.local.name);
52-
}
53-
}
54-
55-
return returnableSpecifiers;
56-
}
57-
5840
export default class ImportTransform extends Transform {
5941
private importedExternalsSyntax: { [key: string]: string } = {};
6042
private importedExternalsLocalNames: Array<string> = [];
@@ -105,43 +87,11 @@ window['${DYNAMIC_IMPORT_REPLACEMENT}'] = ${DYNAMIC_IMPORT_REPLACEMENT};`;
10587
async ImportDeclaration(node: ImportDeclaration) {
10688
const name = literalName(node.source);
10789
const range: Range = node.range as Range;
90+
const specifiers: Specifiers = Specifiers(node.specifiers);
10891

109-
let defaultSpecifier: string | null = null;
110-
const specificSpecifiers: Array<string> = [];
111-
for (const specifier of node.specifiers) {
112-
switch (specifier.type) {
113-
case IMPORT_SPECIFIER:
114-
case IMPORT_NAMESPACE_SPECIFIER:
115-
const { name: local } = (specifier as ImportSpecifier).local;
116-
const { name: imported } = (specifier as ImportSpecifier).imported;
117-
specificSpecifiers.push(local === imported ? local : `${imported} as ${local}`);
118-
break;
119-
case IMPORT_DEFAULT_SPECIFIER:
120-
defaultSpecifier = specifier.local.name;
121-
break;
122-
}
123-
}
124-
125-
const multipleSpecifiers = specificSpecifiers.length > 0;
126-
if (defaultSpecifier !== null) {
127-
if (multipleSpecifiers) {
128-
self.importedExternalsSyntax[
129-
name
130-
] = `import ${defaultSpecifier},{${specificSpecifiers.join(',')}} from '${name}';`;
131-
} else {
132-
self.importedExternalsSyntax[name] = `import ${defaultSpecifier} from '${name}';`;
133-
}
134-
} else if (multipleSpecifiers) {
135-
self.importedExternalsSyntax[name] = `import {${specificSpecifiers.join(
136-
',',
137-
)}} from '${name}';`;
138-
}
139-
92+
self.importedExternalsSyntax[name] = FormatSpecifiers(specifiers, name);
93+
self.importedExternalsLocalNames.push(...specifiers.local);
14094
source.remove(...range);
141-
142-
self.importedExternalsLocalNames = self.importedExternalsLocalNames.concat(
143-
importLocalNames(node),
144-
);
14595
},
14696
Import(node: RangedImport) {
14797
const [start, end] = node.range;

0 commit comments

Comments
 (0)