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] first's handling of import = require #1963

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`prefer-default-export`]: handle empty array destructuring ([#1965], thanks [@ljharb])
- [`no-unused-modules`]: make type imports mark a module as used (fixes #1924) ([#1974], thanks [@cherryblossom000])
- [`no-cycle`]: fix perf regression ([#1944], thanks [@Blasz])
- [`first`]: fix handling of `import = require` ([#1963], thanks [@MatthiasKunnen])

### Changed
- [Generic Import Callback] Make callback for all imports once in rules ([#1237], thanks [@ljqx])
Expand Down Expand Up @@ -1307,4 +1308,5 @@ for info on changes for earlier releases.
[@fa93hws]: https://github.com/fa93hws
[@Librazy]: https://github.com/Librazy
[@swernerx]: https://github.com/swernerx
[@fsmaia]: https://github.com/fsmaia
[@fsmaia]: https://github.com/fsmaia
[@MatthiasKunnen]: https://github.com/MatthiasKunnen
12 changes: 9 additions & 3 deletions src/rules/first.js
@@ -1,5 +1,11 @@
import docsUrl from '../docsUrl';

function getImportValue(node) {
return node.type === 'ImportDeclaration'
? node.source.value
: node.moduleReference.expression.value;
}

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -43,13 +49,13 @@ module.exports = {

anyExpressions = true;

if (node.type === 'ImportDeclaration') {
if (node.type === 'ImportDeclaration' || node.type === 'TSImportEqualsDeclaration') {
if (absoluteFirst) {
if (/^\./.test(node.source.value)) {
if (/^\./.test(getImportValue(node))) {
anyRelative = true;
} else if (anyRelative) {
context.report({
node: node.source,
node: node.type === 'ImportDeclaration' ? node.source : node.moduleReference,
message: 'Absolute imports should come before relative imports.',
});
}
Expand Down
50 changes: 49 additions & 1 deletion tests/src/rules/first.js
@@ -1,4 +1,4 @@
import { test } from '../utils';
import { test, getTSParsers } from '../utils';

import { RuleTester } from 'eslint';

Expand Down Expand Up @@ -66,3 +66,51 @@ ruleTester.run('first', rule, {
}),
],
});

context('TypeScript', function () {
getTSParsers()
.filter((parser) => parser !== require.resolve('typescript-eslint-parser'))
.forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
};

ruleTester.run('order', rule, {
valid: [
test(
{
code: `
import y = require('bar');
import { x } from 'foo';
import z = require('baz');
`,
parser,
},
parserConfig,
),
],
invalid: [
test(
{
code: `
import { x } from './foo';
import y = require('bar');
`,
options: ['absolute-first'],
parser,
errors: [
{
message: 'Absolute imports should come before relative imports.',
},
],
},
parserConfig,
),
],
});
});
});