Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fs.readFile(path, {encoding: 'utf-8'}, function(err, data) {
if(err) {
return cb(err, null);
}
// first apply a jsx transformation
data = jsx.transform(data, {
harmony: true,
es6module: true
});
cb(null, acorn.parse(data, {
sourceType: 'module',
ecmaVersion: "6",
locations: true,
plugins: {
'jsx': true
}
}));
});
}
sourceType: 'module',
plugins: { jsx: { allowNamespaces: false } },
onComment: function (block, text, start, end, line/*, column*/) {
text = text.match(commentRegex) && text.replace(/^\//, '').trim();
if (!text)
return;
astComments.push({
line : line,
value: text
});
},
locations: true
}, options.parserOptions && JSON.parse(options.parserOptions));
var ast = parser.parse(source, parserOptions);
// finds comments that end on the previous line
function findComments(comments, line) {
return comments.map(function (node) {
var commentLine = node.line.line;
if (commentLine === line || commentLine + 1 === line) {
return node.value;
}
}).filter(Boolean).join('\n');
}
walk.simple(ast, {'CallExpression': function (node) {
var arg = getTranslatable(node, options);
if (!arg)
return;
fs.readFile(path, { encoding: 'utf-8' }, function (err, data) {
if (err) {
return cb(err, null);
}
// first apply a jsx transformation
data = jsx.transform(data, {
harmony: true,
es6module: true
});
cb(null, acorn.parse(data, {
sourceType: 'module',
ecmaVersion: '6',
locations: true,
plugins: {
'jsx': true
}
}));
});
}
function createSlot(node, parentNode) {
let slotName = 'default';
node.openingElement.attributes.forEach((attr) => {
if (attr.name.name === 'name') slotName = attr.name.name;
});
const slotNode = acorn.parse(`this.slots.${slotName}`).body[0].expression;
const children = node.children;
delete node.children;
delete node.openingElement;
delete node.closingElement;
Object.assign(node, slotNode);
let index = 0;
const parentElements = parentNode.arguments || parentNode.children || parentNode.elements;
children.forEach((slotChild) => {
let child = slotChild;
if (child.type === 'JSXText') {
child = transformJSXText(slotChild);
if (!child) return;
}
const slotChildNode = acorn.parse(`!this.slots.${slotName} && child`).body[0].expression;
node.declaration.properties.forEach((prop) => {
if (prop.key.name === 'render' && prop.method === true) {
const createElementNode = acorn.parse('var _h = this.$createElement;').body[0];
prop.value.body.body.unshift(createElementNode);
}
});
}
function createSlot(node) {
let slotName = 'default';
node.openingElement.attributes.forEach((attr) => {
if (attr.name.name === 'name') slotName = attr.name.name;
});
const children = [];
node.children.forEach((slotChild) => {
let child = slotChild;
if (child.type === 'JSXText') {
child = transformJSXText(slotChild);
if (!child) return;
}
children.push(slotChild);
});
const slotNode = acorn.parse(`this.slots.${slotName}${children.length ? ' || []' : ''}`).body[0].expression;
if (children.length) {
slotNode.right.elements = children;
}
delete node.children;
delete node.openingElement;
delete node.closingElement;
Object.assign(node, slotNode);
}
function transformJSXElement(node, parentNode) {
return new Promise((resolve, reject) => {
try {
var ast = acorn.parse(code, {plugins: {jsx: true}})
var tokens = ast.body.map((node) => ({
type: node.type,
node,
codeString: escodegen.generate(node)
}))
resolve(tokens)
} catch(e) {
console.log('error parsing code', e.toString())
reject(e)
}
})
}
function Tree(source, escodegenOptions, acornOptions) {
this.acornOptionDefaults = _.extend({}, acornOptionDefaults, acornOptions);
this.comments = [];
this.tokens = [];
this.acornOptionDefaults.onComment = this.comments;
this.acornOptionDefaults.onToken = this.tokens;
this.tree = acorn.parse(source.toString(), this.acornOptionDefaults);
this.tree = escodegen.attachComments(this.tree, this.comments, this.tokens);
this.body = new Body(this.tree.body, this.acornOptionDefaults);
this.escodegenOptions = _.extend({}, escodegenOptionDefaults, escodegenOptions);
}
return new Promise((resolve, reject) => {
try {
const ast = acorn.parse(code, {
plugins: {jsx: true},
sourceType: 'module',
})
resolve('No Errors')
} catch (e) {
reject(e)
}
})
}
.indexOf(safe.declarationType) !== -1
allocator.reset()
allocator.VARIABLE_PREFIX = typeof safe.variablePrefix === 'string'
? safe.variablePrefix
: allocator.VARIABLE_PREFIX
generators.DECLARATION_TYPE = isValidDeclarationType
? safe.declarationType
: generators.DECLARATION_TYPE
transformers.INLINE_NATIVEJSX_HELPERS = safe.prototypes === 'inline'
transformers.MODULE_NATIVEJSX_HELPERS = safe.prototypes === 'module'
const state = {transformed: false}
const ast = acorn.parse(jsx, safe.acorn)
walk.simple(ast, transformers, walker, state)
if (state.transformed) {
if (transformers.INLINE_NATIVEJSX_HELPERS) {
ast.body = [
setAttributesAST,
setStylesAST,
appendChildrenAST
].concat(ast.body)
}
return escodegen.generate(ast)
} else {
return jsx
}