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

[Tests] order: Add TS import type tests #1736

Merged
merged 1 commit into from May 31, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -49,6 +49,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Changed
- [`import/external-module-folders` setting] behavior is more strict now: it will only match complete path segments ([#1605], thanks [@skozin])
- [meta] fix "files" field to include/exclude the proper files ([#1635], thanks [@ljharb])
- [Tests] `order`: Add TS import type tests ([#1736], thanks [@kmui2])

## [2.20.0] - 2020-01-10
### Added
Expand Down Expand Up @@ -685,6 +686,7 @@ for info on changes for earlier releases.
[#1785]: https://github.com/benmosher/eslint-plugin-import/pull/1785
[#1770]: https://github.com/benmosher/eslint-plugin-import/pull/1770
[#1763]: https://github.com/benmosher/eslint-plugin-import/pull/1763
[#1736]: https://github.com/benmosher/eslint-plugin-import/pull/1736
[#1726]: https://github.com/benmosher/eslint-plugin-import/pull/1726
[#1724]: https://github.com/benmosher/eslint-plugin-import/pull/1724
[#1722]: https://github.com/benmosher/eslint-plugin-import/issues/1722
Expand Down
144 changes: 142 additions & 2 deletions tests/src/rules/order.js
@@ -1,4 +1,4 @@
import { test, getTSParsers } from '../utils'
import { test, getTSParsers, getNonDefaultParsers } from '../utils'

import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
Expand Down Expand Up @@ -2071,7 +2071,7 @@ ruleTester.run('order', rule, {
const { cello } = require('./cello');
const blah = require('./blah');
import { hello } from './hello';
`,
`,
errors: [{
message: '`./int` import should occur before import of `./cello`',
}, {
Expand All @@ -2081,3 +2081,143 @@ ruleTester.run('order', rule, {
],
].filter((t) => !!t),
})


context('TypeScript', function () {
getNonDefaultParsers()
.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: [
// #1667: typescript type import support

// Option alphabetize: {order: 'asc'}
test(
{
code: `
import c from 'Bar';
import type { C } from 'Bar';
import b from 'bar';
import a from 'foo';
import type { A } from 'foo';

import index from './';
`,
parser,
options: [
{
groups: ['external', 'index'],
alphabetize: { order: 'asc' },
},
],
},
parserConfig,
),
// Option alphabetize: {order: 'desc'}
test(
{
code: `
import a from 'foo';
import type { A } from 'foo';
import b from 'bar';
import c from 'Bar';
import type { C } from 'Bar';

import index from './';
`,
parser,
options: [
{
groups: ['external', 'index'],
alphabetize: { order: 'desc' },
},
],
},
parserConfig,
),
],
invalid: [
// Option alphabetize: {order: 'asc'}
test(
{
code: `
import b from 'bar';
import c from 'Bar';
import type { C } from 'Bar';
import a from 'foo';
import type { A } from 'foo';

import index from './';
`,
output: `
import c from 'Bar';
import type { C } from 'Bar';
import b from 'bar';
import a from 'foo';
import type { A } from 'foo';

import index from './';
`,
parser,
options: [
{
groups: ['external', 'index'],
alphabetize: { order: 'asc' },
},
],
errors: [
{
message: process.env.ESLINT_VERSION === '2' ? '`bar` import should occur after import of `Bar`' : /(`bar` import should occur after import of `Bar`)|(`Bar` import should occur before import of `bar`)/,
},
],
},
parserConfig,
),
// Option alphabetize: {order: 'desc'}
test(
{
code: `
import a from 'foo';
import type { A } from 'foo';
import c from 'Bar';
import type { C } from 'Bar';
import b from 'bar';

import index from './';
`,
output: `
import a from 'foo';
import type { A } from 'foo';
import b from 'bar';
import c from 'Bar';
import type { C } from 'Bar';

import index from './';
`,
parser,
options: [
{
groups: ['external', 'index'],
alphabetize: { order: 'desc' },
},
],
errors: [
{
message: process.env.ESLINT_VERSION === '2' ? '`bar` import should occur before import of `Bar`' : /(`bar` import should occur before import of `Bar`)|(`Bar` import should occur after import of `bar`)/,
},
],
},
parserConfig,
),
],
})
})
})