Skip to content

Commit

Permalink
feat: extract exports - class
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 4, 2020
1 parent 6220d59 commit 55bdb30
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 4 deletions.
34 changes: 30 additions & 4 deletions core/instrument/src/babel/extract-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,17 @@ export const traverseExports = (results: ExportTypes) => {
};
return {
ExportDefaultDeclaration: (path: any) => {
const { declaration } = path.node;
const internalName = declaration
? declaration.id
? declaration.id.name
: declaration.name || EXPORT_DEFAULT
: EXPORT_DEFAULT;
const localExport = localExports[internalName];
results.default = {
name: EXPORT_DEFAULT,
internalName: EXPORT_DEFAULT,
loc: sourceLocation(path.node.loc),
internalName,
loc: localExport ? localExport.loc : sourceLocation(path.node.loc),
};
},
ImportDeclaration: (path: any) => {
Expand Down Expand Up @@ -81,12 +88,23 @@ export const traverseExports = (results: ExportTypes) => {
});
}
},
ClassDeclaration: (path: any) => {
const node = path.node.declaration || path.node;
if (node.id) {
const name = node.id.name;
localExports[name] = {
name,
internalName: name,
loc: sourceLocation(node.body ? node.body.loc : node.loc),
};
}
},

ExportSpecifier: (path: any) => {
const { node } = path;
const localName = node.local.name;
const exportedName = node.exported.name;
const namedExport = localExports[localName];

if (namedExport) {
namedExport.internalName = namedExport.name;
namedExport.name = exportedName;
Expand All @@ -101,7 +119,6 @@ export const traverseExports = (results: ExportTypes) => {
const { declaration, specifiers, source } = path.node;
if (declaration) {
const { declarations } = declaration;

if (Array.isArray(declarations)) {
declarations.forEach(declaration => {
const namedExport = extractArrowFunction(declaration);
Expand All @@ -116,6 +133,15 @@ export const traverseExports = (results: ExportTypes) => {
results.named[name] = namedExport;
}
});
} else {
const name = declaration.id.name;
results.named[name] = {
name,
internalName: name,
loc: sourceLocation(
declaration.body ? declaration.body.loc : path.node.loc,
),
};
}
} else if (specifiers) {
specifiers.forEach((specifier: any) => {
Expand Down
82 changes: 82 additions & 0 deletions core/instrument/test/__snapshots__/extract-exports.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,46 @@ Object {
}
`;

exports[`extract-exports export default class 1`] = `
Object {
"default": Object {
"internalName": "Button",
"loc": Object {
"end": Object {
"column": 1,
"line": 7,
},
"start": Object {
"column": 0,
"line": 3,
},
},
"name": "default",
},
"named": Object {},
}
`;

exports[`extract-exports export default class export 1`] = `
Object {
"default": Object {
"internalName": "Button",
"loc": Object {
"end": Object {
"column": 1,
"line": 7,
},
"start": Object {
"column": 37,
"line": 3,
},
},
"name": "default",
},
"named": Object {},
}
`;

exports[`extract-exports export from import 1`] = `
Object {
"named": Object {
Expand Down Expand Up @@ -66,6 +106,48 @@ Object {
}
`;

exports[`extract-exports export named class 1`] = `
Object {
"named": Object {
"Button": Object {
"internalName": "Button",
"loc": Object {
"end": Object {
"column": 1,
"line": 7,
},
"start": Object {
"column": 44,
"line": 3,
},
},
"name": "Button",
},
},
}
`;

exports[`extract-exports export named class export 1`] = `
Object {
"named": Object {
"Button": Object {
"internalName": "Button",
"loc": Object {
"end": Object {
"column": 1,
"line": 7,
},
"start": Object {
"column": 37,
"line": 3,
},
},
"name": "Button",
},
},
}
`;

exports[`extract-exports named const export 1`] = `
Object {
"named": Object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

class Button extends React.Component {
render() {
return <button>Hello, {this.props.name}</button>;
}
}

export default Button;
7 changes: 7 additions & 0 deletions core/instrument/test/examples/exports/button-default-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

export default class Button extends React.Component {
render() {
return <button>Hello, {this.props.name}</button>;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

class Button extends React.Component {
render() {
return <button>Hello, {this.props.name}</button>;
}
}

export { Button };
7 changes: 7 additions & 0 deletions core/instrument/test/examples/exports/button-named-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

export class Button extends React.Component {
render() {
return <button>Hello, {this.props.name}</button>;
}
}
25 changes: 25 additions & 0 deletions core/instrument/test/extract-exports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,29 @@ describe('extract-exports', () => {
extractExportsForFile('./examples/exports/export-all-from.js'),
).toMatchSnapshot();
});

it('export named class', () => {
expect(
extractExportsForFile('./examples/exports/button-named-class.js'),
).toMatchSnapshot();
});

it('export named class export', () => {
expect(
extractExportsForFile('./examples/exports/button-named-class-export.js'),
).toMatchSnapshot();
});

it('export default class', () => {
expect(
extractExportsForFile('./examples/exports/button-default-class.js'),
).toMatchSnapshot();
});
it('export default class export', () => {
expect(
extractExportsForFile(
'./examples/exports/button-default-class-export.js',
),
).toMatchSnapshot();
});
});

0 comments on commit 55bdb30

Please sign in to comment.