From 66739e1df38fff00b4ab7259ea2f8a8c4d642406 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Sun, 5 May 2024 12:10:10 +0800 Subject: [PATCH] feat: support spaces before `@filename` comments --- packages/twoslash/src/regexp.ts | 1 + packages/twoslash/src/utils.ts | 4 +--- packages/twoslash/test/compiler_options.test.ts | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/twoslash/src/regexp.ts b/packages/twoslash/src/regexp.ts index ca5c6b1..48806e9 100644 --- a/packages/twoslash/src/regexp.ts +++ b/packages/twoslash/src/regexp.ts @@ -6,3 +6,4 @@ export const reCutBefore = /^[\t\v\f ]*\/\/\s?---cut(-before)?---\r?\n/mg export const reCutAfter = /^[\t\v\f ]*\/\/\s?---cut-after---$/mg export const reCutStart = /^[\t\v\f ]*\/\/\s?---cut-start---$/mg export const reCutEnd = /^[\t\v\f ]*\/\/\s?---cut-end---\r?\n/mg +export const reFilenamesMakers = /^[\t\v\f ]*\/\/\s?@filename: (.+)$/mg diff --git a/packages/twoslash/src/utils.ts b/packages/twoslash/src/utils.ts index 1974463..d0af1a5 100644 --- a/packages/twoslash/src/utils.ts +++ b/packages/twoslash/src/utils.ts @@ -4,7 +4,7 @@ import type { Range, createPositionConverter } from 'twoslash-protocol' import { TwoslashError } from './error' import type { CompilerOptionDeclaration, ParsedFlagNotation, TwoslashReturnMeta, VirtualFile } from './types' import { defaultHandbookOptions } from './defaults' -import { reAnnonateMarkers, reConfigBoolean, reConfigValue, reCutAfter, reCutBefore, reCutEnd, reCutStart } from './regexp' +import { reAnnonateMarkers, reConfigBoolean, reConfigValue, reCutAfter, reCutBefore, reCutEnd, reCutStart, reFilenamesMakers } from './regexp' export function getObjectHash(obj: any): string { return objectHash(obj) @@ -90,8 +90,6 @@ export function getOptionValueFromMap(name: string, key: string, optMap: Map match[1].trimEnd()) diff --git a/packages/twoslash/test/compiler_options.test.ts b/packages/twoslash/test/compiler_options.test.ts index 1f0d12d..5de24ad 100755 --- a/packages/twoslash/test/compiler_options.test.ts +++ b/packages/twoslash/test/compiler_options.test.ts @@ -18,3 +18,20 @@ console.log(helloWorld) expect(result.errors).toEqual([]) expect(result.code!).toContain('require("./file-with-export")') }) + +it('supports space before @filename', () => { + const files = ` + // @filename: file-with-export.ts +export const helloWorld = "Example string"; + + // @filename: index.ts +import {helloWorld} from "./file-with-export" +console.log(helloWorld) +` + const result = twoslasher(files, 'ts', { + handbookOptions: { showEmit: true }, + compilerOptions: { module: ModuleKind.CommonJS }, + }) + expect(result.errors).toEqual([]) + expect(result.code!).toContain('require("./file-with-export")') +})