Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should generate AST for element with empty attribute', function(done) {
const tpl = '<template id="x-foo">'
+ '<span></span></template>';
const res = trucks.compile(tpl);
expect(res.list).to.be.an('array').to.have.length(1);
// component id
expect(res.list[0].id).to.eql('x-foo');
// function body AST
expect(res.list[0].body).to.be.an('object');
const result = babel.transformFromAst(res.list[0].body);
expect(result.code).to.eql('skate.vdom.element("span", {\n'
+ ' "icon": ""\n'
+ '});');
done();
});
const tpl = '<template id="x-foo">'
+ ' <span></span>'
+ ' <em></em>'
+ '</template>';
const res = trucks.compile(tpl, {normalize: false});
expect(res.list).to.be.an('array').to.have.length(1);
// component id
expect(res.list[0].id).to.eql('x-foo');
// function body AST
expect(res.list[0].body).to.be.an('object');
const result = babel.transformFromAst(res.list[0].body);
expect(result.code).to.eql(
'skate.vdom.text(" ");\n' +
'skate.vdom.element("span");\n' +
'skate.vdom.text(" ");\n' +
'skate.vdom.element("em");'
);
done();
});
moduleIds: !opts.anonymous,
moduleId: !opts.anonymous && load.name,
code: true,
resolveModuleSource: function(dep) {
if (opts.normalize)
return load.depMap[dep];
else
return dep;
}
};
var source = load.metadata.originalSource || load.source;
var output;
if (load.metadata.ast)
output = babel.transformFromAst(load.metadata.ast, source, babelOptions);
else
output = babel.transform(source, babelOptions);
// NB this can be removed with merging of ()
if (opts.systemGlobal != 'System')
output.code = output.code.replace(/(\s|^)System\.register\(/, '$1' + opts.systemGlobal + '.register(');
// for some reason Babel isn't respecting sourceFileName...
if (output.map)
output.map.sources[0] = load.path;
return Promise.resolve({
source: output.code,
sourceMap: output.map
});
}
export function compileAst(ast, babelCoreOpts, watch = false) {
try {
return transformFromAst(ast, babelCoreOpts);
} catch (err) {
if (watch) {
console.error(err);
return { ignored: true };
} else {
throw err;
}
}
}
function processOthers (code, filePath, fileType) {
let ast = wxTransformer({
code,
sourcePath: filePath,
isNormal: true,
isTyped: Util.REG_TYPESCRIPT.test(filePath),
adapter: 'h5'
}).ast
let taroImportDefaultName
let hasAddNervJsImportDefaultName = false
let hasJSX = false
let isPage = fileType === FILE_TYPE.PAGE
let hasComponentDidMount = false
let hasComponentDidShow = false
ast = babel.transformFromAst(ast, '', {
plugins: [
[require('babel-plugin-danger-remove-unused-import'), { ignore: ['@tarojs/taro', 'react', 'nervjs'] }]
]
}).ast
const ClassDeclarationOrExpression = {
enter (astPath) {
const node = astPath.node
if (!node.superClass) return
if (
node.superClass.type === 'MemberExpression' &&
node.superClass.object.name === taroImportDefaultName
) {
node.superClass.object.name = taroImportDefaultName
if (node.id === null) {
const renameComponentClassName = '_TaroComponentClass'
return function processJavaScriptCode(code, options){
code = removeSourceMapIfAny(code)
const ast = babylon.parse(code, {
strict: false,
allowReturnOutsideFunction: true,
sourceFilename: options !== undefined ? options.filename + ".dontprocess" : undefined
});
babelPlugin.sourceCode = code;
var res = babel.transformFromAst(ast, code, {
sourceMaps: true,
compact: false,
plugins: [
sharedPlugin,
babelPlugin
]
});
res.map.sourcesContent = undefined
delete babelPlugin.sourceCode
return res
}
}
function stringifyCommonJSEntry(exportStatement) {
return babel.transformFromAst({
type: 'File',
program: {
type: 'Program',
sourceType: 'module',
body: [exportStatement]
}
}).code;
}
function parseComponentExportAst (ast, componentName, componentPath, componentType) {
let componentRealPath = null
let importExportName
ast = babel.transformFromAst(ast, '', {
plugins: [
[require('babel-plugin-transform-define').default, constantsReplaceList]
]
}).ast
traverse(ast, {
ExportNamedDeclaration (astPath) {
const node = astPath.node
const specifiers = node.specifiers
const source = node.source
if (source && source.type === 'StringLiteral') {
specifiers.forEach(specifier => {
const exported = specifier.exported
if (_.kebabCase(exported.name) === componentName) {
componentRealPath = Util.resolveScriptPath(path.resolve(path.dirname(componentPath), source.value))
}
})
export function compileFromAst(src: babel.types.Program): h.Result {
try {
const babelOpts = {
plugins: [ [ () => ({ visitor }), {
reserved: [],
global: t.memberExpression(t.identifier('$rts'), t.identifier('g'))
} ] ],
babelrc: false,
ast: false,
code: true,
minified: false,
comments: false,
};
const result = babel.transformFromAst(src, undefined, babelOpts);
if (typeof result.code === 'string') {
return h.ok(result.code);
}
else {
return h.error('compile failed: no code returned');
}
}
catch (exn) {
return h.error(exn.toString());
}
}
plugs.forEach(trs => {
const res = babel.transformFromAst(ast, code, {
plugins: [...trs],
babelrc: false,
});
code = res.code;
ast = res.ast;
});