-
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
7832976
commit 69c2313
Showing
5 changed files
with
156 additions
and
26 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Stories, Story } from '../types'; | ||
import traverse from '@babel/traverse'; | ||
import { extractFunctionParameters } from './get-function-parameters'; | ||
|
||
export const extractCSFStories = (stories: Stories) => ({ | ||
ExportNamedDeclaration: (path: any) => { | ||
const { | ||
declaration: { | ||
declarations: [declaration], | ||
}, | ||
} = path.node; | ||
const el = declaration.init.body; | ||
const name = declaration.id.name; | ||
const story: Story = { | ||
source: { | ||
start: { | ||
column: el.loc.start.column, | ||
line: el.loc.start.line, | ||
}, | ||
end: { | ||
column: el.loc.end.column, | ||
line: el.loc.end.line, | ||
}, | ||
}, | ||
}; | ||
traverse(path.node, extractFunctionParameters(story), path.scope, path); | ||
stories[name] = story; | ||
}, | ||
}); |
51 changes: 51 additions & 0 deletions
51
core/instrument/src/babel-traverse/get-function-parameters.ts
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,51 @@ | ||
import { Story, CodeSource, StoryParameters } from '../types'; | ||
|
||
interface ASTPropNode { | ||
name?: string; | ||
loc: CodeSource; | ||
properties?: any; | ||
} | ||
export const extractFunctionParameters = (story: Story) => ({ | ||
ArrowFunctionExpression: (path: any) => { | ||
const node = path.node; | ||
if (!story.parameters) { | ||
story.parameters = []; | ||
} | ||
const pushParams = (node: ASTPropNode, parameters: StoryParameters) => { | ||
const loc = { | ||
start: { | ||
column: node.loc.start.column, | ||
line: node.loc.start.line, | ||
}, | ||
end: { | ||
column: node.loc.end.column, | ||
line: node.loc.end.line, | ||
}, | ||
}; | ||
if (node.name) { | ||
parameters.push({ | ||
value: node.name, | ||
loc, | ||
}); | ||
} else if (node.properties) { | ||
const nestedParameters: StoryParameters = []; | ||
parameters.push({ | ||
value: nestedParameters, | ||
loc, | ||
}); | ||
node.properties.forEach(({ value }: { value: ASTPropNode }) => { | ||
pushParams(value, nestedParameters); | ||
}); | ||
} | ||
}; | ||
if (node.params) { | ||
node.params.forEach( | ||
(p: { name: string; loc: CodeSource; properties?: any }) => { | ||
if (story.parameters) { | ||
pushParams(p, story.parameters); | ||
} | ||
}, | ||
); | ||
} | ||
}, | ||
}); |
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,14 +1,14 @@ | ||
import * as parser from '@babel/parser'; | ||
import traverse from '@babel/traverse'; | ||
import { extractCSFStories } from './babel-traverse/get-csf-stories'; | ||
export * from './types'; | ||
|
||
export const parseSource = (source: string) => { | ||
const ast = parser.parse(source, { | ||
sourceType: 'module', | ||
plugins: ['jsx', 'typescript'], | ||
}); | ||
traverse(ast, { | ||
enter(path) { | ||
console.log(path); | ||
}, | ||
}); | ||
const stories = {}; | ||
traverse(ast, extractCSFStories(stories)); | ||
return stories; | ||
}; |
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,24 @@ | ||
export interface SourceLoc { | ||
line: number; | ||
column: number; | ||
} | ||
|
||
export interface CodeSource { | ||
start: SourceLoc; | ||
end: SourceLoc; | ||
} | ||
export interface StoryParameter { | ||
value: string | StoryParameters; | ||
loc: CodeSource; | ||
} | ||
|
||
export type StoryParameters = StoryParameter[]; | ||
|
||
export interface Story { | ||
parameters?: StoryParameters; | ||
source: CodeSource; | ||
} | ||
|
||
export interface Stories { | ||
[name: string]: Story; | ||
} |
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