Skip to content

Commit

Permalink
extracted unambiguous JS grammar patterns/predicates to utils module …
Browse files Browse the repository at this point in the history
…(for reuse)
  • Loading branch information
benmosher committed Jul 7, 2016
1 parent a55cd67 commit 7b25c1c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
21 changes: 3 additions & 18 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ const hashObject = require('eslint-module-utils/hash').hashObject

const exportCache = new Map()

/**
* detect possible imports/exports without a full parse.
* used primarily to ignore the import/ignore setting, iif it looks like
* there might be something there (i.e., jsnext:main is set).
*
* Not perfect, just a fast way to disqualify large non-ES6 modules and
* avoid a parse.
* @type {RegExp}
*/
const potentiallyUnambiguousModule =
new RegExp(`(?:^|;)\s*(?:export|import)(?:(?:\s+\w)|(?:\s*[{*]))`)
const unambiguous = require('eslint-module-utils/unambiguous')

class ExportMap {
constructor(path) {
Expand Down Expand Up @@ -290,7 +280,7 @@ ExportMap.for = function (path, context) {
const content = fs.readFileSync(path, { encoding: 'utf8' })

// check for and cache ignore
if (isIgnored(path, context) && !potentiallyUnambiguousModule.test(content)) {
if (isIgnored(path, context) && !unambiguous.potentialModulePattern.test(content)) {
exportCache.set(cacheKey, null)
return null
}
Expand All @@ -307,11 +297,6 @@ ExportMap.for = function (path, context) {
}


const unambiguousNodeType = /^(Exp|Imp)ort.*Declaration$/
function isUnambiguousModule(ast) {
return ast.body.some(node => unambiguousNodeType.test(node.type))
}

ExportMap.parse = function (path, content, context) {
var m = new ExportMap(path)

Expand All @@ -322,7 +307,7 @@ ExportMap.parse = function (path, content, context) {
return m // can't continue
}

if (!isUnambiguousModule(ast)) return null
if (!unambiguous.isModule(ast)) return null

const docstyle = (context.settings && context.settings['import/docstyle']) || ['jsdoc']
const docStyleParsers = {}
Expand Down
29 changes: 29 additions & 0 deletions utils/unambiguous.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'
exports.__esModule = true

/**
* detect possible imports/exports without a full parse.
* used primarily to ignore the import/ignore setting, iif it looks like
* there might be something there (i.e., jsnext:main is set).
*
* A negative test means that a file is definitely _not_ a module.
* A positive test means it _could_ be.
*
* Not perfect, just a fast way to disqualify large non-ES6 modules and
* avoid a parse.
* @type {RegExp}
*/
exports.potentialModulePattern =
new RegExp(`(?:^|;)\s*(?:export|import)(?:(?:\s+\w)|(?:\s*[{*]))`)

// future-/Babel-proof at the expense of being a little loose
const unambiguousNodeType = /^(Exp|Imp)ort.*Declaration$/

/**
* Given an AST, return true if the AST unambiguously represents a module.
* @param {Program node} ast
* @return {Boolean}
*/
exports.isModule = function isUnambiguousModule(ast) {
return ast.body.some(node => unambiguousNodeType.test(node.type))
}

0 comments on commit 7b25c1c

Please sign in to comment.