Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Commit

Permalink
fix: properly handle esm exports when file has imports too
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 13, 2019
1 parent a1b2d83 commit 2f0cadf
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"kind": "class",
"methods": [
{
"name": "foo",
"lineno": 4
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'typescript'

export default class UserController {
public foo () {
return this.bar()
}

private bar () {}
}
9 changes: 9 additions & 0 deletions fixtures/typescript/export-class-declaration/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"kind": "class",
"methods": [
{
"name": "foo",
"lineno": 2
}
]
}
7 changes: 7 additions & 0 deletions fixtures/typescript/export-class-declaration/source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default class UserController {
public foo () {
return this.bar()
}

private bar () {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"kind": "class",
"methods": [
{
"name": "foo",
"lineno": 4
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'typescript'

class UserController {
public foo () {
return this.bar()
}

private bar () {}
}

export default UserController
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"kind": "object",
"methods": [
{
"name": "foo",
"lineno": 4
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'typescript'

const foo = {
foo () {},
}

export default foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"kind": "object",
"methods": [
{
"name": "foo",
"lineno": 2
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const foo = {
foo () {},
}
export default foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"kind": "object",
"methods": [
{
"name": "foo",
"lineno": 4
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'typescript'

export default {
foo () {},
}
9 changes: 9 additions & 0 deletions fixtures/typescript/export-object-literal/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"kind": "object",
"methods": [
{
"name": "foo",
"lineno": 2
}
]
}
3 changes: 3 additions & 0 deletions fixtures/typescript/export-object-literal/source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
foo () {},
}
41 changes: 31 additions & 10 deletions src/Extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,47 @@ export class Extractor {
}

/**
* Returns the expression next to commonjs or esm default export. Esm
* named exports are not entertained, since AdonisJs IoC container
* bindings doesn't allow them as well.
* Finds esm export expresions
*/
private _getExportExpression (source: ts.SourceFile): ts.Expression | ts.ClassDeclaration | null {
let expression: ts.Expression | null = null
private _findEsmExportExpression (node: ts.Statement): null | ts.ClassDeclaration | ts.Expression {
/**
* Export assignment expressions are returned as it is
*/
if (ts.isExportAssignment(node)) {
return node.expression
}

/**
* Node is class declaration, so we need to check it's modifiers (if any)
* to see if it has `export default`
*/
if (!ts.isClassDeclaration(node) || !node.modifiers) {
return null
}

/**
* Return export defaults right away
* Has export default modifier
*/
if (source['externalModuleIndicator']) {
debug('located default export "%s"', source['externalModuleIndicator'].kind)
return source['externalModuleIndicator'] as (ts.Expression | ts.ClassDeclaration)
if (node.modifiers.find((modifier) => ts.SyntaxKind.DefaultKeyword === modifier.kind)) {
return node
}

return null
}

/**
* Returns the expression next to commonjs or esm default export. Esm
* named exports are not entertained, since AdonisJs IoC container
* bindings doesn't allow them as well.
*/
private _getExportExpression (source: ts.SourceFile): ts.Expression | ts.ClassDeclaration | null {
let expression: ts.Expression | ts.ClassDeclaration | null = null

/**
* Look for commonJs style module.exports and exports
*/
for (let statement of source.statements) {
expression = this._findCommonJsExportExpression(statement)
expression = this._findCommonJsExportExpression(statement) || this._findEsmExportExpression(statement)
if (expression) {
break
}
Expand Down

0 comments on commit 2f0cadf

Please sign in to comment.