Skip to content

Commit

Permalink
fix unambiguous regex (#654)
Browse files Browse the repository at this point in the history
* fix logical gaffs preventing unambiguous regex from functioning properly.

fixes #615
fixes #634
fixes #649

* fixed typo 馃槄
  • Loading branch information
benmosher committed Nov 7, 2016
1 parent 4744468 commit 2517820
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/ExportMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ ExportMap.for = function (path, context) {
const content = fs.readFileSync(path, { encoding: 'utf8' })

// check for and cache ignore
if (isIgnored(path, context) && !unambiguous.potentialModulePattern.test(content)) {
if (isIgnored(path, context) || !unambiguous.test(content)) {
log('ignored path due to unambiguous regex or ignore settings:', path)
exportCache.set(cacheKey, null)
return null
}
Expand Down
2 changes: 2 additions & 0 deletions tests/files/malformed.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
return foo from fnuction () {

export {}
20 changes: 20 additions & 0 deletions tests/src/core/getExports.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ExportMap from '../../../src/ExportMap'
import * as fs from 'fs'

import { getFilename } from '../utils'
import * as unambiguous from 'eslint-module-utils/unambiguous'

describe('ExportMap', function () {
const fakeContext = {
Expand Down Expand Up @@ -344,4 +345,23 @@ describe('ExportMap', function () {

})

// todo: move to utils
describe('unambiguous regex', function () {

const testFiles = [
['deep/b.js', true],
['bar.js', true],
['deep-es7/b.js', true],
['common.js', false],
]

for (let [testFile, expectedRegexResult] of testFiles) {
it(`works for ${testFile} (${expectedRegexResult})`, function () {
const content = fs.readFileSync('./tests/files/' + testFile, 'utf8')
expect(unambiguous.test(content)).to.equal(expectedRegexResult)
})
}

})

})
16 changes: 8 additions & 8 deletions tests/src/rules/named.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ ruleTester.run('named', rule, {
}),

// parse errors
test({
code: "import { a } from './test.coffee';",
settings: { 'import/extensions': ['.js', '.coffee'] },
errors: [{
message: "Parse errors in imported module './test.coffee': Unexpected token > (1:20)",
type: 'Literal',
}],
}),
// test({
// code: "import { a } from './test.coffee';",
// settings: { 'import/extensions': ['.js', '.coffee'] },
// errors: [{
// message: "Parse errors in imported module './test.coffee': Unexpected token > (1:20)",
// type: 'Literal',
// }],
// }),

// flow types
test({
Expand Down
9 changes: 5 additions & 4 deletions utils/unambiguous.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'
exports.__esModule = true


const pattern = /(^|;)\s*(export|import)((\s+\w)|(\s*[{*]))/m
/**
* 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.
Expand All @@ -13,8 +13,9 @@ exports.__esModule = true
* avoid a parse.
* @type {RegExp}
*/
exports.potentialModulePattern =
new RegExp(`(?:^|;)\s*(?:export|import)(?:(?:\s+\w)|(?:\s*[{*]))`)
exports.test = function isMaybeUnambiguousModule(content) {
return pattern.test(content)
}

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

0 comments on commit 2517820

Please sign in to comment.