-
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
0575fbb
commit 628d09f
Showing
27 changed files
with
637 additions
and
721 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
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
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,57 @@ | ||
import fs from 'fs'; | ||
import * as parser from '@babel/parser'; | ||
import traverse, { TraverseOptions } from '@babel/traverse'; | ||
import { Component, JSXNode, JSXTree } from '@component-controls/core'; | ||
import { collectAttributes } from './extract-attributes'; | ||
import { InstrumentOptions } from '../types'; | ||
|
||
type JSXLinkedNode = JSXNode & { parent?: JSXNode }; | ||
type JSXLinkedTree = JSXLinkedNode[]; | ||
|
||
export const traverseJSX = (jsx: JSXLinkedTree): TraverseOptions => { | ||
let current: JSXLinkedNode = { children: jsx }; | ||
return { | ||
JSXElement: { | ||
enter(path) { | ||
const node = path.node; | ||
const name = node.openingElement.name as { name: string }; | ||
const attributes = collectAttributes(node.openingElement); | ||
const jsxNode: JSXLinkedNode = { | ||
name: name.name, | ||
parent: current, | ||
children: [], | ||
attributes: Object.keys(attributes), | ||
}; | ||
if (current.children) { | ||
current.children.push(jsxNode); | ||
} | ||
current = jsxNode; | ||
}, | ||
|
||
exit() { | ||
current = current.parent as JSXLinkedNode; | ||
}, | ||
}, | ||
}; | ||
}; | ||
export const analyze_components = ( | ||
component: Component, | ||
filePath: string, | ||
options?: InstrumentOptions, | ||
): void => { | ||
const { parser: parserOptions } = options || {}; | ||
const source = fs.readFileSync(filePath, 'utf8'); | ||
const ast = parser.parse(source, parserOptions); | ||
const jsx: JSXLinkedTree = []; | ||
traverse(ast, traverseJSX(jsx)); | ||
const mapJSXTree = (input?: JSXLinkedTree): JSXTree => { | ||
return input | ||
? // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
input.map(({ parent, children, ...rest }) => ({ | ||
children: mapJSXTree(children), | ||
...rest, | ||
})) | ||
: []; | ||
}; | ||
component.jsx = mapJSXTree(jsx); | ||
}; |
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
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
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.