From c871fd8780482ec693fa60cacda4fcbe091025c7 Mon Sep 17 00:00:00 2001 From: wenzhe Date: Thu, 11 Jul 2024 18:30:22 +0800 Subject: [PATCH] fix(linter): correct find first non whitespace logic --- .../typescript/consistent_type_imports.rs | 105 +++++++++++++++++- .../snapshots/consistent_type_imports.snap | 20 ++++ 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/crates/oxc_linter/src/rules/typescript/consistent_type_imports.rs b/crates/oxc_linter/src/rules/typescript/consistent_type_imports.rs index 86c25344e06265..0d12c6100bc0b4 100644 --- a/crates/oxc_linter/src/rules/typescript/consistent_type_imports.rs +++ b/crates/oxc_linter/src/rules/typescript/consistent_type_imports.rs @@ -526,7 +526,7 @@ fn fix_insert_named_specifiers_in_named_specifier_list<'a>( let close_brace = try_find_char(import_text, '}')?; let first_non_whitespace_before_close_brace = - import_text[..close_brace as usize].chars().rev().find(|c| c.is_whitespace()); + import_text[..close_brace as usize].chars().rev().find(|c| !c.is_whitespace()); let span = Span::new(import_decl.span().start + close_brace, import_decl.span().start + close_brace); @@ -907,8 +907,15 @@ fn fix_remove_type_specifier_from_import_specifier<'a>( fn test() { use crate::tester::Tester; - fn remove_prefix_space(str: &str) -> String { - str.lines().map(str::trim).collect::>().join("\n") + fn remove_common_prefix_space(str: &str) -> String { + let first_content_line = str.lines().find(|line| line.trim() != "").unwrap(); + let prefix_space = + first_content_line.chars().take_while(|c| c.is_whitespace()).collect::(); + + str.lines() + .map(|line| line.strip_prefix(&prefix_space).unwrap_or(line).to_string()) + .collect::>() + .join("\n") } let pass = vec![ @@ -2177,6 +2184,38 @@ fn test() { // ", // None, // ), + ( + " + import type { + StorageProvider, + } from '../../../fundamentals'; + import { + Config, + OnEvent, + StorageProviderFactory, + URLHelper, + } from '../../../fundamentals'; + + type A = StorageProvider + type B = Config + type C = OnEvent + type D = URLHelper + ", + None, + ), + ( + " + import type { + TransformResult, + } from './plugin' + import { defineParallelPlugin,DefineParallelPluginResult } from './plugin' + + type A = TransformResult; + type B = DefineParallelPluginResult; + const c = defineParallelPlugin() + ", + None, + ), ]; let fix = vec![ @@ -3258,6 +3297,64 @@ fn test() { ", None, ), + ( + " + import type { + StorageProvider, + } from '../../../fundamentals'; + import { + Config, + OnEvent, + StorageProviderFactory, + URLHelper, + } from '../../../fundamentals'; + + type A = StorageProvider + type B = Config + type C = OnEvent + type D = URLHelper + ", + " + import type { + StorageProvider, + + Config, + OnEvent, + URLHelper} from '../../../fundamentals'; + import { + StorageProviderFactory + } from '../../../fundamentals'; + + type A = StorageProvider + type B = Config + type C = OnEvent + type D = URLHelper + ", + None, + ), + ( + " + import type { + TransformResult, + } from './plugin' + import { defineParallelPlugin, DefineParallelPluginResult } from './plugin' + + type A = TransformResult; + type B = DefineParallelPluginResult; + const c = defineParallelPlugin() + ", + " + import type { + TransformResult, + DefineParallelPluginResult } from './plugin' + import { defineParallelPlugin } from './plugin' + + type A = TransformResult; + type B = DefineParallelPluginResult; + const c = defineParallelPlugin() + ", + None, + ), ]; // To format fix code. @@ -3267,7 +3364,7 @@ fn test() { // '\n'`, so let's remove the prefix space. let fix = fix .into_iter() - .map(|(a, b, c)| (remove_prefix_space(a), remove_prefix_space(b), c)) + .map(|(a, b, c)| (remove_common_prefix_space(a), remove_common_prefix_space(b), c)) .collect::>(); Tester::new(ConsistentTypeImports::NAME, pass, fail).expect_fix(fix).test_and_snapshot(); diff --git a/crates/oxc_linter/src/snapshots/consistent_type_imports.snap b/crates/oxc_linter/src/snapshots/consistent_type_imports.snap index b27ddfd0a24687..2712c36d353fab 100644 --- a/crates/oxc_linter/src/snapshots/consistent_type_imports.snap +++ b/crates/oxc_linter/src/snapshots/consistent_type_imports.snap @@ -616,3 +616,23 @@ source: crates/oxc_linter/src/tester.rs · ─────────────────────────────── 4 │ function test(foo: Foo) {} ╰──── + + ⚠ typescript-eslint(consistent-type-imports): Imports Config, OnEvent, and URLHelper are only used as type. + ╭─[consistent_type_imports.tsx:5:13] + 4 │ } from '../../../fundamentals'; + 5 │ ╭─▶ import { + 6 │ │ Config, + 7 │ │ OnEvent, + 8 │ │ StorageProviderFactory, + 9 │ │ URLHelper, + 10 │ ╰─▶ } from '../../../fundamentals'; + 11 │ + ╰──── + + ⚠ typescript-eslint(consistent-type-imports): Imports DefineParallelPluginResult are only used as type. + ╭─[consistent_type_imports.tsx:5:13] + 4 │ } from './plugin' + 5 │ import { defineParallelPlugin,DefineParallelPluginResult } from './plugin' + · ────────────────────────────────────────────────────────────────────────── + 6 │ + ╰────