Skip to content

Commit 56367b3

Browse files
Remove unused branches now that parsing happens after rollup tree shaking (#251)
1 parent 9d966c2 commit 56367b3

File tree

2 files changed

+18
-221
lines changed

2 files changed

+18
-221
lines changed

src/transformers/exports.ts

-75
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
ExportAllDeclaration,
2121
Identifier,
2222
Node,
23-
ClassDeclaration,
2423
} from 'estree';
2524
import { TransformSourceDescription, OutputOptions } from 'rollup';
2625
import { NamedDeclaration, DefaultDeclaration } from './parsing-utilities';
@@ -208,7 +207,6 @@ export default class ExportTransform extends Transform implements TransformInter
208207
exportName,
209208
) as ExportDetails;
210209
switch (exportDetails.type) {
211-
case ExportClosureMapping.DEFAULT_FUNCTION:
212210
case ExportClosureMapping.NAMED_DEFAULT_FUNCTION:
213211
case ExportClosureMapping.DEFAULT:
214212
if (ancestor.expression.left.range) {
@@ -219,54 +217,6 @@ export default class ExportTransform extends Transform implements TransformInter
219217
);
220218
}
221219
break;
222-
case ExportClosureMapping.NAMED_FUNCTION:
223-
if (
224-
ancestor.expression.right.type === 'FunctionExpression' &&
225-
ancestor.expression.right.params.length > 0
226-
) {
227-
const [firstParameter] = ancestor.expression.right.params;
228-
if (ancestor.expression.range && firstParameter.range) {
229-
source.overwrite(
230-
ancestor.expression.range[0],
231-
firstParameter.range[0] - 1,
232-
`export function ${exportName}`,
233-
);
234-
}
235-
}
236-
break;
237-
case ExportClosureMapping.DEFAULT_CLASS:
238-
case ExportClosureMapping.NAMED_DEFAULT_CLASS:
239-
if (ancestor.expression.right.type === 'Identifier') {
240-
const { name: mangledName } = ancestor.expression.right;
241-
242-
walk.simple(program, {
243-
ClassDeclaration(node: ClassDeclaration) {
244-
if (
245-
node.id &&
246-
node.id.name === mangledName &&
247-
node.range &&
248-
node.body.range &&
249-
ancestor.range
250-
) {
251-
if (node.superClass && node.superClass.type === 'Identifier') {
252-
source.overwrite(
253-
node.range[0],
254-
node.body.range[0],
255-
`export default class extends ${node.superClass.name}`,
256-
);
257-
} else {
258-
source.overwrite(
259-
node.range[0],
260-
node.body.range[0],
261-
'export default class',
262-
);
263-
}
264-
source.remove(...ancestor.range);
265-
}
266-
},
267-
});
268-
}
269-
break;
270220
case ExportClosureMapping.NAMED_CONSTANT:
271221
if (exportDetails.source === null) {
272222
const { object: leftObject } = ancestor.expression.left;
@@ -292,31 +242,6 @@ export default class ExportTransform extends Transform implements TransformInter
292242
exportDetails,
293243
);
294244
break;
295-
case ExportClosureMapping.DEFAULT_VALUE:
296-
case ExportClosureMapping.DEFAULT_OBJECT:
297-
if (
298-
ancestor.expression.left.object.range &&
299-
ancestor.expression.right.range
300-
) {
301-
source.overwrite(
302-
ancestor.expression.left.object.range[0],
303-
ancestor.expression.right.range[0],
304-
'export default ',
305-
);
306-
}
307-
break;
308-
default:
309-
if (ancestor.range) {
310-
source.remove(...ancestor.range);
311-
}
312-
313-
if (ancestor.expression.right.type === 'Identifier') {
314-
collectedExportsToAppend = ExportTransform.storeExportToAppend(
315-
collectedExportsToAppend,
316-
exportDetails,
317-
);
318-
}
319-
break;
320245
}
321246
}
322247
}

src/transformers/parsing-utilities.ts

+18-146
Original file line numberDiff line numberDiff line change
@@ -31,104 +31,17 @@ import {
3131
Range,
3232
} from '../types';
3333

