From 6f5d95af708f88c5afd8e3f476ceeaa1e7b0e0e7 Mon Sep 17 00:00:00 2001 From: Taye Adeyemi Date: Sat, 13 Jun 2020 13:27:51 +0200 Subject: [PATCH] [Tests] `no-extraneous-dependencies`: add tests for importing types --- CHANGELOG.md | 5 + tests/src/rules/no-extraneous-dependencies.js | 126 ++++++++++-------- 2 files changed, 79 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ad6bc43b..08f0e83c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ### Fixed - [`no-extraneous-dependencies`]/TypeScript: do not error when importing type from dev dependencies ([#1820], thanks [@fernandopasik]) +### Changed +- [`no-extraneous-dependencies`]: add tests for importing types ([#1824], thanks [@taye]) + ## [2.21.2] - 2020-06-09 ### Fixed - [`order`]: avoid a crash on TypeScript’s `export import` syntax ([#1808], thanks [@ljharb]) @@ -707,6 +710,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md +[#1824]: https://github.com/benmosher/eslint-plugin-import/pull/1824 [#1820]: https://github.com/benmosher/eslint-plugin-import/pull/1820 [#1819]: https://github.com/benmosher/eslint-plugin-import/pull/1819 [#1802]: https://github.com/benmosher/eslint-plugin-import/pull/1802 @@ -1225,3 +1229,4 @@ for info on changes for earlier releases. [@malykhinvi]: https://github.com/malykhinvi [@nicolashenry]: https://github.com/nicolashenry [@fernandopasik]: https://github.com/fernandopasik +[@taye]: https://github.com/taye diff --git a/tests/src/rules/no-extraneous-dependencies.js b/tests/src/rules/no-extraneous-dependencies.js index a3ddb0742..77de28b33 100644 --- a/tests/src/rules/no-extraneous-dependencies.js +++ b/tests/src/rules/no-extraneous-dependencies.js @@ -1,11 +1,15 @@ -import { getTSParsers, test } from '../utils' -import * as path from 'path' -import * as fs from 'fs' +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' const ruleTester = new RuleTester() +const typescriptRuleTester = new RuleTester(typescriptConfig) const rule = require('rules/no-extraneous-dependencies') const packageDirWithSyntaxError = path.join(__dirname, '../../files/with-syntax-error') @@ -315,54 +319,72 @@ ruleTester.run('no-extraneous-dependencies', rule, { }) describe('TypeScript', function () { - getTSParsers() - .forEach((parser) => { - const parserConfig = { - parser: parser, - settings: { - 'import/parsers': { [parser]: ['.ts'] }, - 'import/resolver': { 'eslint-import-resolver-typescript': true }, - }, - } + getTSParsers().forEach((parser) => { + const parserConfig = { + parser: 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({ - code: 'import type { JSONSchema7Type } from "@types/json-schema";', - options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], - }, parserConfig)), - ], - invalid: [ - test(Object.assign({ - code: 'import { JSONSchema7Type } from "@types/json-schema";', - options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], - errors: [{ - message: "'@types/json-schema' 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 { JSONSchema7Type } from "@types/json-schema";', - options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], - errors: [{ - message: "'@types/json-schema' should be listed in the project's dependencies, not devDependencies.", - }], - }, parserConfig)), - test(Object.assign({ - code: 'import type { JSONSchema7Type } from "@types/json-schema";', - options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], - errors: [{ - message: "'@types/json-schema' should be listed in the project's dependencies, not devDependencies.", - }], - }, parserConfig)), - ], - }) - } - }) + if (parser !== require.resolve('typescript-eslint-parser')) { + ruleTester.run('no-extraneous-dependencies', rule, { + valid: [ + test(Object.assign({ + code: 'import type { JSONSchema7Type } from "@types/json-schema";', + options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], + }, parserConfig)), + ], + invalid: [ + test(Object.assign({ + code: 'import { JSONSchema7Type } from "@types/json-schema";', + options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], + errors: [{ + message: "'@types/json-schema' 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 { JSONSchema7Type } from "@types/json-schema";', + options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], + errors: [{ + message: "'@types/json-schema' should be listed in the project's dependencies, not devDependencies.", + }], + }, parserConfig)), + test(Object.assign({ + code: 'import type { JSONSchema7Type } from "@types/json-schema";', + options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }], + errors: [{ + message: "'@types/json-schema' 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: [ + ], + }) +}