Skip to content

Commit

Permalink
[Fix] first: fix handling of import = require
Browse files Browse the repository at this point in the history
Fixes #1957.
  • Loading branch information
MatthiasKunnen authored and ljharb committed Dec 10, 2020
1 parent 6bb8528 commit 196d655
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
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,
),
],
});
});
});

0 comments on commit 196d655

Please sign in to comment.