Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return {
type: "Resolve",
node: getExpressionNodeFromCommentValue(match[1])
};
} else if (PROMISE_REJECT_COMMENT_PATTERN.test(message)) {
const match = message.match(PROMISE_REJECT_COMMENT_PATTERN);
if (!match) {
throw new Error("Can not Parse: // => Reject: value");
}
return {
type: "Reject",
node: getExpressionNodeFromCommentValue(match[1])
};
}
try {
return parseExpression(string);
} catch (e) {
console.error(`Can't parse comments // => expression`);
throw e;
}
}
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 getExports(node, counter) {
const { value, type } = node;
if (type === 'jsx') {
if (STORY_REGEX.exec(value)) {
// Single story
const ast = parser.parseExpression(value, { plugins: ['jsx'] });
const storyExport = genStoryExport(ast, counter);
return storyExport && { stories: storyExport };
}
if (PREVIEW_REGEX.exec(value)) {
// Preview, possibly containing multiple stories
const ast = parser.parseExpression(value, { plugins: ['jsx'] });
return { stories: genPreviewExports(ast, counter) };
}
if (META_REGEX.exec(value)) {
// Preview, possibly containing multiple stories
const ast = parser.parseExpression(value, { plugins: ['jsx'] });
return { meta: genMeta(ast) };
}
}
return null;
}
function getStories(node, counter) {
const { value, type } = node;
// Single story
if (type === 'jsx') {
if (STORY_REGEX.exec(value)) {
// Single story
const ast = parser.parseExpression(value, { plugins: ['jsx'] });
const storyExport = genStoryExport(ast, value, counter);
return storyExport && [storyExport];
}
if (PREVIEW_REGEX.exec(value)) {
// Preview, possibly containing multiple stories
const ast = parser.parseExpression(value, { plugins: ['jsx'] });
return genPreviewExports(ast, value, counter);
}
}
return null;
}
function getExports(node, counter) {
const { value, type } = node;
if (type === 'jsx') {
if (STORY_REGEX.exec(value)) {
// Single story
const ast = parser.parseExpression(value, { plugins: ['jsx'] });
const storyExport = genStoryExport(ast, counter);
return storyExport && { stories: storyExport };
}
if (PREVIEW_REGEX.exec(value)) {
// Preview, possibly containing multiple stories
const ast = parser.parseExpression(value, { plugins: ['jsx'] });
return { stories: genPreviewExports(ast, counter) };
}
if (META_REGEX.exec(value)) {
// Preview, possibly containing multiple stories
const ast = parser.parseExpression(value, { plugins: ['jsx'] });
return { meta: genMeta(ast) };
}
}
return null;
}
function getStories(node, counter) {
const { value, type } = node;
// Single story
if (type === 'jsx') {
if (STORY_REGEX.exec(value)) {
// Single story
const ast = parser.parseExpression(value, { plugins: ['jsx'] });
const storyExport = genStoryExport(ast, value, counter);
return storyExport && [storyExport];
}
if (PREVIEW_REGEX.exec(value)) {
// Preview, possibly containing multiple stories
const ast = parser.parseExpression(value, { plugins: ['jsx'] });
return genPreviewExports(ast, value, counter);
}
}
return null;
}
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,