1
1
namespace ts {
2
2
describe ( "convertCompilerOptionsFromJson" , ( ) => {
3
+ const formatDiagnosticHost : FormatDiagnosticsHost = {
4
+ getCurrentDirectory : ( ) => "/apath/" ,
5
+ getCanonicalFileName : createGetCanonicalFileName ( /*useCaseSensitiveFileNames*/ true ) ,
6
+ getNewLine : ( ) => "\n"
7
+ } ;
3
8
function assertCompilerOptions ( json : any , configFileName : string , expectedResult : { compilerOptions : CompilerOptions , errors : Diagnostic [ ] } ) {
4
9
assertCompilerOptionsWithJson ( json , configFileName , expectedResult ) ;
5
10
assertCompilerOptionsWithJsonNode ( json , configFileName , expectedResult ) ;
@@ -24,9 +29,11 @@ namespace ts {
24
29
}
25
30
26
31
function assertCompilerOptionsWithJsonNode ( json : any , configFileName : string , expectedResult : { compilerOptions : CompilerOptions , errors : Diagnostic [ ] } ) {
27
- const fileText = JSON . stringify ( json ) ;
32
+ assertCompilerOptionsWithJsonText ( JSON . stringify ( json ) , configFileName , expectedResult ) ;
33
+ }
34
+
35
+ function assertCompilerOptionsWithJsonText ( fileText : string , configFileName : string , expectedResult : { compilerOptions : CompilerOptions , errors : ( Diagnostic | string ) [ ] } ) {
28
36
const result = parseJsonText ( configFileName , fileText ) ;
29
- assert ( ! result . parseDiagnostics . length ) ;
30
37
assert ( ! ! result . endOfFileToken ) ;
31
38
const host : ParseConfigHost = new fakes . ParseConfigHost ( new vfs . FileSystem ( /*ignoreCase*/ false , { cwd : "/apath/" } ) ) ;
32
39
const { options : actualCompilerOptions , errors : actualParseErrors } = parseJsonSourceFileConfigFileContent ( result , host , "/apath/" , /*existingOptions*/ undefined , configFileName ) ;
@@ -37,20 +44,29 @@ namespace ts {
37
44
assert . equal ( parsedCompilerOptions , expectedCompilerOptions ) ;
38
45
assert . equal ( actualCompilerOptions . configFile , result ) ;
39
46
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 ) ;
47
+ const actualErrors = ( result . parseDiagnostics as Diagnostic [ ] ) . concat ( 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 ) ) ;
41
48
const expectedErrors = expectedResult . errors ;
42
- assert . isTrue ( expectedResult . errors . length === actualErrors . length , `Expected error: ${ JSON . stringify ( expectedResult . errors ) } . Actual error: ${ JSON . stringify ( actualErrors ) } .` ) ;
49
+ assert . isTrue ( expectedErrors . length === actualErrors . length , `Expected error: ${ JSON . stringify ( expectedErrors . map ( e => isString ( e ) ? e : getDiagnosticString ( e ) ) , undefined , " " ) } . Actual error: ${ JSON . stringify ( actualErrors . map ( getDiagnosticString ) , undefined , " " ) } .` ) ;
43
50
for ( let i = 0 ; i < actualErrors . length ; i ++ ) {
44
51
const actualError = actualErrors [ i ] ;
45
52
const expectedError = expectedErrors [ i ] ;
46
- assert . equal ( actualError . code , expectedError . code , `Expected error-code: ${ JSON . stringify ( expectedError . code ) } . Actual error-code: ${ JSON . stringify ( actualError . code ) } .` ) ;
47
- 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 ) ;
53
+ if ( isString ( expectedError ) ) {
54
+ assert . equal ( getDiagnosticString ( actualError ) , expectedError ) ;
55
+ }
56
+ else {
57
+ assert . equal ( actualError . code , expectedError . code , `Expected error-code: ${ JSON . stringify ( expectedError . code ) } . Actual error-code: ${ JSON . stringify ( actualError . code ) } .` ) ;
58
+ assert . equal ( actualError . category , expectedError . category , `Expected error-category: ${ JSON . stringify ( expectedError . category ) } . Actual error-category: ${ JSON . stringify ( actualError . category ) } .` ) ;
59
+ assert ( actualError . file ) ;
60
+ assert ( actualError . start ) ;
61
+ assert ( actualError . length ) ;
62
+ }
51
63
}
52
64
}
53
65
66
+ function getDiagnosticString ( diagnostic : Diagnostic ) {
67
+ return formatDiagnostic ( diagnostic , formatDiagnosticHost ) ;
68
+ }
69
+
54
70
// tsconfig.json tests
55
71
it ( "Convert correctly format tsconfig.json to compiler-options " , ( ) => {
56
72
assertCompilerOptions (
@@ -539,5 +555,99 @@ namespace ts {
539
555
}
540
556
) ;
541
557
} ) ;
558
+
559
+ it ( "Convert tsconfig options when there are multiple invalid strings" , ( ) => {
560
+ assertCompilerOptionsWithJsonText ( `{
561
+ "compilerOptions": {
562
+ "target": "<%- options.useTsWithBabel ? 'esnext' : 'es5' %>",
563
+ "module": "esnext",
564
+ <%_ if (options.classComponent) { _%>
565
+ "experimentalDecorators": true,
566
+ <%_ } _%>
567
+ "sourceMap": true,
568
+ "types": [
569
+ "webpack-env"<% if (hasMocha || hasJest) { %>,<% } %>
570
+ <%_ if (hasMocha) { _%>
571
+ "mocha",
572
+ "chai"
573
+ <%_ } else if (hasJest) { _%>
574
+ "jest"
575
+ <%_ } _%>
576
+ ]
577
+ }
578
+ }
579
+ ` , "tsconfig.json" ,
580
+ {
581
+ compilerOptions : {
582
+ target : undefined ,
583
+ module : ModuleKind . ESNext ,
584
+ types : [ ]
585
+ } ,
586
+ errors : [
587
+ "tsconfig.json(5,5): error TS1136: Property assignment expected.\n" ,
588
+ "tsconfig.json(5,6): error TS1136: Property assignment expected.\n" ,
589
+ "tsconfig.json(5,9): error TS1005: ':' expected.\n" ,
590
+ "tsconfig.json(5,20): error TS1005: ',' expected.\n" ,
591
+ "tsconfig.json(5,41): error TS1109: Expression expected.\n" ,
592
+ "tsconfig.json(6,29): error TS1005: ';' expected.\n" ,
593
+ "tsconfig.json(7,5): error TS1109: Expression expected.\n" ,
594
+ "tsconfig.json(7,6): error TS1109: Expression expected.\n" ,
595
+ "tsconfig.json(7,11): error TS1005: ',' expected.\n" ,
596
+ "tsconfig.json(7,12): error TS1005: ':' expected.\n" ,
597
+ "tsconfig.json(7,13): error TS1109: Expression expected.\n" ,
598
+ "tsconfig.json(8,16): error TS1005: ',' expected.\n" ,
599
+ "tsconfig.json(8,22): error TS1005: ':' expected.\n" ,
600
+ "tsconfig.json(10,21): error TS1109: Expression expected.\n" ,
601
+ "tsconfig.json(10,23): error TS1109: Expression expected.\n" ,
602
+ "tsconfig.json(10,36): error TS1005: ',' expected.\n" ,
603
+ "tsconfig.json(10,50): error TS1109: Expression expected.\n" ,
604
+ "tsconfig.json(10,51): error TS1109: Expression expected.\n" ,
605
+ "tsconfig.json(10,52): error TS1109: Expression expected.\n" ,
606
+ "tsconfig.json(10,53): error TS1109: Expression expected.\n" ,
607
+ "tsconfig.json(10,54): error TS1109: Expression expected.\n" ,
608
+ "tsconfig.json(10,56): error TS1109: Expression expected.\n" ,
609
+ "tsconfig.json(10,58): error TS1005: ',' expected.\n" ,
610
+ "tsconfig.json(10,59): error TS1136: Property assignment expected.\n" ,
611
+ "tsconfig.json(11,7): error TS1136: Property assignment expected.\n" ,
612
+ "tsconfig.json(11,8): error TS1136: Property assignment expected.\n" ,
613
+ "tsconfig.json(11,11): error TS1005: ':' expected.\n" ,
614
+ "tsconfig.json(11,29): error TS1109: Expression expected.\n" ,
615
+ "tsconfig.json(14,8): error TS1109: Expression expected.\n" ,
616
+ "tsconfig.json(14,13): error TS1005: ',' expected.\n" ,
617
+ "tsconfig.json(14,18): error TS1005: ':' expected.\n" ,
618
+ "tsconfig.json(14,35): error TS1109: Expression expected.\n" ,
619
+ "tsconfig.json(16,8): error TS1109: Expression expected.\n" ,
620
+ "tsconfig.json(16,13): error TS1005: ',' expected.\n" ,
621
+ "tsconfig.json(16,14): error TS1005: ':' expected.\n" ,
622
+ "tsconfig.json(16,15): error TS1109: Expression expected.\n" ,
623
+ "tsconfig.json(17,5): error TS1109: Expression expected.\n" ,
624
+ "tsconfig.json(3,15): error TS6046: Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'esnext'.\n" ,
625
+ "tsconfig.json(5,7): error TS1327: String literal with double quotes expected.\n" ,
626
+ "tsconfig.json(5,7): error TS5023: Unknown compiler option '_'.\n" ,
627
+ "tsconfig.json(5,8): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n" ,
628
+ "tsconfig.json(5,9): error TS1136: Property assignment expected.\n" ,
629
+ "tsconfig.json(7,11): error TS1327: String literal with double quotes expected.\n" ,
630
+ "tsconfig.json(7,11): error TS5023: Unknown compiler option '_'.\n" ,
631
+ "tsconfig.json(7,12): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n" ,
632
+ "tsconfig.json(8,18): error TS1327: String literal with double quotes expected.\n" ,
633
+ "tsconfig.json(8,18): error TS5023: Unknown compiler option 'true'.\n" ,
634
+ "tsconfig.json(8,22): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n" ,
635
+ "tsconfig.json(10,7): error TS5024: Compiler option 'types' requires a value of type string.\n" ,
636
+ "tsconfig.json(10,23): error TS1136: Property assignment expected.\n" ,
637
+ "tsconfig.json(11,9): error TS1327: String literal with double quotes expected.\n" ,
638
+ "tsconfig.json(11,9): error TS5023: Unknown compiler option '_'.\n" ,
639
+ "tsconfig.json(11,10): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n" ,
640
+ "tsconfig.json(11,11): error TS1136: Property assignment expected.\n" ,
641
+ "tsconfig.json(14,13): error TS1327: String literal with double quotes expected.\n" ,
642
+ "tsconfig.json(14,13): error TS5023: Unknown compiler option 'else'.\n" ,
643
+ "tsconfig.json(14,17): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n" ,
644
+ "tsconfig.json(14,18): error TS1136: Property assignment expected.\n" ,
645
+ "tsconfig.json(16,13): error TS1327: String literal with double quotes expected.\n" ,
646
+ "tsconfig.json(16,13): error TS5023: Unknown compiler option '_'.\n" ,
647
+ "tsconfig.json(16,14): error TS1328: Property value can only be string literal, numeric literal, 'true', 'false', 'null', object literal or array literal.\n"
648
+ ]
649
+ }
650
+ ) ;
651
+ } ) ;
542
652
} ) ;
543
653
}
0 commit comments