Skip to content

Commit

Permalink
Type-only imports were added in TypeScript ESTree 2.23.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Sep 20, 2021
1 parent 95bd584 commit 98eba27
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 94 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
81 changes: 41 additions & 40 deletions tests/src/rules/max-dependencies.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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.',
],
}),
],
});
});
});
});
1 change: 1 addition & 0 deletions tests/src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
82 changes: 29 additions & 53 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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({
Expand All @@ -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: [
],
});
1 change: 1 addition & 0 deletions tests/src/rules/no-unresolved.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
1 change: 1 addition & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 98eba27

Please sign in to comment.