Skip to content

Commit

Permalink
feat: support spaces before @filename comments (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
btea authored May 5, 2024
1 parent 7ad2ba8 commit 80d48a2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
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")')
})

0 comments on commit 80d48a2

Please sign in to comment.