Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function(content, { relativePath, identifier }) {
const ast = parser.parse(content, {
sourceType: 'module',
plugins: ['jsx', 'decorators-legacy', 'typescript'],
});
traverse(ast, {
Program({ node }) {
// add import
const { body } = node;
const lastImportSit = findLastIndex(body, item => {
return t.isImportDeclaration(item);
});
const newImport = t.ImportDeclaration(
[t.ImportDefaultSpecifier(t.identifier(upperCamelCase(identifier)))],
t.stringLiteral(relativePath),
);
body.splice(lastImportSit + 1, 0, newImport);
},
const parse = (script) => {
try {
return babelParser.parse(script, {
sourceType: 'module',
plugins: defaultBabelPlugins,
});
} catch (e) {
// try to parse with flow syntax
return babelParser.parse(script, {
sourceType: 'module',
plugins: flowBabelPlugins,
});
}
};
enter(path) {
if (path.node.type === 'ExportDefaultDeclaration') {
// 拿到export ddefault new Method(); 这一行代码
let exportCode = generate["default"](path.node);
// 拿到 new Method(); 这一段代码
let declarationCode = generate["default"](path.node.declaration);
// 得到 export default __OBJECT__WARPPER__(new Method());
let codeSeg = exportCode.code.replace(declarationCode.code, '__OBJECT__WRAPPER__(' + declarationCode.code + ', __CML_ERROR__, __enableTypes__, __CHECK__DEFINES__ )');
// 转成ast
let replacement = parser.parse(codeSeg, {
plugins: parsePlugins,
sourceType: 'module'
});
traverse["default"].removeProperties(replacement);
// 替换
path.replaceWith(replacement.program.body[0]);
path.stop();
}
}
});
genInlineStyles(src: string | undefined): void {
if (src) {
// We get back a AST module which may have three pieces:
// 1) import statements
// 2) the inline function
// 3) default export
// We need to separate the imports and change the default export for a correct inlining
const importDeclarations: t.ImportDeclaration[] = [];
const styleBody: t.Statement[] = [];
// Parse the generated module code and return it's body.
const parsed = babylon.parse(src, { sourceType: 'module' });
const inlineStylesAst = parsed.program.body;
inlineStylesAst.forEach(node => {
if (t.isImportDeclaration(node)) {
importDeclarations.push(node);
} else if (t.isExportDefaultDeclaration(node)) {
const stylesheetDeclaration = t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('stylesheets'),
node.declaration as t.ArrayExpression
),
]);
styleBody.push(stylesheetDeclaration);
} else {
styleBody.push(node);
function parseFileToOutline(code) {
const types = [];
const ast = parse(code, {sourceType: 'module', plugins: ['flow']});
traverse(ast, {
ExportNamedDeclaration(path) {
if (t.isTypeAlias(path.node.declaration)) {
const typeNode = {
name: path.node.declaration.id.name,
lineStart: path.node.declaration.id.loc.start.line,
children: [],
};
if (t.isObjectTypeAnnotation(path.node.declaration.right)) {
typeNode.children = path.node.declaration.right.properties.map(
property => {
if (t.isObjectTypeProperty(property)) {
if (t.isLiteral(property.key)) {
return {
name: property.key.value,
function parse(code) {
return babelParser.parse(code, {
sourceType: 'module',
plugins: ['flow', 'jsx'],
});
}
function find(text: string): $ReadOnlyArray {
const result: Array = [];
const ast = babylon.parse(text, BABYLON_OPTIONS);
const visitors = {
CallExpression: node => {
const callee = node.callee;
if (
!(
(callee.type === 'Identifier' &&
CREATE_CONTAINER_FUNCTIONS[callee.name]) ||
(callee.kind === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.value === 'Relay' &&
callee.property.type === 'Identifier' &&
CREATE_CONTAINER_FUNCTIONS[callee.property.name])
)
) {
traverse(node, visitors);
function parseCode(code) {
return babelParser.parse(code, parserOption);
}
function transformJsStringToJsAst(string) {
return babelParser.parse(string, {
sourceType: "module",
plugins: ["classProperties", "asyncGenerators", "jsx", "typescript"]
});
}