From eb2b7eac06147d9c88f39b780a048896eb6b7d2c Mon Sep 17 00:00:00 2001 From: Kevin Mui Date: Sun, 26 Apr 2020 20:49:38 -0500 Subject: [PATCH] [Tests] `order`: Add TS import type tests Co-authored-by: Kevin Mui Co-authored-by: Jordan Harband --- CHANGELOG.md | 2 + tests/src/rules/order.js | 144 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 144 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9cfe0d71..c5634132e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 529582e84..f6e2dddba 100644 --- a/tests/src/rules/order.js +++ b/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' @@ -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`', }, { @@ -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, + ), + ], + }) + }) +})