Skip to content

Commit

Permalink
feat: support cut ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 13, 2024
1 parent 9d81c92 commit 7702562
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 8 deletions.
51 changes: 43 additions & 8 deletions packages/twoslash/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const reConfigBoolean = /^\/\/\s?@(\w+)$/mg
const reConfigValue = /^\/\/\s?@(\w+):\s?(.+)$/mg
const reAnnonateMarkers = /^\s*\/\/\s*\^(\?|\||\^+)( .*)?$/mg

const cutString = '// ---cut---\n'
const cutAfterString = '// ---cut-after---\n'
// TODO: feat cut range
const reCutBefore = /^\/\/\s?---cut(-before)?---$/mg
const reCutAfter = /^\/\/\s?---cut-after---$/mg
const reCutStart = /^\/\/\s?---cut-start---$/mg
const reCutEnd = /^\/\/\s?---cut-end---$/mg

/**
* Create a TwoSlash instance with cached TS environments
Expand Down Expand Up @@ -132,11 +133,7 @@ export function createTwoSlasher(createOptions: CreateTwoSlashOptions = {}): Two
const pc = createPositionConverter(code)

// #region extract cuts
if (code.includes(cutString))
meta.removals.push([0, code.indexOf(cutString) + cutString.length])

if (code.includes(cutAfterString))
meta.removals.push([code.indexOf(cutAfterString), code.length])
meta.removals.push(...extractCuts(code))
// #endregion

// #region extract markers
Expand Down Expand Up @@ -461,6 +458,44 @@ export function createTwoSlasher(createOptions: CreateTwoSlashOptions = {}): Two
return twoslasher
}

function extractCuts(code: string) {
const removals: Range[] = []

const cutBefore = [...code.matchAll(reCutBefore)]
const cutAfter = [...code.matchAll(reCutAfter)]
const cutStart = [...code.matchAll(reCutStart)]
const cutEnd = [...code.matchAll(reCutEnd)]

if (cutBefore.length) {
const last = cutBefore[cutBefore.length - 1]
removals.push([0, last.index! + last[0].length + 1])
}
if (cutAfter.length) {
const first = cutAfter[0]
removals.push([first.index!, code.length])
}
if (cutStart.length !== cutEnd.length) {
throw new TwoslashError(
`Mismatched cut markers`,
`You have ${cutStart.length} cut-starts and ${cutEnd.length} cut-ends`,
`Make sure you have a matching pair for each.`,
)
}
for (let i = 0; i < cutStart.length; i++) {
const start = cutStart[i]
const end = cutEnd[i]
if (start.index! > end.index!) {
throw new TwoslashError(
`Mismatched cut markers`,
`You have a cut-start at ${start.index} which is after the cut-end at ${end.index}`,
`Make sure you have a matching pair for each.`,
)
}
removals.push([start.index!, end.index! + end[0].length + 1])
}
return removals
}

/**
* Run TwoSlash on a string of code
*
Expand Down
16 changes: 16 additions & 0 deletions packages/twoslash/test/fixtures/tests/cut_ranges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const a = 1
// ---cut---
// multiple cut before, we cut from the last one
const b = 2
// ---cut-before---
const c = 3
// ---cut-start---
const d = 4 // cut in range
// ---cut-end---
const e = 5
// ---cut-start---
const f = 6 // cut in range
// ---cut-end---
const g = 7
// ---cut-after---
const h = 8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// ---cut-start---
const a = 3
// ---cut-end---
const b = 4
// ---cut-start---
const c = 5

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const a = 3
// ---cut-end---
const b = 4 // cut in range
// ---cut-start---
const c = 5

33 changes: 33 additions & 0 deletions packages/twoslash/test/results/tests/cut_ranges.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7702562

Please sign in to comment.