Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
transform(code, id) {
if (!/new\s+Worker/.test(code)) return;
const ast = this.parse(code);
// The walker calls a method for each node type on a “base” before calling
// the method on our visitor object. The default base doesn’t handle
// dynamic import. Here we create a copy of the original base
// using `make()` and put add an empty handler for dynamic imports
// ourselves. Seems to work :shrug:
const newBase = walker.make({
Import(node) {}
});
const warn = this.warn.bind(this);
// Collect all the worker calls in this array.
const newWorkerCalls = [];
walker.simple(
ast,
{
NewExpression(node) {
if (node.callee.name !== "Worker") {
return;
}
const workerFile = node.arguments[0].value;
if (!/^\.*\//.test(workerFile)) {
warn(
transform(code, id) {
const ast = this.parse(code);
// The walker calls a method for each node type on a “base” before calling
// the method on our visitor object. The default base doesn’t handle
// dynamic import. Here we create a copy of the original base
// using `make()` and put add an empty handler for dynamic imports
// ourselves. Seems to work :shrug:
const newBase = walker.make({
Import(node) {}
});
// Collect all import calls that are inside a Worker constructor call
// inside this array.
const importCalls = [];
walker.simple(
ast,
{
NewExpression(node) {
if (node.callee.name !== "Worker") {
return;
}
if (node.arguments[0].callee.type !== "Import") {
return;
}
- assigning to a variable you didn't declare
- access to non-whitelisted globals
(use a '// declare global: foo, bar' comment to declare extra
globals in a file)
[1]: https://github.com/marijnh/acorn/
*/
var fs = require("fs"), acorn = require("acorn"), walk = require("acorn-walk");
var getOptions = require("./options").getOptions;
var buildScopes = require("./scope").buildScopes;
var globals = require("./globals");
var loop = require("./loop");
var scopePasser = walk.make({
Statement: function(node, prev, c) { c(node, node.scope || prev); },
Function: function(node, _prev, c) { walk.base.Function(node, node.scope, c) }
});
function checkFile(fileName, options, text) {
options = getOptions(options);
if (text == null) text = fs.readFileSync(fileName, "utf8");
var bad, msg;
if (!options.trailingSpace)
bad = text.match(/[\t ]\n/);
if (!bad && !options.tabs)
bad = text.match(/\t/);
if (!bad)
bad = text.match(/[\x00-\x08\x0b\x0c\x0e-\x19\uFEFF]/);
if (bad) {
function make (funcs, baseVisitor) {
return walk.make(funcs, baseVisitor || base)
}
function make (funcs, baseVisitor) {
return walk.make(funcs, baseVisitor || base)
}
return acornWalk.findNodeAfter(node, pos, test, baseVisitor, state);
}
/**
* Find the outermost matching node before a given position.
*/
export function findNodeBefore(node: Ast.Node,
pos: number,
test: string | AstTestFn | null,
baseVisitor = base,
state?: T):
{ node: Node, state: T } {
return acornWalk.findNodeAfter(node, pos, test, baseVisitor, state);
}
export const base: AstVisitors = acornWalk.make({
ENDIdentifier: ignore,
ENDPropertyIdentifier: ignore,
ENDStateIdentifier: ignore,
ENDStoreIdentifier: ignore,
ENDVariableIdentifier: ignore,
ENDProgram(node: Ast.ENDProgram, state, c) {
walkArray(node.body, state, c);
walkArray(node.stylesheets, state, c);
walkArray(node.scripts, state, c);
},
ENDTemplate(node: Ast.ENDTemplate, state, c) {
walkArray(node.body, state, c);
},
ENDPartial(node: Ast.ENDPartial, state, c) {
walkArray(node.params, state, c);
walkArray(node.body, state, c);
function make (funcs, baseVisitor) {
return walk.make(funcs, baseVisitor || base)
}