Skip to content

Commit

Permalink
[Tests] order: Add TS import type tests
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Mui <kmui2@wisc.edu>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
  • Loading branch information
kmui2 and ljharb committed Apr 27, 2020
1 parent 6159ce9 commit eb2b7ea
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 2 deletions.
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,
),
],
})
})
})

0 comments on commit eb2b7ea

Please sign in to comment.