Skip to content

Commit f897599

Browse files
committed
[New] first/TypeScript: Add support for import = expressions
1 parent a00727e commit f897599

File tree

3 files changed

+104
-4
lines changed

3 files changed

+104
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).
66

77
## [Unreleased]
8+
### Added
9+
- [`first`]/TypeScript: Add support for `import =` expressions ([#1917], thanks [@ExE-Boss])
810

911
### Fixed
1012
- [`export`]/TypeScript: properly detect export specifiers as children of a TS module block ([#1889], thanks [@andreubotella])
@@ -735,6 +737,7 @@ for info on changes for earlier releases.
735737

736738
[`memo-parser`]: ./memo-parser/README.md
737739

740+
[#1917]: https://github.com/benmosher/eslint-plugin-import/pull/1917
738741
[#1889]: https://github.com/benmosher/eslint-plugin-import/pull/1889
739742
[#1878]: https://github.com/benmosher/eslint-plugin-import/pull/1878
740743
[#1854]: https://github.com/benmosher/eslint-plugin-import/issues/1854
@@ -1281,3 +1284,4 @@ for info on changes for earlier releases.
12811284
[@tomprats]: https://github.com/tomprats
12821285
[@straub]: https://github.com/straub
12831286
[@andreubotella]: https://github.com/andreubotella
1287+
[@ExE-Boss]: https://github.com/ExE-Boss

src/rules/first.js

+38
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,44 @@ module.exports = {
7575
} else {
7676
lastLegalImp = node
7777
}
78+
} else if (node.type === 'TSImportEqualsDeclaration') {
79+
const moduleReference = node.moduleReference
80+
if (moduleReference.type !== 'TSExternalModuleReference') {
81+
nonImportCount++
82+
} else {
83+
if (absoluteFirst) {
84+
const source = moduleReference.expression
85+
if (/^\./.test(source.value)) {
86+
anyRelative = true
87+
} else if (anyRelative) {
88+
context.report({
89+
node: source,
90+
message: 'Absolute imports should come before relative imports.',
91+
})
92+
}
93+
}
94+
if (nonImportCount > 0) {
95+
for (let variable of context.getDeclaredVariables(node)) {
96+
if (!shouldSort) break
97+
const references = variable.references
98+
if (references.length) {
99+
for (let reference of references) {
100+
if (reference.identifier.range[0] < node.range[1]) {
101+
shouldSort = false
102+
break
103+
}
104+
}
105+
}
106+
}
107+
shouldSort && (lastSortNodesIndex = errorInfos.length)
108+
errorInfos.push({
109+
node,
110+
range: [body[index - 1].range[1], node.range[1]],
111+
})
112+
} else {
113+
lastLegalImp = node
114+
}
115+
}
78116
} else {
79117
nonImportCount++
80118
}

tests/src/rules/first.js

+62-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { test } from '../utils'
1+
import { test, getTSParsers } from '../utils'
22

33
import { RuleTester } from 'eslint'
4+
import flatMap from 'array.prototype.flatmap'
45

5-
const ruleTester = new RuleTester()
6+
const ruleTester = new RuleTester({ parserOptions: { sourceType: 'module' } })
67
, rule = require('rules/first')
78

89
ruleTester.run('first', rule, {
@@ -16,7 +17,42 @@ ruleTester.run('first', rule, {
1617
})
1718
, test({ code: "'use directive';\
1819
import { x } from 'foo';" })
19-
,
20+
, ...flatMap(getTSParsers(), parser => [
21+
test({
22+
parser,
23+
code: `
24+
import fs = require('fs');
25+
import { x } from './foo';
26+
import { y } from './bar';
27+
`,
28+
}),
29+
test({
30+
parser,
31+
code: `
32+
import fs = require('fs');
33+
import { x } from './foo';
34+
import { y } from './bar';
35+
`,
36+
options: ['absolute-first'],
37+
}),
38+
test({
39+
parser,
40+
code: `
41+
import { x } from './foo';
42+
import fs = require('fs');
43+
import { y } from './bar';
44+
`,
45+
}),
46+
test({
47+
parser,
48+
code: `
49+
import { x } from './foo';
50+
import fs = require('fs');
51+
import { y } from './bar';
52+
`,
53+
options: ['disable-absolute-first'],
54+
}),
55+
]),
2056
],
2157
invalid: [
2258
test({ code: "import { x } from './foo';\
@@ -65,6 +101,28 @@ ruleTester.run('first', rule, {
65101
, errors: 1
66102
, output: "import a from 'b'\nif (true) { console.log(1) }",
67103
})
68-
,
104+
, ...flatMap(getTSParsers(), parser => [
105+
{
106+
parser,
107+
code: `
108+
import { x } from './foo';
109+
import fs = require('fs');
110+
`,
111+
options: ['absolute-first'],
112+
errors: 1,
113+
},
114+
{
115+
parser,
116+
code: `
117+
var a = 1;
118+
import fs = require('fs');
119+
`,
120+
errors: 1,
121+
output: `
122+
import fs = require('fs');
123+
var a = 1;
124+
`,
125+
},
126+
]),
69127
],
70128
})

0 commit comments

Comments
 (0)