(option: CommandLineOption, value: unknown, source: OptionSource)
| 215 | } |
| 216 | |
| 217 | function readValue(option: CommandLineOption, value: unknown, source: OptionSource): ReadValueResult { |
| 218 | if (value === null) return { value }; |
| 219 | |
| 220 | switch (option.type) { |
| 221 | case "boolean": |
| 222 | case "string": { |
| 223 | if (typeof value !== option.type) { |
| 224 | return { |
| 225 | value: undefined, |
| 226 | error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, option.type), |
| 227 | }; |
| 228 | } |
| 229 | |
| 230 | return { value }; |
| 231 | } |
| 232 | case "array": |
| 233 | case "json-array-of-objects": { |
| 234 | const isInvalidNonCliValue = source === OptionSource.TsConfig && !Array.isArray(value); |
| 235 | const isInvalidCliValue = source === OptionSource.CommandLine && typeof value !== "string"; |
| 236 | |
| 237 | if (isInvalidNonCliValue || isInvalidCliValue) { |
| 238 | return { |
| 239 | value: undefined, |
| 240 | error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, option.type), |
| 241 | }; |
| 242 | } |
| 243 | |
| 244 | const shouldParseValue = source === OptionSource.CommandLine && typeof value === "string"; |
| 245 | if (!shouldParseValue) return { value }; |
| 246 | |
| 247 | if (option.type === "array") { |
| 248 | const array = value.split(","); |
| 249 | return { value: array }; |
| 250 | } |
| 251 | |
| 252 | try { |
| 253 | const objects = JSON.parse(value); |
| 254 | if (!Array.isArray(objects)) { |
| 255 | return { |
| 256 | value: undefined, |
| 257 | error: cliDiagnostics.compilerOptionRequiresAValueOfType(option.name, option.type), |
| 258 | }; |
| 259 | } |
| 260 | |
| 261 | return { value: objects }; |
| 262 | } catch (e) { |
| 263 | if (!(e instanceof SyntaxError)) throw e; |
| 264 | |
| 265 | return { |
| 266 | value: undefined, |
| 267 | error: cliDiagnostics.compilerOptionCouldNotParseJson(option.name, e.message), |
| 268 | }; |
| 269 | } |
| 270 | } |
| 271 | case "enum": { |
| 272 | if (typeof value !== "string") { |
| 273 | return { |
| 274 | value: undefined, |
no outgoing calls
no test coverage detected