Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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,
),
],
});
});
});