@@ -10,6 +10,8 @@ type Monaco = typeof import("monaco-editor")
10
10
export function getDefaultSandboxCompilerOptions ( config : SandboxConfig , monaco : Monaco ) {
11
11
const useJavaScript = config . filetype === "js"
12
12
const settings : CompilerOptions = {
13
+ strict : true ,
14
+
13
15
noImplicitAny : true ,
14
16
strictNullChecks : ! useJavaScript ,
15
17
strictFunctionTypes : true ,
@@ -60,24 +62,40 @@ export function getDefaultSandboxCompilerOptions(config: SandboxConfig, monaco:
60
62
* Loop through all of the entries in the existing compiler options then compare them with the
61
63
* query params and return an object which is the changed settings via the query params
62
64
*/
63
- export const getCompilerOptionsFromParams = ( options : CompilerOptions , params : URLSearchParams ) : CompilerOptions => {
64
- const urlDefaults = Object . entries ( options ) . reduce ( ( acc : any , [ key , value ] ) => {
65
- if ( params . has ( key ) ) {
66
- const urlValue = params . get ( key ) !
67
-
68
- if ( urlValue === "true" ) {
69
- acc [ key ] = true
70
- } else if ( urlValue === "false" ) {
71
- acc [ key ] = false
72
- } else if ( ! isNaN ( parseInt ( urlValue , 10 ) ) ) {
73
- acc [ key ] = parseInt ( urlValue , 10 )
65
+ export const getCompilerOptionsFromParams = (
66
+ playgroundDefaults : CompilerOptions ,
67
+ ts : typeof import ( "typescript" ) ,
68
+ params : URLSearchParams
69
+ ) : CompilerOptions => {
70
+ const returnedOptions : CompilerOptions = { }
71
+
72
+ params . forEach ( ( val , key ) => {
73
+ // First use the defaults object to drop compiler flags which are already set to the default
74
+ if ( playgroundDefaults [ key ] ) {
75
+ let toSet = undefined
76
+ if ( val === "true" && playgroundDefaults [ key ] !== true ) {
77
+ toSet = true
78
+ } else if ( val === "false" && playgroundDefaults [ key ] !== false ) {
79
+ toSet = false
80
+ } else if ( ! isNaN ( parseInt ( val , 10 ) ) && playgroundDefaults [ key ] !== parseInt ( val , 10 ) ) {
81
+ toSet = parseInt ( val , 10 )
74
82
}
75
- }
76
83
77
- return acc
78
- } , { } )
84
+ if ( toSet !== undefined ) returnedOptions [ key ] = toSet
85
+ } else {
86
+ // If that doesn't work, double check that the flag exists and allow it through
87
+ // @ts -ignore
88
+ const flagExists = ts . optionDeclarations . find ( opt => opt . name === key )
89
+ if ( flagExists ) {
90
+ let realValue : number | boolean = true
91
+ if ( val === "false" ) realValue = false
92
+ if ( ! isNaN ( parseInt ( val , 10 ) ) ) realValue = parseInt ( val , 10 )
93
+ returnedOptions [ key ] = realValue
94
+ }
95
+ }
96
+ } )
79
97
80
- return urlDefaults
98
+ return returnedOptions
81
99
}
82
100
83
101
// Can't set sandbox to be the right type because the param would contain this function
0 commit comments