How to use scope-analyzer - 10 common examples

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 / split-require / plugin.js View on Github external
dash(ast, function (node) {
    var binding
    if (isRequire(node, 'split-require')) {
      if (onrequire) onrequire(node)
      if (node.parent.type === 'VariableDeclarator') {
        // var sr = require('split-require')
        binding = scan.getBinding(node.parent.id)
        if (binding) binding.getReferences().slice(1).forEach(onreference)
      } else if (node.parent.type === 'AssignmentExpression') {
        // sr = require('split-require')
        binding = scan.getBinding(node.parent.left)
        if (binding) binding.getReferences().slice(1).forEach(onreference)
      } else {
        // require('split-require')(...args)
        onreference(node)
      }
    }
  })
}
github goto-bus-stop / split-require / plugin.js View on Github external
dash(ast, function (node) {
    var binding
    if (isRequire(node, 'split-require')) {
      if (onrequire) onrequire(node)
      if (node.parent.type === 'VariableDeclarator') {
        // var sr = require('split-require')
        binding = scan.getBinding(node.parent.id)
        if (binding) binding.getReferences().slice(1).forEach(onreference)
      } else if (node.parent.type === 'AssignmentExpression') {
        // sr = require('split-require')
        binding = scan.getBinding(node.parent.left)
        if (binding) binding.getReferences().slice(1).forEach(onreference)
      } else {
        // require('split-require')(...args)
        onreference(node)
      }
    }
  })
}
github goto-bus-stop / split-require / plugin.js View on Github external
function detectSplitRequireCalls (ast, onreference, onrequire) {
  scan.crawl(ast)
  dash(ast, function (node) {
    var binding
    if (isRequire(node, 'split-require')) {
      if (onrequire) onrequire(node)
      if (node.parent.type === 'VariableDeclarator') {
        // var sr = require('split-require')
        binding = scan.getBinding(node.parent.id)
        if (binding) binding.getReferences().slice(1).forEach(onreference)
      } else if (node.parent.type === 'AssignmentExpression') {
        // sr = require('split-require')
        binding = scan.getBinding(node.parent.left)
        if (binding) binding.getReferences().slice(1).forEach(onreference)
      } else {
        // require('split-require')(...args)
        onreference(node)
      }
github goto-bus-stop / webpack-unpack / index.js View on Github external
if (!meta) meta = unpackJsonpPrelude(ast)
  if (!meta) return

  var entryId = meta.entryId
  var factories = meta.factories

  if (!factories.every(isFunctionOrEmpty)) {
    return
  }

  var modules = []
  for (var i = 0; i < factories.length; i++) {
    var factory = factories[i]
    if (factory.factory === null) continue

    scan.crawl(factory.factory)
    // If source is available, rewrite the require,exports,module var names in place
    // Else, generate a string afterwards.
    var range = getModuleRange(factory.factory.body)
    var moduleSource = rewriteMagicIdentifiers(
      factory.factory,
      source ? source.slice(range.start, range.end) : null,
      range.start
    )
    if (!moduleSource) {
      moduleSource = astring.generate({
        type: 'Program',
        body: factory.factory.body.body
      })
    }

    var deps = getDependencies(factory.factory)
github goto-bus-stop / browser-pack-flat / index.js View on Github external
})[0]
    row.source = dedup.source
  }

  var requireCalls = []
  var orderOfExecution = new Map()

  var ast
  // hack to keep track of `module`/`exports` references correctly.
  // in node.js they're defined by a function wrapper, so their scope is
  // one level higher-ish than the module scope. this emulates that.
  var globalScope = {
    type: 'BrowserPackFlatWrapper',
    parent: null
  }
  scan.createScope(globalScope, ['require', 'module', 'exports'])

  var source = row.source

  // Determine if a require() call may not be evaluated according to its linear source position
  // This happens primarily if the call is inside a function, or inside a conditional branch
  function isOrderUnpredictable (node) {
    while ((node = node.parent)) {
      // Special-case for IIFE, behaviour is the same as evaluating inline
      if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
        // (() => {})()
        if (node.parent && node.parent.type === 'CallExpression' && node.parent.callee === node) {
          continue
        }
        // (function(){}).call(null)
        // this form is added by browserify to inject globals
        if (node.parent && node.parent.type === 'MemberExpression' &&
github goto-bus-stop / browser-pack-flat / index.js View on Github external
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)
      }
    }
  }

  row[kAst] = ast
  row[kIsSimpleExport] = isSimpleExport
  row[kExportsName] = moduleExportsName
  row.hasExports = (moduleExportsList.length + exportsList.length) > 0
  row[kRequireCalls] = requireCalls
  row[kDependencyOrder] = orderOfExecution
  row[kReferences] = {
    require: requireList,

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