How to use eslint-module-utils - 10 common examples

To help you get started, we’ve selected a few eslint-module-utils 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 benmosher / eslint-plugin-import / src / ExportMap.js View on Github external
let exportMap = exportCache.get(cacheKey)

  // return cached ignore
  if (exportMap === null) return null

  const stats = fs.statSync(path)
  if (exportMap != null) {
    // date equality check
    if (exportMap.mtime - stats.mtime === 0) {
      return exportMap
    }
    // future: check content equality?
  }

  // check valid extensions first
  if (!hasValidExtension(path, context)) {
    exportCache.set(cacheKey, null)
    return null
  }

  // check for and cache ignore
  if (isIgnored(path, context)) {
    log('ignored path due to ignore settings:', path)
    exportCache.set(cacheKey, null)
    return null
  }

  const content = fs.readFileSync(path, { encoding: 'utf8' })

  // check for and cache unambigious modules
  if (!unambiguous.test(content)) {
    log('ignored path due to unambiguous regex:', path)
github benmosher / eslint-plugin-import / src / ExportMap.js View on Github external
if (exportMap != null) {
    // date equality check
    if (exportMap.mtime - stats.mtime === 0) {
      return exportMap
    }
    // future: check content equality?
  }

  // check valid extensions first
  if (!hasValidExtension(path, context)) {
    exportCache.set(cacheKey, null)
    return null
  }

  // check for and cache ignore
  if (isIgnored(path, context)) {
    log('ignored path due to ignore settings:', path)
    exportCache.set(cacheKey, null)
    return null
  }

  const content = fs.readFileSync(path, { encoding: 'utf8' })

  // check for and cache unambigious modules
  if (!unambiguous.test(content)) {
    log('ignored path due to unambiguous regex:', path)
    exportCache.set(cacheKey, null)
    return null
  }

  log('cache miss', cacheKey, 'for path', path)
  exportMap = ExportMap.parse(path, content, context)
github benmosher / eslint-plugin-import / src / ExportMap.js View on Github external
if (!hasValidExtension(path, context)) {
    exportCache.set(cacheKey, null)
    return null
  }

  // check for and cache ignore
  if (isIgnored(path, context)) {
    log('ignored path due to ignore settings:', path)
    exportCache.set(cacheKey, null)
    return null
  }

  const content = fs.readFileSync(path, { encoding: 'utf8' })

  // check for and cache unambigious modules
  if (!unambiguous.test(content)) {
    log('ignored path due to unambiguous regex:', path)
    exportCache.set(cacheKey, null)
    return null
  }

  log('cache miss', cacheKey, 'for path', path)
  exportMap = ExportMap.parse(path, content, context)

  // ambiguous modules return null
  if (exportMap == null) return null

  exportMap.mtime = stats.mtime

  exportCache.set(cacheKey, exportMap)
  return exportMap
}
github benmosher / eslint-plugin-import / src / rules / no-unused-modules.js View on Github external
node.body.forEach(astNode => {
        let resolvedPath

        // support for export { value } from 'module'
        if (astNode.type === EXPORT_NAMED_DECLARATION) {
          if (astNode.source) {
            resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context)
            astNode.specifiers.forEach(specifier => {
              let name
              if (specifier.exported.name === DEFAULT) {
                name = IMPORT_DEFAULT_SPECIFIER
              } else {
                name = specifier.local.name
              }
              newImports.set(name, resolvedPath)
            })
          }
        }

        if (astNode.type === EXPORT_ALL_DECLARATION) {
          resolvedPath = resolve(astNode.source.raw.replace(/('|")/g, ''), context)
          newExportAll.add(resolvedPath)
        }
github benmosher / eslint-plugin-import / src / rules / no-internal-modules.js View on Github external
} else {
            return acc.concat(step)
          }
        }, [])

      const nonScopeSteps = steps.filter(step => step.indexOf('@') !== 0)
      if (nonScopeSteps.length <= 1) return false

      // before trying to resolve, see if the raw import (with relative
      // segments resolved) matches an allowed pattern
      const justSteps = steps.join('/')
      if (reachingAllowed(justSteps) || reachingAllowed(`/${justSteps}`)) return false

      // if the import statement doesn't match directly, try to match the
      // resolved path if the import is resolvable
      const resolved = resolve(importPath, context)
      if (!resolved || reachingAllowed(normalizeSep(resolved))) return false

      // this import was not allowed by the allowed paths, and reaches
      // so it is a violation
      return true
    }
github eschirtz / Computer-Science-Series / node_modules / eslint-plugin-import / lib / rules / no-cycle.js View on Github external
var _moduleVisitor = require('eslint-module-utils/moduleVisitor');

var _moduleVisitor2 = _interopRequireDefault(_moduleVisitor);

var _docsUrl = require('../docsUrl');

var _docsUrl2 = _interopRequireDefault(_docsUrl);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// todo: cache cycles / deep relationships for faster repeat evaluation
module.exports = {
  meta: {
    type: 'suggestion',
    docs: { url: (0, _docsUrl2.default)('no-cycle') },
    schema: [(0, _moduleVisitor.makeOptionsSchema)({
      maxDepth: {
        description: 'maximum dependency depth to traverse',
        type: 'integer',
        minimum: 1
      }
    })]
  },

  create: function (context) {
    const myPath = context.getFilename();
    if (myPath === '
github eschirtz / Computer-Science-Series / node_modules / eslint-plugin-import / lib / rules / no-relative-parent-imports.js View on Github external
var _resolve2 = _interopRequireDefault(_resolve);

var _importType = require('../core/importType');

var _importType2 = _interopRequireDefault(_importType);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

module.exports = {
  meta: {
    type: 'suggestion',
    docs: {
      url: (0, _docsUrl2.default)('no-relative-parent-imports')
    },
    schema: [(0, _moduleVisitor.makeOptionsSchema)()]
  },

  create: function noRelativePackages(context) {
    const myPath = context.getFilename();
    if (myPath === '
github benmosher / eslint-plugin-import / src / rules / no-cycle.js View on Github external
}
      }

      while (untraversed.length > 0) {
        const next = untraversed.shift() // bfs!
        if (detectCycle(next)) {
          const message = (next.route.length > 0
            ? `Dependency cycle via ${routeString(next.route)}`
            : 'Dependency cycle detected.')
          context.report(importer, message)
          return
        }
      }
    }

    return moduleVisitor(checkSourceValue, context.options[0])
  },
}
github benmosher / eslint-plugin-import / src / rules / no-deprecated.js View on Github external
'Identifier': function (node) {
        if (node.parent.type === 'MemberExpression' && node.parent.property === node) {
          return // handled by MemberExpression
        }

        // ignore specifier identifiers
        if (node.parent.type.slice(0, 6) === 'Import') return

        if (!deprecated.has(node.name)) return

        if (declaredScope(context, node.name) !== 'module') return
        context.report({
          node,
          message: message(deprecated.get(node.name)),
        })
      },
github benmosher / eslint-plugin-import / src / rules / namespace.js View on Github external
VariableDeclarator: function ({ id, init }) {
        if (init == null) return
        if (init.type !== 'Identifier') return
        if (!namespaces.has(init.name)) return

        // check for redefinition in intermediate scopes
        if (declaredScope(context, init.name) !== 'module') return

        // DFS traverse child namespaces
        function testKey(pattern, namespace, path = [init.name]) {
          if (!(namespace instanceof Exports)) return

          if (pattern.type !== 'ObjectPattern') return

          for (const property of pattern.properties) {
            if (
              property.type === 'ExperimentalRestProperty'
              || property.type === 'RestElement'
              || !property.key
            ) {
              continue
            }