Skip to content

Commit

Permalink
[extensions] Ignore query strings when checking for extensions.
Browse files Browse the repository at this point in the history
Fixes #1567.
  • Loading branch information
pcorpet committed Dec 15, 2019
1 parent f12ae59 commit e3fa2ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/rules/extensions.js
Expand Up @@ -132,10 +132,12 @@ module.exports = {
// bail if the declaration doesn't have a source, e.g. "export { foo };"
if (!source) return

const importPath = source.value
const importPathWithQueryString = source.value

// don't enforce anything on builtins
if (isBuiltIn(importPath, context.settings)) return
if (isBuiltIn(importPathWithQueryString, context.settings)) return

const importPath = importPathWithQueryString.replace(/\?(.*)$/, '')

const resolvedPath = resolve(importPath, context)

Expand All @@ -154,14 +156,14 @@ module.exports = {
context.report({
node: source,
message:
`Missing file extension ${extension ? `"${extension}" ` : ''}for "${importPath}"`,
`Missing file extension ${extension ? `"${extension}" ` : ''}for "${source.value}"`,
})
}
} else if (extension) {
if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) {
context.report({
node: source,
message: `Unexpected use of file extension "${extension}" for "${importPath}"`,
message: `Unexpected use of file extension "${extension}" for "${source.value}"`,
})
}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/src/rules/extensions.js
Expand Up @@ -116,6 +116,16 @@ ruleTester.run('extensions', rule, {
].join('\n'),
options: [ 'never' ],
}),

// Query strings.
test({
code: 'import bare from "./foo?a=True.ext"',
options: [ 'never' ],
}),
test({
code: 'import bare from "./foo.js?a=True"',
options: [ 'always' ],
}),
],

invalid: [
Expand Down Expand Up @@ -381,5 +391,29 @@ ruleTester.run('extensions', rule, {
},
],
}),

// Query strings.
test({
code: 'import withExtension from "./foo.js?a=True"',
options: [ 'never' ],
errors: [
{
message: 'Unexpected use of file extension "js" for "./foo.js?a=True"',
line: 1,
column: 27,
},
],
}),
test({
code: 'import withoutExtension from "./foo?a=True.ext"',
options: [ 'always' ],
errors: [
{
message: 'Missing file extension for "./foo?a=True.ext"',
line: 1,
column: 30,
},
],
}),
],
})

0 comments on commit e3fa2ce

Please sign in to comment.