diff --git a/src/ExportMap.js b/src/ExportMap.js index 29c894e3d..a75ef44bc 100644 --- a/src/ExportMap.js +++ b/src/ExportMap.js @@ -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 } diff --git a/tests/files/malformed.js b/tests/files/malformed.js index 2778c4347..b6ebf04b7 100644 --- a/tests/files/malformed.js +++ b/tests/files/malformed.js @@ -1 +1,3 @@ return foo from fnuction () { + +export {} diff --git a/tests/src/core/getExports.js b/tests/src/core/getExports.js index 965cba1fc..62513d442 100644 --- a/tests/src/core/getExports.js +++ b/tests/src/core/getExports.js @@ -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 = { @@ -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) + }) + } + + }) + }) diff --git a/tests/src/rules/named.js b/tests/src/rules/named.js index 668182d47..f1c40b474 100644 --- a/tests/src/rules/named.js +++ b/tests/src/rules/named.js @@ -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({ diff --git a/utils/unambiguous.js b/utils/unambiguous.js index d4830a253..a8e842cac 100644 --- a/utils/unambiguous.js +++ b/utils/unambiguous.js @@ -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. @@ -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$/