34-
type ExportDeclarationsWithFunctions = ExportNamedDeclaration | ExportDefaultDeclaration;
35-
36-
function functionDeclarationName(
37-
context: PluginContext,
38-
declaration: ExportDeclarationsWithFunctions,
39-
): string | null {
40-
// For the Declaration passed, there can be a function declaration.
41-
if (declaration.declaration && declaration.declaration.type === 'FunctionDeclaration') {
42-
const functionDeclaration = declaration.declaration;
43-
44-
if (
45-
functionDeclaration !== null &&
46-
functionDeclaration.id !== null &&
47-
functionDeclaration.id.name !== null
48-
) {
49-
return functionDeclaration.id.name;
50-
}
51-
}
52-
53-
return null;
54-
}
55-
56-
function classDeclarationName(
57-
context: PluginContext,
58-
declaration: ExportDeclarationsWithFunctions,
59-
): string | null {
60-
// For the Declaration passed, there can be a function declaration.
61-
if (declaration.declaration && declaration.declaration.type === 'ClassDeclaration') {
62-
const classDeclaration = declaration.declaration;
63-
64-
if (
65-
classDeclaration !== null &&
66-
classDeclaration.id !== null &&
67-
classDeclaration.id.name !== null
68-
) {
69-
// This class declaration is the export name we need to know.
70-
return classDeclaration.id.name;
71-
}
72-
}
73-
74-
return null;
75-
}
76-
7734
export function NamedDeclaration(
7835
context: PluginContext,
7936
declaration: ExportNamedDeclaration,
8037
): Array<ExportDetails> {
81-
const functionName = functionDeclarationName(context, declaration);
82-
const className = classDeclarationName(context, declaration);
8338
const range: Range = declaration.range as Range;
8439
const source: string | null =
8540
declaration.source && declaration.source.value && typeof declaration.source.value === 'string'
8641
? declaration.source.value
8742
: null;
8843

89-
// TODO(KB): This logic isn't great. If something has a named declaration, lets instead use the AST to find out what it is.
90-
// var Foo=function(){}export{Foo as default} => default export function
91-
92-
if (functionName !== null) {
93-
return [
94-
{
95-
local: functionName,
96-
exported: functionName,
97-
closureName: functionName,
98-
type: ExportClosureMapping.NAMED_FUNCTION,
99-
range,
100-
source,
101-
},
102-
];
103-
} else if (className !== null) {
104-
return [
105-
{
106-
local: className,
107-
exported: className,
108-
closureName: className,
109-
type: ExportClosureMapping.NAMED_CLASS,
110-
range,
111-
source,
112-
},
113-
];
114-
} else if (declaration.declaration && declaration.declaration.type === 'VariableDeclaration') {
115-
const { declarations } = declaration.declaration;
116-
const exportDetails: Array<ExportDetails> = [];
117-
118-
for (const declarator of declarations) {
119-
if (declarator.id.type === 'Identifier') {
120-
exportDetails.push({
121-
local: declarator.id.name,
122-
exported: declarator.id.name,
123-
closureName: declarator.id.name,
124-
type: ExportClosureMapping.NAMED_CONSTANT,
125-
range,
126-
source,
127-
});
128-
}
129-
}
130-
return exportDetails;
131-
} else if (declaration.specifiers) {
44+
if (declaration.specifiers) {
13245
const exportDetails: Array<ExportDetails> = [];
13346

13447
for (const specifier of declaration.specifiers) {
@@ -155,51 +68,17 @@ export function DefaultDeclaration(
15568
const range: Range = declaration.range as Range;
15669
const source = null;
15770

158-
switch (declaration.declaration.type) {
159-
case 'FunctionDeclaration':
160-
const functionName = functionDeclarationName(context, declaration);
161-
if (functionName !== null) {
162-
return [
163-
{
164-
local: functionName,
165-
exported: functionName,
166-
closureName: functionName,
167-
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
168-
range,
169-
source,
170-
},
171-
];
172-
}
173-
break;
174-
case 'ClassDeclaration':
175-
const className = classDeclarationName(context, declaration);
176-
if (className !== null) {
177-
return [
178-
{
179-
local: className,
180-
exported: className,
181-
closureName: className,
182-
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
183-
range,
184-
source,
185-
},
186-
];
187-
}
188-
break;
189-
case 'Identifier':
190-
if (declaration.declaration.name) {
191-
return [
192-
{
193-
local: declaration.declaration.name,
194-
exported: declaration.declaration.name,
195-
closureName: declaration.declaration.name,
196-
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
197-
range,
198-
source,
199-
},
200-
];
201-
}
202-
break;
71+
if (declaration.declaration.type === 'Identifier' && declaration.declaration.name) {
72+
return [
73+
{
74+
local: declaration.declaration.name,
75+
exported: declaration.declaration.name,
76+
closureName: declaration.declaration.name,
77+
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
78+
range,
79+
source,
80+
},
81+
];
20382
}
20483
}
20584

@@ -224,21 +103,14 @@ export function importLocalNames(
224103
context: PluginContext,
225104
declaration: ImportDeclaration,
226105
): Array<string> {
106+
const VALID_SPECIFIERS = [IMPORT_SPECIFIER, IMPORT_NAMESPACE_SPECIFIER, IMPORT_DEFAULT_SPECIFIER];
227107
const returnableSpecifiers: Array<string> = [];
228108

229-
if (declaration.specifiers) {
230-
declaration.specifiers.forEach(specifier => {
231-
switch (specifier.type) {
232-
case IMPORT_SPECIFIER:
233-
case IMPORT_NAMESPACE_SPECIFIER:
234-
case IMPORT_DEFAULT_SPECIFIER:
235-
returnableSpecifiers.push(specifier.local.name);
236-
break;
237-
default:
238-
break;
239-
}
240-
});
241-
}
109+
(declaration.specifiers || []).forEach(specifier => {
110+
if (VALID_SPECIFIERS.includes(specifier.type)) {
111+
returnableSpecifiers.push(specifier.local.name);
112+
}
113+
});
242114

243115
return returnableSpecifiers;
244116
}

0 commit comments

Comments
 (0)