From 2df9be6b7edd150c9ac94b2d479ea51f5fbe66d0 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 4 Sep 2022 18:44:40 +0930 Subject: [PATCH] address review --- src/rules/consistent-type-specifier-style.js | 10 +-- .../rules/consistent-type-specifier-style.js | 90 +++++++++++++++++-- 2 files changed, 87 insertions(+), 13 deletions(-) diff --git a/src/rules/consistent-type-specifier-style.js b/src/rules/consistent-type-specifier-style.js index 67532e6ac1..81bcb7d006 100644 --- a/src/rules/consistent-type-specifier-style.js +++ b/src/rules/consistent-type-specifier-style.js @@ -1,5 +1,9 @@ import docsUrl from '../docsUrl'; +function isComma(token) { + return token.type === 'Punctuator' && token.value === ','; +} + module.exports = { meta: { type: 'suggestion', @@ -24,6 +28,7 @@ module.exports = { return { ImportDeclaration(node) { if (node.importKind === 'value' || node.importKind == null) { + // top-level value / unknown is valid return; } @@ -220,8 +225,3 @@ module.exports = { }; }, }; - -function isComma(token) { - return token.type === 'Punctuator' && token.value === ','; -} - diff --git a/tests/src/rules/consistent-type-specifier-style.js b/tests/src/rules/consistent-type-specifier-style.js index fa83abd96b..0ee437ead4 100644 --- a/tests/src/rules/consistent-type-specifier-style.js +++ b/tests/src/rules/consistent-type-specifier-style.js @@ -8,14 +8,88 @@ const COMMON_TESTS = { // // always valid // - test({ code: "import Foo from 'Foo';" }), - test({ code: "import type Foo from 'Foo';" }), - test({ code: "import { Foo } from 'Foo';" }), - test({ code: "import { Foo as Bar } from 'Foo';" }), - test({ code: "import * as Foo from 'Foo';" }), - test({ code: "import 'Foo';" }), - test({ code: "import {} from 'Foo';" }), - test({ code: "import type {} from 'Foo';" }), + // prefer-inline + test({ + code: "import Foo from 'Foo';", + options: ['prefer-inline'], + }), + test({ + code: "import type Foo from 'Foo';", + options: ['prefer-inline'], + }), + test({ + code: "import { Foo } from 'Foo';", + options: ['prefer-inline'], + }), + test({ + code: "import { Foo as Bar } from 'Foo';", + options: ['prefer-inline'], + }), + test({ + code: "import * as Foo from 'Foo';", + options: ['prefer-inline'], + }), + test({ + code: "import 'Foo';", + options: ['prefer-inline'], + }), + test({ + code: "import {} from 'Foo';", + options: ['prefer-inline'], + }), + test({ + code: "import type {} from 'Foo';", + options: ['prefer-inline'], + }), + // prefer-top-level + test({ + code: "import Foo from 'Foo';", + options: ['prefer-top-level'], + }), + test({ + code: "import type Foo from 'Foo';", + options: ['prefer-top-level'], + }), + test({ + code: "import { Foo } from 'Foo';", + options: ['prefer-top-level'], + }), + test({ + code: "import { Foo as Bar } from 'Foo';", + options: ['prefer-top-level'], + }), + test({ + code: "import * as Foo from 'Foo';", + options: ['prefer-top-level'], + }), + test({ + code: "import 'Foo';", + options: ['prefer-top-level'], + }), + test({ + code: "import {} from 'Foo';", + options: ['prefer-top-level'], + }), + test({ + code: "import type {} from 'Foo';", + options: ['prefer-top-level'], + }), + + // + // prefer-inline + // + { + code: "import { type Foo } from 'Foo';", + options: ['prefer-inline'], + }, + { + code: "import { type Foo as Bar } from 'Foo';", + options: ['prefer-inline'], + }, + { + code: "import { type Foo, type Bar, Baz, Bam } from 'Foo';", + options: ['prefer-inline'], + }, // // prefer-top-level