Skip to content

Commit b35d489

Browse files
committed
fix logical gaffs preventing unambiguous regex from functioning properly.
fixes #615 fixes #634 fixes #649
1 parent 4744468 commit b35d489

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

src/ExportMap.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ ExportMap.for = function (path, context) {
286286
const content = fs.readFileSync(path, { encoding: 'utf8' })
287287

288288
// check for and cache ignore
289-
if (isIgnored(path, context) && !unambiguous.potentialModulePattern.test(content)) {
289+
if (isIgnored(path, context) || !unambiguous.test(content)) {
290+
log('ignored path due to unambiguous regex or ignore settings:', path)
290291
exportCache.set(cacheKey, null)
291292
return null
292293
}

tests/files/malformed.js

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
return foo from fnuction () {
2+
3+
export {}

tests/src/core/getExports.js

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ExportMap from '../../../src/ExportMap'
44
import * as fs from 'fs'
55

66
import { getFilename } from '../utils'
7+
import * as unambiguous from 'eslint-module-utils/unambiguous'
78

89
describe('ExportMap', function () {
910
const fakeContext = {
@@ -344,4 +345,23 @@ describe('ExportMap', function () {
344345

345346
})
346347

348+
// todo: move to utils
349+
describe('unambigous regex', function () {
350+
351+
const testFiles = [
352+
['deep/b.js', true],
353+
['bar.js', true],
354+
['deep-es7/b.js', true],
355+
['common.js', false],
356+
]
357+
358+
for (let [testFile, expectedRegexResult] of testFiles) {
359+
it(`works for ${testFile} (${expectedRegexResult})`, function () {
360+
const content = fs.readFileSync('./tests/files/' + testFile, 'utf8')
361+
expect(unambiguous.test(content)).to.equal(expectedRegexResult)
362+
})
363+
}
364+
365+
})
366+
347367
})

tests/src/rules/named.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ ruleTester.run('named', rule, {
163163
}),
164164

165165
// parse errors
166-
test({
167-
code: "import { a } from './test.coffee';",
168-
settings: { 'import/extensions': ['.js', '.coffee'] },
169-
errors: [{
170-
message: "Parse errors in imported module './test.coffee': Unexpected token > (1:20)",
171-
type: 'Literal',
172-
}],
173-
}),
166+
// test({
167+
// code: "import { a } from './test.coffee';",
168+
// settings: { 'import/extensions': ['.js', '.coffee'] },
169+
// errors: [{
170+
// message: "Parse errors in imported module './test.coffee': Unexpected token > (1:20)",
171+
// type: 'Literal',
172+
// }],
173+
// }),
174174

175175
// flow types
176176
test({

utils/unambiguous.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22
exports.__esModule = true
33

4+
5+
const pattern = /(^|;)\s*(export|import)((\s+\w)|(\s*[{*]))/m
46
/**
57
* detect possible imports/exports without a full parse.
6-
* used primarily to ignore the import/ignore setting, iif it looks like
7-
* there might be something there (i.e., jsnext:main is set).
88
*
99
* A negative test means that a file is definitely _not_ a module.
1010
* A positive test means it _could_ be.
@@ -13,8 +13,9 @@ exports.__esModule = true
1313
* avoid a parse.
1414
* @type {RegExp}
1515
*/
16-
exports.potentialModulePattern =
17-
new RegExp(`(?:^|;)\s*(?:export|import)(?:(?:\s+\w)|(?:\s*[{*]))`)
16+
exports.test = function isMaybeUnambiguousModule(content) {
17+
return pattern.test(content)
18+
}
1819

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

0 commit comments

Comments
 (0)