Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unambiguous regex #654

Merged
merged 2 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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