How to use the scope-analyzer.scope function in scope-analyzer

To help you get started, we’ve selected a few scope-analyzer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github goto-bus-stop / browser-pack-flat / index.js View on Github external
return id != null && !!rows.byId[id]
      }
    }
  })
  magicString.walk(function (node) {
    // transform-ast has set this to `undefined`
    ast.parent = globalScope
    scan.visitBinding(node)
  })

  var requireList = scan.scope(globalScope).getReferences('require')
  var moduleExportsList = scan.scope(globalScope).getReferences('module')
    .map(function (node) { return node.parent })
    .filter(isModuleExports)
  var exportsList = scan.scope(globalScope).getReferences('exports')
  var moduleList = scan.scope(globalScope).getReferences('module')
    .filter(function (node) { return !isModuleExports(node.parent) })

  // Detect simple exports that are just `module.exports = xyz`, we can compile them to a single
  // variable assignment.
  var isSimpleExport = false
  if (moduleExportsList.length === 1 && exportsList.length === 0 && moduleList.length === 0) {
    var node = moduleExportsList[0]
    if (node.parent.type === 'AssignmentExpression' && node.parent.left === node &&
        node.parent.parent.type === 'ExpressionStatement') {
      isSimpleExport = scan.nearestScope(node.object, false) === ast

      var name = getNodeName(node.parent.right)
      if (name) {
        moduleExportsName = toIdentifier('_$' + name + '_' + row.id)
      }
    }
github goto-bus-stop / browser-pack-flat / index.js View on Github external
// rewrite `typeof module` to `"object"`
      if (node.parent.type === 'UnaryExpression' && node.parent.operator === 'typeof') {
        node.parent.edit.update('"object"')
      } else if (isModuleParent(node.parent)) {
        if (row.entry) {
          node.parent.edit.update('null')
        } else {
          node.parent.edit.update('({})')
        }
      } else {
        renameIdentifier(node, moduleBaseName)
      }
    })
    if (scan.scope(ast)) {
      // rename colliding global variable names
      scan.scope(ast).forEach(function (binding, name) {
        if (binding[kShouldRename]) {
          renameBinding(binding, toIdentifier('__' + name + '_' + row.id))
        }
      })
    }
  }

  row[kRequireCalls].forEach(function (req) {
    var node = req.node
    var other = req.requiredModule
    if (req.external) {
      node.edit.update('require(' + JSON.stringify(req.id) + ')')
    } else if (other && other[kEvaluateOnDemand]) {
      node.edit.update(other[kExportsName] + '({})') // `module.parent` value = {}
    } else if (other && other[kExportsName]) {
      renameImport(row, node, other[kExportsName])
github goto-bus-stop / browser-pack-flat / index.js View on Github external
orderOfExecution.set(row.deps[required] || required, node.end)
      }

      function moduleExists (id) {
        return id != null && !!rows.byId[id]
      }
    }
  })
  magicString.walk(function (node) {
    // transform-ast has set this to `undefined`
    ast.parent = globalScope
    scan.visitBinding(node)
  })

  var requireList = scan.scope(globalScope).getReferences('require')
  var moduleExportsList = scan.scope(globalScope).getReferences('module')
    .map(function (node) { return node.parent })
    .filter(isModuleExports)
  var exportsList = scan.scope(globalScope).getReferences('exports')
  var moduleList = scan.scope(globalScope).getReferences('module')
    .filter(function (node) { return !isModuleExports(node.parent) })

  // Detect simple exports that are just `module.exports = xyz`, we can compile them to a single
  // variable assignment.
  var isSimpleExport = false
  if (moduleExportsList.length === 1 && exportsList.length === 0 && moduleList.length === 0) {
    var node = moduleExportsList[0]
    if (node.parent.type === 'AssignmentExpression' && node.parent.left === node &&
        node.parent.parent.type === 'ExpressionStatement') {
      isSimpleExport = scan.nearestScope(node.object, false) === ast

      var name = getNodeName(node.parent.right)
github goto-bus-stop / browser-pack-flat / index.js View on Github external
})
    moduleList.forEach(function (node) {
      // rewrite `typeof module` to `"object"`
      if (node.parent.type === 'UnaryExpression' && node.parent.operator === 'typeof') {
        node.parent.edit.update('"object"')
      } else if (isModuleParent(node.parent)) {
        if (row.entry) {
          node.parent.edit.update('null')
        } else {
          node.parent.edit.update('({})')
        }
      } else {
        renameIdentifier(node, moduleBaseName)
      }
    })
    if (scan.scope(ast)) {
      // rename colliding global variable names
      scan.scope(ast).forEach(function (binding, name) {
        if (binding[kShouldRename]) {
          renameBinding(binding, toIdentifier('__' + name + '_' + row.id))
        }
      })
    }
  }

  row[kRequireCalls].forEach(function (req) {
    var node = req.node
    var other = req.requiredModule
    if (req.external) {
      node.edit.update('require(' + JSON.stringify(req.id) + ')')
    } else if (other && other[kEvaluateOnDemand]) {
      node.edit.update(other[kExportsName] + '({})') // `module.parent` value = {}
github goto-bus-stop / browser-pack-flat / index.js View on Github external
function markDuplicateVariableNames (row, i, rows) {
  var ast = row[kAst]

  var scope = scan.scope(ast)
  if (scope) {
    scope.forEach(function (binding, name) {
      binding[kShouldRename] = rows.usedGlobalVariables.has(name)
      rows.usedGlobalVariables.add(name)
    })
  }
}
github goto-bus-stop / browser-pack-flat / index.js View on Github external
function identifyGlobals (row, i, rows) {
  var ast = row[kAst]
  var globalScope = ast.parent

  var scope = scan.scope(ast)
  if (scope) {
    scan.scope(globalScope).getUndeclaredNames().forEach(function (name) {
      rows.usedGlobalVariables.add(name)
    })
  }
}
github goto-bus-stop / browser-pack-flat / index.js View on Github external
function identifyGlobals (row, i, rows) {
  var ast = row[kAst]
  var globalScope = ast.parent

  var scope = scan.scope(ast)
  if (scope) {
    scan.scope(globalScope).getUndeclaredNames().forEach(function (name) {
      rows.usedGlobalVariables.add(name)
    })
  }
}
github browserify / browser-pack / esm.js View on Github external
return through.obj(function (row, enc, cb) {
    if (!row.esm) {
      cb(null, row);
      return;
    }

    var ast = acorn.parse(row.source, { sourceType: 'module' });
    assignParent(ast);
    scan.analyze(ast);

    var scope = scan.scope(ast);

    var esmDefaultName = '_esmDefault'
    var patches = [];
    ast.body.forEach(function (node) {
      if (node.type === 'ExportDefaultDeclaration') {
        if (node.declaration.id) {
          esmDefaultName = node.declaration.id.name
        }
        patches.push({
          start: node.start,
          end: node.declaration.start,
          string: node.declaration.id ? '' : 'var _esmDefault = '
        });
      }
      if (node.type === 'ExportNamedDeclaration') {
        if (node.declaration) {

scope-analyzer

simple scope analysis for javascript ASTs

Apache-2.0
Latest version published 3 years ago

Package Health Score

50 / 100
Full package analysis