From 52f7c80d411f82a31c800e605ddc0b5e3fe6a4bf Mon Sep 17 00:00:00 2001 From: leegeunhyeok Date: Sat, 14 Sep 2024 22:17:17 +0900 Subject: [PATCH] fix(transformer): strip typeof import statements using regexp --- packages/transformer/lib/pipelines/builder.ts | 2 +- packages/transformer/lib/transformer/sucrase/sucrase.ts | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/transformer/lib/pipelines/builder.ts b/packages/transformer/lib/pipelines/builder.ts index 366f9e28..101ac4c7 100644 --- a/packages/transformer/lib/pipelines/builder.ts +++ b/packages/transformer/lib/pipelines/builder.ts @@ -10,7 +10,7 @@ import type { import { babelPresets } from '../transformer'; import type { TransformPipeline } from './pipeline'; -const FLOW_SYMBOL = ['@flow', '@noflow'] as const; +const FLOW_SYMBOL = ['@flow', '@noflow', 'import typeof'] as const; export abstract class TransformPipelineBuilder< Step extends TransformStep, diff --git a/packages/transformer/lib/transformer/sucrase/sucrase.ts b/packages/transformer/lib/transformer/sucrase/sucrase.ts index a0d5baf2..3eb23d10 100644 --- a/packages/transformer/lib/transformer/sucrase/sucrase.ts +++ b/packages/transformer/lib/transformer/sucrase/sucrase.ts @@ -2,12 +2,11 @@ import { transform, type Transform } from 'sucrase'; import type { SyncTransformer } from '../../types'; const TRANSFORM_FOR_STRIP_FLOW: Transform[] = ['flow', 'jsx']; +const typeofImportRegExp = + /import\s+typeof\s+[^;]+\s+from\s+['"][^'"]+['"];\s*/g; const stripFlowTypeofImportStatements = (code: string): string => { - return code - .split('\n') - .filter((line) => !line.startsWith('import typeof ')) - .join('\n'); + return code.replace(typeofImportRegExp, ''); }; export const stripFlowWithSucrase: SyncTransformer = (code, context) => {