Skip to content

Commit fbe87c7

Browse files
author
Orta Therox
authored
Merge pull request microsoft#2028 from microsoft/full_twoslash_sandbox
Adds full twoslash support to the sandbox
2 parents cdad769 + 0be8c3f commit fbe87c7

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

packages/sandbox/src/twoslashSupport.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,23 @@ function setOption(name: string, value: string, opts: CompilerOptions, optMap: M
5757
break
5858

5959
case "list":
60-
opts[opt.name] = value.split(",").map(v => parsePrimitive(v, opt.element!.type as string))
60+
const elementType = opt.element!.type
61+
const strings = value.split(",")
62+
if (typeof elementType === "string") {
63+
opts[opt.name] = strings.map(v => parsePrimitive(v, elementType))
64+
} else {
65+
opts[opt.name] = strings.map(v => getOptionValueFromMap(opt.name, v, elementType as Map<string, string>)!).filter(Boolean)
66+
}
6167
break
6268

63-
default:
64-
opts[opt.name] = opt.type.get(value.toLowerCase())
69+
default: // It's a map!
70+
const optMap = opt.type as Map<string, string>
71+
opts[opt.name] = getOptionValueFromMap(opt.name, value, optMap)
72+
}
6573

66-
if (opts[opt.name] === undefined) {
67-
const keys = Array.from(opt.type.keys() as any)
68-
console.log(`Invalid value ${value} for ${opt.name}. Allowed values: ${keys.join(",")}`)
69-
}
74+
if (opts[opt.name] === undefined) {
75+
const keys = Array.from(opt.type.keys() as any)
76+
console.log(`Invalid value ${value} for ${opt.name}. Allowed values: ${keys.join(",")}`)
7077
}
7178
}
7279

@@ -82,6 +89,21 @@ export function parsePrimitive(value: string, type: string): any {
8289
console.log(`Unknown primitive type ${type} with - ${value}`)
8390
}
8491

92+
93+
function getOptionValueFromMap(name: string, key: string, optMap: Map<string, string>) {
94+
const result = optMap.get(key.toLowerCase())
95+
if (result === undefined) {
96+
const keys = Array.from(optMap.keys() as any)
97+
98+
console.error(
99+
`Invalid inline compiler value`,
100+
`Got ${key} for ${name} but it is not a supported value by the TS compiler.`,
101+
`Allowed values: ${keys.join(",")}`
102+
)
103+
}
104+
return result
105+
}
106+
85107
// Function to generate autocompletion results
86108
export const twoslashCompletions = (ts: TS, monaco: typeof import("monaco-editor")) => (
87109
model: import("monaco-editor").editor.ITextModel,

0 commit comments

Comments
 (0)