Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
try {
siteStories(cmp); // Checks for existence of this
const cmpStoriesAST = astFromBuffer(componentStories(cmp));
const imports = importsFromAST(cmpStoriesAST).filter(isExampleImport);
const isExampleImported = (ex) =>
imports.some((i) => ex.includes(path.basename(i.source.value)));
const examples = componentExamples(cmp);
const isValid = examples.every(isExampleImported);
if (!isValid) {
const missing = examples.filter((e) => !isExampleImported(e));
// babel-traverse walks the AST and modifies it
babel.traverse(cmpStoriesAST, createAddDocsImportVisitor(missing));
// babel-generator converts AST into source code
const newBody = generator.default(cmpStoriesAST).code; // Note, this doesn't have good formatting, run prettier on the files afterward
// eslint-disable-next-line no-console
console.log(
`${cmp} will get the following files added: \n\t${missing.join(
',\n\t'
)}`
);
fs.writeFileSync(storiesFor(cmp), newBody);
}
} catch (error) {
if (
error.code === 'ENOENT' &&
error.path &&
error.path.match(/storybook-stories/)
) {
// console.log(`No stories for: ${cmp}`);
} else if (
files.forEach(function (filename) {
var buffer = fs.readFileSync(filename, "utf8");
try {
var translationsForFile = translateMessagesToBundle(buffer, translations, { inputFormat: format });
} catch (e) {
console.error(chalk.bold.red("\nError in file " + filename + ":"));
console.error(e);
console.error(e.stack);
e.node && console.error(generate(e.node));
process.exit(1);
}
Object.keys(translationsForFile.bundle).forEach(function (message) {
bundle[message] = translationsForFile.bundle[message];
});
Object.keys(translationsForFile.missing).forEach(function (message) {
missing[message] = message;
});
});
if (typeof ast === 'string') {
opt.code = ast;
} else {
/*
目前babel 6 版本的 遍历功能看起来对ast有污染,无法做到ast复用去minify
所以需要fix一次ast
*/
opt.ast = fixAst(ast);
}
opt.config = minifyOpt;
return esminify.minify(opt);
} else {
if (typeof ast === 'string') {
return ast;
}
let out = babelGenerator(ast, {
retainLines: true,
filename: qpath,
quotes: 'single'
});
return out.code;
}
}
t.identifier(renameComponentClassName),
node.superClass,
node.body,
node.decorators || []
)
);
componentClassName = renameComponentClassName;
} else {
componentClassName = node.id.name;
}
}
}
}
});
return {
code: generate(ast).code,
taroImportDefaultName,
componentClassName,
styleFiles
};
}
if (op.key.name === 'path' && op.value.value === path) {
return true;
}
return false;
});
});
if (!menuExist) {
node.init.elements.push(menuNode({ name, path, icon }));
}
}
},
});
fs.writeFileSync(
menuConfigFilePath,
prettier.format(generator(menuConfigAST).code, config.prettier)
);
};
const specifiers = [];
if (defaultImport) {
specifiers.push(t.importDefaultSpecifier(t.identifier(defaultImport)));
}
if (namespaceImport) {
specifiers.push(t.importNamespaceSpecifier(t.identifier(namespaceImport)));
}
names.forEach((n) => {
const local = t.identifier(n);
const imported = local;
specifiers.push(t.importSpecifier(local, imported));
});
const node = t.importDeclaration(specifiers, t.stringLiteral(moduleSource));
const code = generate(node, babelGeneratorOptions).code;
changes.push({
start: targetImportPos,
end: targetImportPos,
replacement: `${code}\n`,
});
}
return changes;
}
[t.identifier('prototype'), t.objectExpression(propertyDefinitions)]
);
statements.push(
t.expressionStatement(
t.assignmentExpression(
'=',
t.memberExpression(t.identifier('module'), t.identifier('exports')),
t.functionExpression(
null,
[t.identifier('prototype')],
t.blockStatement([t.expressionStatement(definePropertiesCall)])
)
)
)
);
out_file.write(generate(t.program(statements)).code + '\n');
out_file.end(function(err) {
if (err) {
throw err;
}
});
node.body[index] = null
}
}
})
//@fix 注释掉用来解决小程序报错
//node.body.push(template(`Component(require('${taroMiniAppFrameworkPath}').default.createComponent(${exportVariableName}))`, babylonConfig)())
break
default:
break
}
}
}
})
return {
code: generate(ast).code,
styleFiles,
scriptFiles,
jsonFiles,
configObj,
mediaFiles,
componentClassName
}
}
.reduce(function(cs, name) {
cs[name] = generate(components[name].body).code;
return cs;
}, {});
}
function transform(src, filename, options) {
options = options || {};
const OLD_BABEL_ENV = process.env.BABEL_ENV;
process.env.BABEL_ENV = options.dev ? 'development' : 'production';
try {
const babelConfig = buildBabelConfig(filename, options);
const {ast} = babel.transform(src, babelConfig);
const result = generate(ast, {
comments: false,
compact: false,
filename,
sourceFileName: filename,
sourceMaps: true,
}, src);
return {
ast,
code: result.code,
filename,
map: options.generateSourceMaps ? result.map : result.rawMappings.map(compactMapping),
};
} finally {
process.env.BABEL_ENV = OLD_BABEL_ENV;
}