From caba18c95112c2095bce243e23c59ea288516853 Mon Sep 17 00:00:00 2001 From: Jack Bates Date: Mon, 20 Sep 2021 11:52:43 -0700 Subject: [PATCH] [Tests] Type-only imports were added in TypeScript ESTree 2.23.0 --- tests/src/rules/first.js | 1 - tests/src/rules/max-dependencies.js | 81 +++++++++--------- tests/src/rules/no-duplicates.js | 1 + tests/src/rules/no-extraneous-dependencies.js | 82 +++++++------------ tests/src/rules/no-unresolved.js | 1 + tests/src/rules/order.js | 1 + 6 files changed, 73 insertions(+), 94 deletions(-) diff --git a/tests/src/rules/first.js b/tests/src/rules/first.js index 9d27870677..05328e51e9 100644 --- a/tests/src/rules/first.js +++ b/tests/src/rules/first.js @@ -80,7 +80,6 @@ ruleTester.run('first', rule, { context('TypeScript', function () { getTSParsers() - .filter((parser) => parser !== require.resolve('typescript-eslint-parser')) .forEach((parser) => { const parserConfig = { parser, diff --git a/tests/src/rules/max-dependencies.js b/tests/src/rules/max-dependencies.js index bab38496e0..6d80bbf046 100644 --- a/tests/src/rules/max-dependencies.js +++ b/tests/src/rules/max-dependencies.js @@ -1,8 +1,6 @@ import { test, getTSParsers } from '../utils'; import { RuleTester } from 'eslint'; -import eslintPkg from 'eslint/package.json'; -import semver from 'semver'; const ruleTester = new RuleTester(); const rule = require('rules/max-dependencies'); @@ -91,43 +89,46 @@ ruleTester.run('max-dependencies', rule, { ], }); -(semver.satisfies(eslintPkg.version, '>5.0.0') ? describe.skip : describe)('TypeScript', () => { - getTSParsers().forEach((parser) => { - ruleTester.run(`max-dependencies (${parser.replace(process.cwd(), '.')})`, rule, { - valid: [ - test({ - code: 'import type { x } from \'./foo\'; import { y } from \'./bar\';', - parser, - options: [{ - max: 1, - ignoreTypeImports: true, - }], - }), - ], - invalid: [ - test({ - code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'', - parser, - options: [{ - max: 1, - }], - errors: [ - 'Maximum number of dependencies (1) exceeded.', - ], - }), - - test({ - code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'; import type { z } from \'./baz\'', - parser, - options: [{ - max: 2, - ignoreTypeImports: false, - }], - errors: [ - 'Maximum number of dependencies (2) exceeded.', - ], - }), - ], +describe('TypeScript', () => { + getTSParsers() + // Type-only imports were added in TypeScript ESTree 2.23.0 + .filter((parser) => parser !== require.resolve('typescript-eslint-parser')) + .forEach((parser) => { + ruleTester.run(`max-dependencies (${parser.replace(process.cwd(), '.')})`, rule, { + valid: [ + test({ + code: 'import type { x } from \'./foo\'; import { y } from \'./bar\';', + parser, + options: [{ + max: 1, + ignoreTypeImports: true, + }], + }), + ], + invalid: [ + test({ + code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'', + parser, + options: [{ + max: 1, + }], + errors: [ + 'Maximum number of dependencies (1) exceeded.', + ], + }), + + test({ + code: 'import type { x } from \'./foo\'; import type { y } from \'./bar\'; import type { z } from \'./baz\'', + parser, + options: [{ + max: 2, + ignoreTypeImports: false, + }], + errors: [ + 'Maximum number of dependencies (2) exceeded.', + ], + }), + ], + }); }); - }); }); diff --git a/tests/src/rules/no-duplicates.js b/tests/src/rules/no-duplicates.js index e550b63ce7..ad39543f81 100644 --- a/tests/src/rules/no-duplicates.js +++ b/tests/src/rules/no-duplicates.js @@ -417,6 +417,7 @@ import {x,y} from './foo' context('TypeScript', function () { getNonDefaultParsers() + // Type-only imports were added in TypeScript ESTree 2.23.0 .filter((parser) => parser !== require.resolve('typescript-eslint-parser')) .forEach((parser) => { const parserConfig = { diff --git a/tests/src/rules/no-extraneous-dependencies.js b/tests/src/rules/no-extraneous-dependencies.js index 29604af170..852dbdf4e7 100644 --- a/tests/src/rules/no-extraneous-dependencies.js +++ b/tests/src/rules/no-extraneous-dependencies.js @@ -2,8 +2,6 @@ import { getTSParsers, test, testFilePath } from '../utils'; import typescriptConfig from '../../../config/typescript'; import path from 'path'; import fs from 'fs'; -import semver from 'semver'; -import eslintPkg from 'eslint/package.json'; import { RuleTester } from 'eslint'; import flatMap from 'array.prototype.flatmap'; @@ -380,18 +378,19 @@ ruleTester.run('no-extraneous-dependencies', rule, { ], }); -// TODO: figure out why these tests fail in eslint 4 -(semver.satisfies(eslintPkg.version, '^4') ? describe.skip : describe)('TypeScript', () => { - getTSParsers().forEach((parser) => { - const parserConfig = { - parser, - settings: { - 'import/parsers': { [parser]: ['.ts'] }, - 'import/resolver': { 'eslint-import-resolver-typescript': true }, - }, - }; +describe('TypeScript', () => { + getTSParsers() + // Type-only imports were added in TypeScript ESTree 2.23.0 + .filter((parser) => parser !== require.resolve('typescript-eslint-parser')) + .forEach((parser) => { + const parserConfig = { + parser, + settings: { + 'import/parsers': { [parser]: ['.ts'] }, + 'import/resolver': { 'eslint-import-resolver-typescript': true }, + }, + }; - if (parser !== require.resolve('typescript-eslint-parser')) { ruleTester.run('no-extraneous-dependencies', rule, { valid: [ test(Object.assign({ @@ -409,45 +408,22 @@ ruleTester.run('no-extraneous-dependencies', rule, { }, parserConfig)), ], }); - } else { - ruleTester.run('no-extraneous-dependencies', rule, { - valid: [], - invalid: [ - test(Object.assign({ - code: 'import T from "a"; /* typescript-eslint-parser */', - options: [{ packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], - errors: [{ - message: "'a' should be listed in the project's dependencies, not devDependencies.", - }], - }, parserConfig)), - test(Object.assign({ - code: 'import type T from "a"; /* typescript-eslint-parser */', - options: [{ packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], - errors: [{ - message: "'a' should be listed in the project's dependencies, not devDependencies.", - }], - }, parserConfig)), - ], - }); - } - }); + }); }); -if (semver.satisfies(eslintPkg.version, '>5.0.0')) { - typescriptRuleTester.run('no-extraneous-dependencies typescript type imports', rule, { - valid: [ - test({ - code: 'import type MyType from "not-a-dependency";', - filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'), - parser: require.resolve('babel-eslint'), - }), - test({ - code: 'import type { MyType } from "not-a-dependency";', - filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'), - parser: require.resolve('babel-eslint'), - }), - ], - invalid: [ - ], - }); -} +typescriptRuleTester.run('no-extraneous-dependencies typescript type imports', rule, { + valid: [ + test({ + code: 'import type MyType from "not-a-dependency";', + filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'), + parser: require.resolve('babel-eslint'), + }), + test({ + code: 'import type { MyType } from "not-a-dependency";', + filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'), + parser: require.resolve('babel-eslint'), + }), + ], + invalid: [ + ], +}); diff --git a/tests/src/rules/no-unresolved.js b/tests/src/rules/no-unresolved.js index 9e6db8c04b..a5ff6da5d1 100644 --- a/tests/src/rules/no-unresolved.js +++ b/tests/src/rules/no-unresolved.js @@ -443,6 +443,7 @@ ruleTester.run('import() with built-in parser', rule, { }); context('TypeScript', () => { + // Type-only imports were added in TypeScript ESTree 2.23.0 getTSParsers().filter(x => x !== require.resolve('typescript-eslint-parser')).forEach((parser) => { ruleTester.run(`${parser}: no-unresolved ignore type-only`, rule, { valid: [ diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index 146306259f..4b15cec898 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -2283,6 +2283,7 @@ ruleTester.run('order', rule, { context('TypeScript', function () { getNonDefaultParsers() + // Type-only imports were added in TypeScript ESTree 2.23.0 .filter((parser) => parser !== require.resolve('typescript-eslint-parser')) .forEach((parser) => { const parserConfig = {