Skip to content

Commit

Permalink
feat: re-exports from imported file
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 4, 2020
1 parent 280ab9d commit 6220d59
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 19 deletions.
38 changes: 28 additions & 10 deletions core/instrument/src/babel/extract-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { sourceLocation } from './utils';

export interface ExportType {
name: string;
internalName?: string;
internalName: string;
loc: CodeLocation;
/**
* in case of export { Button } from './button-named-export';
Expand All @@ -15,6 +15,7 @@ export interface ExportType {
}

export const EXPORT_ALL = '*';
export const EXPORT_DEFAULT = 'default';

export interface NamedExportTypes {
[key: string]: ExportType;
Expand Down Expand Up @@ -45,15 +46,30 @@ export const traverseExports = (results: ExportTypes) => {
return {
ExportDefaultDeclaration: (path: any) => {
results.default = {
name: 'default',
name: EXPORT_DEFAULT,
internalName: EXPORT_DEFAULT,
loc: sourceLocation(path.node.loc),
};
},
ImportDeclaration: (path: any) => {
const { specifiers, source } = path.node;
if (Array.isArray(specifiers)) {
specifiers.forEach(specifier => {
globals[specifier.local.name] = {
name: specifier.local.name,
internalName: specifier.imported
? specifier.imported.name
: specifier.local.name,
from: source.value,
loc: sourceLocation(specifier.loc),
};
});
}
},
VariableDeclaration: (path: any) => {
const { declarations } = path.node;
if (Array.isArray(declarations) && declarations.length > 0) {
const declaration = declarations[0];
if (declaration) {
if (Array.isArray(declarations)) {
declarations.forEach(declaration => {
const name = declaration.id.name;
//check if it was a named export
if (!results.named[name]) {
Expand All @@ -62,7 +78,7 @@ export const traverseExports = (results: ExportTypes) => {
localExports[namedExport.name] = namedExport;
}
}
}
});
}
},
ExportSpecifier: (path: any) => {
Expand Down Expand Up @@ -103,13 +119,15 @@ export const traverseExports = (results: ExportTypes) => {
}
} else if (specifiers) {
specifiers.forEach((specifier: any) => {
const internalName = specifier.local
? specifier.local.name
: specifier.exported.name;
const global = globals[internalName];
results.named[specifier.exported.name] = {
name: specifier.exported.name,
internalName: specifier.local
? specifier.local.name
: specifier.exported.name,
internalName: global ? global.internalName : internalName,
loc: sourceLocation(specifier.exported.loc),
from: source ? source.value : undefined,
from: source ? source.value : global ? global.from : undefined,
};
});
}
Expand Down
26 changes: 19 additions & 7 deletions core/instrument/src/babel/follow-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,35 @@ export const followImports = (
named: {},
};
traverse(ast, traverseExports(exports));

const folderName = path.dirname(filePath);
const findExport =
baseImportedName === 'default' || baseImportedName === 'namespace'
? exports.default
: exports.named[baseImportedName];
if (findExport !== undefined) {
return {
filePath,
exportedAs: findExport.name,
loc: findExport.loc,
};
if (!findExport.from) {
return {
filePath,
exportedAs: findExport.name,
loc: findExport.loc,
};
} else {
const resolvedFilePath = resolve.sync(findExport.from, {
...resolveOptions,
basedir: folderName,
});
return followImports(
findExport.internalName,
resolvedFilePath,
parserOptions,
resolveOptions,
);
}
}
const imports: ImportTypes = {};
traverse(ast, traverseImports(imports));
const findImport = imports[baseImportedName];
if (findImport) {
const folderName = path.dirname(filePath);
const resolvedFilePath = resolve.sync(findImport.from, {
...resolveOptions,
basedir: folderName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Object {
exports[`extract-exports named export 1`] = `
Object {
"default": Object {
"internalName": "default",
"loc": Object {
"end": Object {
"column": 28,
Expand Down Expand Up @@ -146,7 +147,7 @@ exports[`extract-exports re-exported name 1`] = `
Object {
"named": Object {
"myStory": Object {
"from": undefined,
"from": "stories.tsx",
"internalName": "myStory",
"loc": Object {
"end": Object {
Expand Down
17 changes: 16 additions & 1 deletion core/instrument/test/__snapshots__/follow-imports.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,22 @@ Object {
}
`;

exports[`follow-imports one level index import 1`] = `undefined`;
exports[`follow-imports one level index import 1`] = `
Object {
"exportedAs": "Button",
"filePath": "/Users/atanasster/component-controls/core/instrument/test/examples/follow-imports/button-named-export.js",
"loc": Object {
"end": Object {
"column": 30,
"line": 1,
},
"start": Object {
"column": 28,
"line": 1,
},
},
}
`;

exports[`follow-imports one level namespace import 1`] = `
Object {
Expand Down

0 comments on commit 6220d59

Please sign in to comment.