@@ -5,36 +5,46 @@ namespace ts {
5
5
getCanonicalFileName : createGetCanonicalFileName ( /*useCaseSensitiveFileNames*/ true ) ,
6
6
getNewLine : ( ) => "\n"
7
7
} ;
8
- function assertCompilerOptions ( json : any , configFileName : string , expectedResult : { compilerOptions : CompilerOptions , errors : Diagnostic [ ] } ) {
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 ) {
9
26
assertCompilerOptionsWithJson ( json , configFileName , expectedResult ) ;
10
27
assertCompilerOptionsWithJsonNode ( json , configFileName , expectedResult ) ;
11
28
}
12
29
13
- function assertCompilerOptionsWithJson ( json : any , configFileName : string , expectedResult : { compilerOptions : CompilerOptions , errors : Diagnostic [ ] } ) {
14
- 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 ) ;
15
32
16
33
const parsedCompilerOptions = JSON . stringify ( actualCompilerOptions ) ;
17
34
const expectedCompilerOptions = JSON . stringify ( { ...expectedResult . compilerOptions , configFilePath : configFileName } ) ;
18
35
assert . equal ( parsedCompilerOptions , expectedCompilerOptions ) ;
19
36
20
- const expectedErrors = expectedResult . errors ;
21
- assert . isTrue ( expectedResult . errors . length === actualErrors . length , `Expected error: ${ JSON . stringify ( expectedResult . errors ) } . Actual error: ${ JSON . stringify ( actualErrors ) } .` ) ;
22
- for ( let i = 0 ; i < actualErrors . length ; i ++ ) {
23
- const actualError = actualErrors [ i ] ;
24
- const expectedError = expectedErrors [ i ] ;
25
- assert . equal ( actualError . code , expectedError . code ) ;
26
- assert . equal ( actualError . category , expectedError . category ) ;
27
- assert . equal ( actualError . messageText , expectedError . messageText ) ;
28
- }
37
+ verifyErrors ( actualErrors , expectedResult . errors , /*ignoreLocation*/ true ) ;
29
38
}
30
39
31
- function assertCompilerOptionsWithJsonNode ( json : any , configFileName : string , expectedResult : { compilerOptions : CompilerOptions , errors : Diagnostic [ ] } ) {
40
+ function assertCompilerOptionsWithJsonNode ( json : any , configFileName : string , expectedResult : ExpectedResultWithParsingSuccess ) {
32
41
assertCompilerOptionsWithJsonText ( JSON . stringify ( json ) , configFileName , expectedResult ) ;
33
42
}
34
43
35
- function assertCompilerOptionsWithJsonText ( fileText : string , configFileName : string , expectedResult : { compilerOptions : CompilerOptions , errors : ( Diagnostic | string ) [ ] } ) {
44
+ function assertCompilerOptionsWithJsonText ( fileText : string , configFileName : string , expectedResult : ExpectedResult ) {
36
45
const result = parseJsonText ( configFileName , fileText ) ;
37
46
assert ( ! ! result . endOfFileToken ) ;
47
+ assert . equal ( ! ! result . parseDiagnostics . length , isExpectedResultWithParsingFailure ( expectedResult ) ) ;
38
48
const host : ParseConfigHost = new fakes . ParseConfigHost ( new vfs . FileSystem ( /*ignoreCase*/ false , { cwd : "/apath/" } ) ) ;
39
49
const { options : actualCompilerOptions , errors : actualParseErrors } = parseJsonSourceFileConfigFileContent ( result , host , "/apath/" , /*existingOptions*/ undefined , configFileName ) ;
40
50
expectedResult . compilerOptions . configFilePath = configFileName ;
@@ -44,27 +54,33 @@ namespace ts {
44
54
assert . equal ( parsedCompilerOptions , expectedCompilerOptions ) ;
45
55
assert . equal ( actualCompilerOptions . configFile , result ) ;
46
56
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 ) ) ;
48
- const expectedErrors = expectedResult . errors ;
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 , " " ) } .` ) ;
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 , " " ) } .` ) ;
50
64
for ( let i = 0 ; i < actualErrors . length ; i ++ ) {
51
65
const actualError = actualErrors [ i ] ;
52
66
const expectedError = expectedErrors [ i ] ;
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 ) } .` ) ;
67
+
68
+ assert . equal ( actualError . code , expectedError . code , `Expected error-code: ${ JSON . stringify ( expectedError . code ) } . Actual error-code: ${ JSON . stringify ( actualError . code ) } .` ) ;
69
+ assert . equal ( actualError . category , expectedError . category , `Expected error-category: ${ JSON . stringify ( expectedError . category ) } . Actual error-category: ${ JSON . stringify ( actualError . category ) } .` ) ;
70
+ if ( ! ignoreLocation ) {
59
71
assert ( actualError . file ) ;
60
72
assert ( actualError . start ) ;
61
73
assert ( actualError . length ) ;
62
74
}
63
75
}
64
- }
65
76
66
- function getDiagnosticString ( diagnostic : Diagnostic ) {
67
- return formatDiagnostic ( diagnostic , formatDiagnosticHost ) ;
77
+ function getDiagnosticString ( diagnostic : Diagnostic ) {
78
+ if ( ignoreLocation ) {
79
+ const { file, ...rest } = diagnostic ;
80
+ diagnostic = { file : undefined , ...rest } ;
81
+ }
82
+ return formatDiagnostic ( diagnostic , formatDiagnosticHost ) ;
83
+ }
68
84
}
69
85
70
86
// tsconfig.json tests
@@ -583,69 +599,7 @@ namespace ts {
583
599
module : ModuleKind . ESNext ,
584
600
types : [ ]
585
601
} ,
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
- ]
602
+ hasParseErrors : true
649
603
}
650
604
) ;
651
605
} ) ;
0 commit comments