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

[New] first/TypeScript: Add support for import = expressions #1917

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

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

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

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

[#1917]: https://github.com/benmosher/eslint-plugin-import/pull/1917
[#1889]: https://github.com/benmosher/eslint-plugin-import/pull/1889
[#1878]: https://github.com/benmosher/eslint-plugin-import/pull/1878
[#1854]: https://github.com/benmosher/eslint-plugin-import/issues/1854
Expand Down Expand Up @@ -1281,3 +1284,4 @@ for info on changes for earlier releases.
[@tomprats]: https://github.com/tomprats
[@straub]: https://github.com/straub
[@andreubotella]: https://github.com/andreubotella
[@ExE-Boss]: https://github.com/ExE-Boss
38 changes: 38 additions & 0 deletions src/rules/first.js
Expand Up @@ -75,6 +75,44 @@ module.exports = {
} else {
lastLegalImp = node
}
} else if (node.type === 'TSImportEqualsDeclaration') {
const moduleReference = node.moduleReference
if (moduleReference.type !== 'TSExternalModuleReference') {
nonImportCount++
} else {
if (absoluteFirst) {
const source = moduleReference.expression
if (/^\./.test(source.value)) {
anyRelative = true
} else if (anyRelative) {
context.report({
node: source,
message: 'Absolute imports should come before relative imports.',
})
}
}
if (nonImportCount > 0) {
for (let variable of context.getDeclaredVariables(node)) {
if (!shouldSort) break
const references = variable.references
if (references.length) {
for (let reference of references) {
if (reference.identifier.range[0] < node.range[1]) {
shouldSort = false
break
}
}
}
}
shouldSort && (lastSortNodesIndex = errorInfos.length)
errorInfos.push({
node,
range: [body[index - 1].range[1], node.range[1]],
})
} else {
lastLegalImp = node
}
}
} else {
nonImportCount++
}
Expand Down
66 changes: 62 additions & 4 deletions tests/src/rules/first.js
@@ -1,8 +1,9 @@
import { test } from '../utils'
import { test, getTSParsers } from '../utils'

import { RuleTester } from 'eslint'
import flatMap from 'array.prototype.flatmap'

const ruleTester = new RuleTester()
const ruleTester = new RuleTester({ parserOptions: { sourceType: 'module' } })
, rule = require('rules/first')

ruleTester.run('first', rule, {
Expand All @@ -16,7 +17,42 @@ ruleTester.run('first', rule, {
})
, test({ code: "'use directive';\
import { x } from 'foo';" })
,
, ...flatMap(getTSParsers(), parser => [
test({
parser,
code: `
import fs = require('fs');
import { x } from './foo';
import { y } from './bar';
`,
}),
test({
parser,
code: `
import fs = require('fs');
import { x } from './foo';
import { y } from './bar';
`,
options: ['absolute-first'],
}),
test({
parser,
code: `
import { x } from './foo';
import fs = require('fs');
import { y } from './bar';
`,
}),
test({
parser,
code: `
import { x } from './foo';
import fs = require('fs');
import { y } from './bar';
`,
options: ['disable-absolute-first'],
}),
]),
],
invalid: [
test({ code: "import { x } from './foo';\
Expand Down Expand Up @@ -65,6 +101,28 @@ ruleTester.run('first', rule, {
, errors: 1
, output: "import a from 'b'\nif (true) { console.log(1) }",
})
,
, ...flatMap(getTSParsers(), parser => [
{
parser,
code: `
import { x } from './foo';
import fs = require('fs');
`,
options: ['absolute-first'],
errors: 1,
},
{
parser,
code: `
var a = 1;
import fs = require('fs');
`,
errors: 1,
output: `
import fs = require('fs');
var a = 1;
`,
},
]),
],
})