1
1
namespace ts {
2
2
describe ( "convertCompilerOptionsFromJson" , ( ) => {
3
- function assertCompilerOptions ( json : any , configFileName : string , expectedResult : { compilerOptions : CompilerOptions , errors : Diagnostic [ ] } ) {
3
+ const formatDiagnosticHost : FormatDiagnosticsHost = {
4
+ getCurrentDirectory : ( ) => "/apath/" ,
5
+ getCanonicalFileName : createGetCanonicalFileName ( /*useCaseSensitiveFileNames*/ true ) ,
6
+ getNewLine : ( ) => "\n"
7
+ } ;
8
+
9
+ interface ExpectedResultWithParsingSuccess {
10
+ compilerOptions : CompilerOptions ;
11
+ errors : ReadonlyArray < Diagnostic > ;
12
+ }
13
+
14
+ interface ExpectedResultWithParsingFailure {
15
+ compilerOptions : CompilerOptions ;
16
+ hasParseErrors : true ;
17
+ }
18
+
19
+ type ExpectedResult = ExpectedResultWithParsingSuccess | ExpectedResultWithParsingFailure ;
20
+
21
+ function isExpectedResultWithParsingFailure ( expectedResult : ExpectedResult ) : expectedResult is ExpectedResultWithParsingFailure {
22
+ return ! ! ( expectedResult as ExpectedResultWithParsingFailure ) . hasParseErrors ;
23
+ }
24
+
25
+ function assertCompilerOptions ( json : any , configFileName : string , expectedResult : ExpectedResultWithParsingSuccess ) {
4
26
assertCompilerOptionsWithJson ( json , configFileName , expectedResult ) ;
5
27
assertCompilerOptionsWithJsonNode ( json , configFileName , expectedResult ) ;
6
28
}
7
29
8
- function assertCompilerOptionsWithJson ( json : any , configFileName : string , expectedResult : { compilerOptions : CompilerOptions , errors : Diagnostic [ ] } ) {
9
- const { options : actualCompilerOptions , errors : actualErrors } = convertCompilerOptionsFromJson ( json . compilerOptions , "/apath/" , configFileName ) ;
30
+ function assertCompilerOptionsWithJson ( json : any , configFileName : string , expectedResult : ExpectedResultWithParsingSuccess ) {
31
+ const { options : actualCompilerOptions , errors : actualErrors } = convertCompilerOptionsFromJson ( json . compilerOptions , "/apath/" , configFileName ) ;
10
32
11
33
const parsedCompilerOptions = JSON . stringify ( actualCompilerOptions ) ;
12
34
const expectedCompilerOptions = JSON . stringify ( { ...expectedResult . compilerOptions , configFilePath : configFileName } ) ;
13
35
assert . equal ( parsedCompilerOptions , expectedCompilerOptions ) ;
14
36
15
- const expectedErrors = expectedResult . errors ;
16
- assert . isTrue ( expectedResult . errors . length === actualErrors . length , `Expected error: ${ JSON . stringify ( expectedResult . errors ) } . Actual error: ${ JSON . stringify ( actualErrors ) } .` ) ;
17
- for ( let i = 0 ; i < actualErrors . length ; i ++ ) {
18
- const actualError = actualErrors [ i ] ;
19
- const expectedError = expectedErrors [ i ] ;
20
- assert . equal ( actualError . code , expectedError . code ) ;
21
- assert . equal ( actualError . category , expectedError . category ) ;
22
- assert . equal ( actualError . messageText , expectedError . messageText ) ;
23
- }
37
+ verifyErrors ( actualErrors , expectedResult . errors , /*ignoreLocation*/ true ) ;
24
38
}
25
39
26
- function assertCompilerOptionsWithJsonNode ( json : any , configFileName : string , expectedResult : { compilerOptions : CompilerOptions , errors : Diagnostic [ ] } ) {
27
- const fileText = JSON . stringify ( json ) ;
40
+ function assertCompilerOptionsWithJsonNode ( json : any , configFileName : string , expectedResult : ExpectedResultWithParsingSuccess ) {
41
+ assertCompilerOptionsWithJsonText ( JSON . stringify ( json ) , configFileName , expectedResult ) ;
42
+ }
43
+
44
+ function assertCompilerOptionsWithJsonText ( fileText : string , configFileName : string , expectedResult : ExpectedResult ) {
28
45
const result = parseJsonText ( configFileName , fileText ) ;
29
- assert ( ! result . parseDiagnostics . length ) ;
30
46
assert ( ! ! result . endOfFileToken ) ;
47
+ assert . equal ( ! ! result . parseDiagnostics . length , isExpectedResultWithParsingFailure ( expectedResult ) ) ;
31
48
const host : ParseConfigHost = new fakes . ParseConfigHost ( new vfs . FileSystem ( /*ignoreCase*/ false , { cwd : "/apath/" } ) ) ;
32
49
const { options : actualCompilerOptions , errors : actualParseErrors } = parseJsonSourceFileConfigFileContent ( result , host , "/apath/" , /*existingOptions*/ undefined , configFileName ) ;
33
50
expectedResult . compilerOptions . configFilePath = configFileName ;
@@ -37,17 +54,32 @@ namespace ts {
37
54
assert . equal ( parsedCompilerOptions , expectedCompilerOptions ) ;
38
55
assert . equal ( actualCompilerOptions . configFile , result ) ;
39
56
40
- const actualErrors = filter ( actualParseErrors , error => error . code !== Diagnostics . No_inputs_were_found_in_config_file_0_Specified_include_paths_were_1_and_exclude_paths_were_2 . code ) ;
41
- const expectedErrors = expectedResult . errors ;
42
- assert . isTrue ( expectedResult . errors . length === actualErrors . length , `Expected error: ${ JSON . stringify ( expectedResult . errors ) } . Actual error: ${ JSON . stringify ( actualErrors ) } .` ) ;
57
+ if ( ! isExpectedResultWithParsingFailure ( expectedResult ) ) {
58
+ verifyErrors ( actualParseErrors . filter ( error => error . code !== Diagnostics . No_inputs_were_found_in_config_file_0_Specified_include_paths_were_1_and_exclude_paths_were_2 . code ) , expectedResult . errors ) ;
59
+ }
60
+ }
61
+
62
+ function verifyErrors ( actualErrors : Diagnostic [ ] , expectedErrors : ReadonlyArray < Diagnostic > , ignoreLocation ?: boolean ) {
63
+ assert . isTrue ( expectedErrors . length === actualErrors . length , `Expected error: ${ JSON . stringify ( expectedErrors . map ( getDiagnosticString ) , undefined , " " ) } . Actual error: ${ JSON . stringify ( actualErrors . map ( getDiagnosticString ) , undefined , " " ) } .` ) ;
43
64
for ( let i = 0 ; i < actualErrors . length ; i ++ ) {
44
65
const actualError = actualErrors [ i ] ;
45
66
const expectedError = expectedErrors [ i ] ;
67
+
46
68
assert . equal ( actualError . code , expectedError . code , `Expected error-code: ${ JSON . stringify ( expectedError . code ) } . Actual error-code: ${ JSON . stringify ( actualError . code ) } .` ) ;
47
69
assert . equal ( actualError . category , expectedError . category , `Expected error-category: ${ JSON . stringify ( expectedError . category ) } . Actual error-category: ${ JSON . stringify ( actualError . category ) } .` ) ;
48
- assert ( actualError . file ) ;
49
- assert ( actualError . start ) ;
50
- assert ( actualError . length ) ;
70
+ if ( ! ignoreLocation ) {
71
+ assert ( actualError . file ) ;
72
+ assert ( actualError . start ) ;
73
+ assert ( actualError . length ) ;
74
+ }
75
+ }
76
+
77
+ function getDiagnosticString ( diagnostic : Diagnostic ) {
78
+ if ( ignoreLocation ) {
79
+ const { file, ...rest } = diagnostic ;
80
+ diagnostic = { file : undefined , ...rest } ;
81
+ }
82
+ return formatDiagnostic ( diagnostic , formatDiagnosticHost ) ;
51
83
}
52
84
}
53
85
@@ -539,5 +571,37 @@ namespace ts {
539
571
}
540
572
) ;
541
573
} ) ;
574
+
575
+ it ( "Convert tsconfig options when there are multiple invalid strings" , ( ) => {
576
+ assertCompilerOptionsWithJsonText ( `{
577
+ "compilerOptions": {
578
+ "target": "<%- options.useTsWithBabel ? 'esnext' : 'es5' %>",
579
+ "module": "esnext",
580
+ <%_ if (options.classComponent) { _%>
581
+ "experimentalDecorators": true,
582
+ <%_ } _%>
583
+ "sourceMap": true,
584
+ "types": [
585
+ "webpack-env"<% if (hasMocha || hasJest) { %>,<% } %>
586
+ <%_ if (hasMocha) { _%>
587
+ "mocha",
588
+ "chai"
589
+ <%_ } else if (hasJest) { _%>
590
+ "jest"
591
+ <%_ } _%>
592
+ ]
593
+ }
594
+ }
595
+ ` , "tsconfig.json" ,
596
+ {
597
+ compilerOptions : {
598
+ target : undefined ,
599
+ module : ModuleKind . ESNext ,
600
+ types : [ ]
601
+ } ,
602
+ hasParseErrors : true
603
+ }
604
+ ) ;
605
+ } ) ;
542
606
} ) ;
543
607
}
0 commit comments