Skip to content

Commit

Permalink
Remove unused branches now that parsing happens after rollup tree sha…
Browse files Browse the repository at this point in the history
…king (#251)
  • Loading branch information
kristoferbaxter authored Dec 20, 2019
1 parent 9d966c2 commit 56367b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 221 deletions.
75 changes: 0 additions & 75 deletions src/transformers/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
ExportAllDeclaration,
Identifier,
Node,
ClassDeclaration,
} from 'estree';
import { TransformSourceDescription, OutputOptions } from 'rollup';
import { NamedDeclaration, DefaultDeclaration } from './parsing-utilities';
Expand Down Expand Up @@ -208,7 +207,6 @@ export default class ExportTransform extends Transform implements TransformInter
exportName,
) as ExportDetails;
switch (exportDetails.type) {
case ExportClosureMapping.DEFAULT_FUNCTION:
case ExportClosureMapping.NAMED_DEFAULT_FUNCTION:
case ExportClosureMapping.DEFAULT:
if (ancestor.expression.left.range) {
Expand All @@ -219,54 +217,6 @@ export default class ExportTransform extends Transform implements TransformInter
);
}
break;
case ExportClosureMapping.NAMED_FUNCTION:
if (
ancestor.expression.right.type === 'FunctionExpression' &&
ancestor.expression.right.params.length > 0
) {
const [firstParameter] = ancestor.expression.right.params;
if (ancestor.expression.range && firstParameter.range) {
source.overwrite(
ancestor.expression.range[0],
firstParameter.range[0] - 1,
`export function ${exportName}`,
);
}
}
break;
case ExportClosureMapping.DEFAULT_CLASS:
case ExportClosureMapping.NAMED_DEFAULT_CLASS:
if (ancestor.expression.right.type === 'Identifier') {
const { name: mangledName } = ancestor.expression.right;

walk.simple(program, {
ClassDeclaration(node: ClassDeclaration) {
if (
node.id &&
node.id.name === mangledName &&
node.range &&
node.body.range &&
ancestor.range
) {
if (node.superClass && node.superClass.type === 'Identifier') {
source.overwrite(
node.range[0],
node.body.range[0],
`export default class extends ${node.superClass.name}`,
);
} else {
source.overwrite(
node.range[0],
node.body.range[0],
'export default class',
);
}
source.remove(...ancestor.range);
}
},
});
}
break;
case ExportClosureMapping.NAMED_CONSTANT:
if (exportDetails.source === null) {
const { object: leftObject } = ancestor.expression.left;
Expand All @@ -292,31 +242,6 @@ export default class ExportTransform extends Transform implements TransformInter
exportDetails,
);
break;
case ExportClosureMapping.DEFAULT_VALUE:
case ExportClosureMapping.DEFAULT_OBJECT:
if (
ancestor.expression.left.object.range &&
ancestor.expression.right.range
) {
source.overwrite(
ancestor.expression.left.object.range[0],
ancestor.expression.right.range[0],
'export default ',
);
}
break;
default:
if (ancestor.range) {
source.remove(...ancestor.range);
}

if (ancestor.expression.right.type === 'Identifier') {
collectedExportsToAppend = ExportTransform.storeExportToAppend(
collectedExportsToAppend,
exportDetails,
);
}
break;
}
}
}
Expand Down
164 changes: 18 additions & 146 deletions src/transformers/parsing-utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,104 +31,17 @@ import {
Range,
} from '../types';

type ExportDeclarationsWithFunctions = ExportNamedDeclaration | ExportDefaultDeclaration;

function functionDeclarationName(
context: PluginContext,
declaration: ExportDeclarationsWithFunctions,
): string | null {
// For the Declaration passed, there can be a function declaration.
if (declaration.declaration && declaration.declaration.type === 'FunctionDeclaration') {
const functionDeclaration = declaration.declaration;

if (
functionDeclaration !== null &&
functionDeclaration.id !== null &&
functionDeclaration.id.name !== null
) {
return functionDeclaration.id.name;
}
}

return null;
}

function classDeclarationName(
context: PluginContext,
declaration: ExportDeclarationsWithFunctions,
): string | null {
// For the Declaration passed, there can be a function declaration.
if (declaration.declaration && declaration.declaration.type === 'ClassDeclaration') {
const classDeclaration = declaration.declaration;

if (
classDeclaration !== null &&
classDeclaration.id !== null &&
classDeclaration.id.name !== null
) {
// This class declaration is the export name we need to know.
return classDeclaration.id.name;
}
}

return null;
}

export function NamedDeclaration(
context: PluginContext,
declaration: ExportNamedDeclaration,
): Array<ExportDetails> {
const functionName = functionDeclarationName(context, declaration);
const className = classDeclarationName(context, declaration);
const range: Range = declaration.range as Range;
const source: string | null =
declaration.source && declaration.source.value && typeof declaration.source.value === 'string'
? declaration.source.value
: null;

// TODO(KB): This logic isn't great. If something has a named declaration, lets instead use the AST to find out what it is.
// var Foo=function(){}export{Foo as default} => default export function

if (functionName !== null) {
return [
{
local: functionName,
exported: functionName,
closureName: functionName,
type: ExportClosureMapping.NAMED_FUNCTION,
range,
source,
},
];
} else if (className !== null) {
return [
{
local: className,
exported: className,
closureName: className,
type: ExportClosureMapping.NAMED_CLASS,
range,
source,
},
];
} else if (declaration.declaration && declaration.declaration.type === 'VariableDeclaration') {
const { declarations } = declaration.declaration;
const exportDetails: Array<ExportDetails> = [];

for (const declarator of declarations) {
if (declarator.id.type === 'Identifier') {
exportDetails.push({
local: declarator.id.name,
exported: declarator.id.name,
closureName: declarator.id.name,
type: ExportClosureMapping.NAMED_CONSTANT,
range,
source,
});
}
}
return exportDetails;
} else if (declaration.specifiers) {
if (declaration.specifiers) {
const exportDetails: Array<ExportDetails> = [];

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

switch (declaration.declaration.type) {
case 'FunctionDeclaration':
const functionName = functionDeclarationName(context, declaration);
if (functionName !== null) {
return [
{
local: functionName,
exported: functionName,
closureName: functionName,
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
range,
source,
},
];
}
break;
case 'ClassDeclaration':
const className = classDeclarationName(context, declaration);
if (className !== null) {
return [
{
local: className,
exported: className,
closureName: className,
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
range,
source,
},
];
}
break;
case 'Identifier':
if (declaration.declaration.name) {
return [
{
local: declaration.declaration.name,
exported: declaration.declaration.name,
closureName: declaration.declaration.name,
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
range,
source,
},
];
}
break;
if (declaration.declaration.type === 'Identifier' && declaration.declaration.name) {
return [
{
local: declaration.declaration.name,
exported: declaration.declaration.name,
closureName: declaration.declaration.name,
type: ExportClosureMapping.NAMED_DEFAULT_FUNCTION,
range,
source,
},
];
}
}

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

if (declaration.specifiers) {
declaration.specifiers.forEach(specifier => {
switch (specifier.type) {
case IMPORT_SPECIFIER:
case IMPORT_NAMESPACE_SPECIFIER:
case IMPORT_DEFAULT_SPECIFIER:
returnableSpecifiers.push(specifier.local.name);
break;
default:
break;
}
});
}
(declaration.specifiers || []).forEach(specifier => {
if (VALID_SPECIFIERS.includes(specifier.type)) {
returnableSpecifiers.push(specifier.local.name);
}
});

return returnableSpecifiers;
}

0 comments on commit 56367b3

Please sign in to comment.