Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support spaces before @filename comments #37

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/twoslash/src/regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions packages/twoslash/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -90,8 +90,6 @@ export function getOptionValueFromMap(name: string, key: string, optMap: Map<str
return result
}

const reFilenamesMakers = /^\/\/\s?@filename: (.+)$/mg

export function splitFiles(code: string, defaultFileName: string, root: string) {
const matches = Array.from(code.matchAll(reFilenamesMakers))
const allFilenames = matches.map(match => match[1].trimEnd())
Expand Down
17 changes: 17 additions & 0 deletions packages/twoslash/test/compiler_options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")')
})
Loading