-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc6c9f6
commit eb441be
Showing
17 changed files
with
308 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
dist | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as parser from '@babel/parser'; | ||
import traverse from '@babel/traverse'; | ||
|
||
export interface ImportType { | ||
name: string; | ||
importedName: 'default' | 'namespace' | string; | ||
} | ||
|
||
export interface ImportTypes { | ||
[key: string]: ImportType[]; | ||
} | ||
|
||
export const traverseImports = (results: ImportTypes) => { | ||
return { | ||
ImportDeclaration: (path: any) => { | ||
const node = path.node; | ||
const imports: ImportType[] = node.specifiers.map((specifier: any) => { | ||
let importedName; | ||
switch (specifier.type) { | ||
case 'ImportDefaultSpecifier': | ||
importedName = 'default'; | ||
break; | ||
case 'ImportNamespaceSpecifier': | ||
importedName = 'namespace'; | ||
break; | ||
|
||
default: | ||
importedName = specifier.imported | ||
? specifier.imported.name | ||
: specifier.local.name; | ||
} | ||
return { | ||
name: specifier.local.name, | ||
importedName, | ||
}; | ||
}); | ||
if (Array.isArray(results[node.source.value])) { | ||
results[node.source.value] = [ | ||
...results[node.source.value], | ||
...imports, | ||
]; | ||
} else { | ||
results[node.source.value] = imports; | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export const extractImports = ( | ||
source: string, | ||
parserOptions?: parser.ParserOptions, | ||
) => { | ||
const results: ImportTypes = {}; | ||
const ast = parser.parse(source, parserOptions); | ||
|
||
traverse(ast, traverseImports(results)); | ||
return results; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
core/instrument/test/__snapshots__/extract-imports.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`extract-imports all imports 1`] = ` | ||
Object { | ||
"./buttons": Array [ | ||
Object { | ||
"importedName": "Button", | ||
"name": "Btn", | ||
}, | ||
], | ||
"buttons": Array [ | ||
Object { | ||
"importedName": "default", | ||
"name": "Button1", | ||
}, | ||
Object { | ||
"importedName": "namespace", | ||
"name": "Button2", | ||
}, | ||
Object { | ||
"importedName": "Button", | ||
"name": "Button", | ||
}, | ||
], | ||
"react": Array [ | ||
Object { | ||
"importedName": "default", | ||
"name": "React", | ||
}, | ||
Object { | ||
"importedName": "FC", | ||
"name": "FC", | ||
}, | ||
Object { | ||
"importedName": "MouseEvent", | ||
"name": "MouseEvent", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`extract-imports default import 1`] = ` | ||
Object { | ||
"buttons": Array [ | ||
Object { | ||
"importedName": "default", | ||
"name": "Button", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`extract-imports mixed import 1`] = ` | ||
Object { | ||
"./react": Array [ | ||
Object { | ||
"importedName": "default", | ||
"name": "React", | ||
}, | ||
Object { | ||
"importedName": "FC", | ||
"name": "FC", | ||
}, | ||
Object { | ||
"importedName": "MouseEvent", | ||
"name": "MouseEvent", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`extract-imports named alias import 1`] = ` | ||
Object { | ||
"buttons": Array [ | ||
Object { | ||
"importedName": "Button", | ||
"name": "Btn", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`extract-imports named import 1`] = ` | ||
Object { | ||
"buttons": Array [ | ||
Object { | ||
"importedName": "Button", | ||
"name": "Button", | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`extract-imports namespace import 1`] = ` | ||
Object { | ||
"buttons": Array [ | ||
Object { | ||
"importedName": "namespace", | ||
"name": "Button", | ||
}, | ||
], | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Button1 from 'buttons'; | ||
import * as Button2 from 'buttons'; | ||
import { Button } from 'buttons'; | ||
import React, { FC, MouseEvent } from 'react'; | ||
import { Button as Btn } from './buttons'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import Button from 'buttons'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import React, { FC, MouseEvent } from './react'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { Button as Btn } from 'buttons'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import { Button } from 'buttons'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import * as Button from 'buttons'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const myStory = () => {}; | ||
|
||
export { myStory }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const myStory = () => {}; | ||
|
||
export { myStory as exportedStory }; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { myStory } from 'stories.tsx'; | ||
|
||
export { myStory }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.