Skip to content

Commit

Permalink
Type-only imports were added in TypeScript 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Sep 20, 2021
1 parent 95bd584 commit 7e2d128
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 69 deletions.
1 change: 0 additions & 1 deletion tests/src/rules/first.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/src/rules/max-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, getTSParsers } from '../utils';

import { RuleTester } from 'eslint';
import eslintPkg from 'eslint/package.json';
import tsPkg from 'typescript/package.json';
import semver from 'semver';

const ruleTester = new RuleTester();
Expand Down Expand Up @@ -91,7 +91,7 @@ ruleTester.run('max-dependencies', rule, {
],
});

(semver.satisfies(eslintPkg.version, '>5.0.0') ? describe.skip : describe)('TypeScript', () => {
(semver.satisfies(tsPkg.version, '>=3.8') ? describe.skip : describe)('TypeScript', () => {
getTSParsers().forEach((parser) => {
ruleTester.run(`max-dependencies (${parser.replace(process.cwd(), '.')})`, rule, {
valid: [
Expand Down
4 changes: 2 additions & 2 deletions tests/src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { test as testUtil, getNonDefaultParsers } from '../utils';
import { RuleTester } from 'eslint';
import eslintPkg from 'eslint/package.json';
import semver from 'semver';
import tsPkg from 'typescript/package.json';

const ruleTester = new RuleTester();
const rule = require('rules/no-duplicates');
Expand Down Expand Up @@ -415,9 +416,8 @@ import {x,y} from './foo'
],
});

context('TypeScript', function () {
(semver.satisfies(tsPkg.version, '>=3.8') ? describe.skip : describe)('TypeScript', () => {
getNonDefaultParsers()
.filter((parser) => parser !== require.resolve('typescript-eslint-parser'))
.forEach((parser) => {
const parserConfig = {
parser,
Expand Down
95 changes: 35 additions & 60 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import typescriptConfig from '../../../config/typescript';
import path from 'path';
import fs from 'fs';
import semver from 'semver';
import eslintPkg from 'eslint/package.json';
import tsPkg from 'typescript/package.json';

import { RuleTester } from 'eslint';
import flatMap from 'array.prototype.flatmap';
Expand Down Expand Up @@ -380,8 +380,7 @@ 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', () => {
(semver.satisfies(tsPkg.version, '>=3.8') ? describe.skip : describe)('TypeScript', () => {
getTSParsers().forEach((parser) => {
const parserConfig = {
parser,
Expand All @@ -391,63 +390,39 @@ ruleTester.run('no-extraneous-dependencies', rule, {
},
};

if (parser !== require.resolve('typescript-eslint-parser')) {
ruleTester.run('no-extraneous-dependencies', rule, {
valid: [
test(Object.assign({
code: 'import type T from "a";',
options: [{ packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
}, parserConfig)),
],
invalid: [
test(Object.assign({
code: 'import T from "a";',
options: [{ packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
errors: [{
message: "'a' should be listed in the project's dependencies, not devDependencies.",
}],
}, 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)),
],
});
}
ruleTester.run('no-extraneous-dependencies', rule, {
valid: [
test(Object.assign({
code: 'import type T from "a";',
options: [{ packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
}, parserConfig)),
],
invalid: [
test(Object.assign({
code: 'import T from "a";',
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: [
],
});
6 changes: 4 additions & 2 deletions tests/src/rules/no-unresolved.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getTSParsers, test, SYNTAX_CASES, testVersion } from '../utils';
import { CASE_SENSITIVE_FS } from 'eslint-module-utils/resolve';

import { RuleTester } from 'eslint';
import semver from 'semver';
import tsPkg from 'typescript/package.json';

const ruleTester = new RuleTester();
const rule = require('rules/no-unresolved');
Expand Down Expand Up @@ -442,8 +444,8 @@ ruleTester.run('import() with built-in parser', rule, {
),
});

context('TypeScript', () => {
getTSParsers().filter(x => x !== require.resolve('typescript-eslint-parser')).forEach((parser) => {
(semver.satisfies(tsPkg.version, '>=3.8') ? describe.skip : describe)('TypeScript', () => {
getTSParsers().forEach((parser) => {
ruleTester.run(`${parser}: no-unresolved ignore type-only`, rule, {
valid: [
test({
Expand Down
4 changes: 2 additions & 2 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { test, getTSParsers, getNonDefaultParsers } from '../utils';
import { RuleTester } from 'eslint';
import eslintPkg from 'eslint/package.json';
import semver from 'semver';
import tsPkg from 'typescript/package.json';
import flatMap from 'array.prototype.flatmap';

const ruleTester = new RuleTester();
Expand Down Expand Up @@ -2281,9 +2282,8 @@ ruleTester.run('order', rule, {
});


context('TypeScript', function () {
(semver.satisfies(tsPkg.version, '>=3.8') ? describe.skip : describe)('TypeScript', () => {
getNonDefaultParsers()
.filter((parser) => parser !== require.resolve('typescript-eslint-parser'))
.forEach((parser) => {
const parserConfig = {
parser,
Expand Down

0 comments on commit 7e2d128

Please sign in to comment.