@@ -9,10 +9,17 @@ import camelcase = require('camelcase');
9
9
import chalk = require( 'chalk' ) ;
10
10
import type { Options } from 'yargs' ;
11
11
import type { Config } from '@jest/types' ;
12
- import defaultConfig from './defaultConfig' ;
13
- import { deprecationWarning } from './deprecated' ;
14
- import type { DeprecatedOptionFunc , DeprecatedOptions } from './types' ;
15
- import { ValidationError , createDidYouMeanMessage , format } from './utils' ;
12
+ import type {
13
+ DeprecatedOptionFunc ,
14
+ DeprecatedOptions ,
15
+ DeprecationItem ,
16
+ } from './types' ;
17
+ import {
18
+ ValidationError ,
19
+ createDidYouMeanMessage ,
20
+ format ,
21
+ logValidationWarning ,
22
+ } from './utils' ;
16
23
17
24
const BULLET : string = chalk . bold ( '\u25cf' ) ;
18
25
export const DOCUMENTATION_NOTE = ` ${ chalk . bold ( 'CLI Options Documentation:' ) }
@@ -48,16 +55,21 @@ const createCLIValidationError = (
48
55
return new ValidationError ( title , message , comment ) ;
49
56
} ;
50
57
51
- const logDeprecatedOptions = (
52
- deprecatedOptions : Array < string > ,
58
+ const validateDeprecatedOptions = (
59
+ deprecatedOptions : Array < DeprecationItem > ,
53
60
deprecationEntries : DeprecatedOptions ,
54
61
argv : Config . Argv ,
55
62
) => {
56
63
deprecatedOptions . forEach ( opt => {
57
- deprecationWarning ( argv , opt , deprecationEntries , {
58
- ...defaultConfig ,
59
- comment : DOCUMENTATION_NOTE ,
60
- } ) ;
64
+ const name = opt . name ;
65
+ const message = deprecationEntries [ name ] ( argv ) ;
66
+ const comment = DOCUMENTATION_NOTE ;
67
+
68
+ if ( opt . fatal ) {
69
+ throw new ValidationError ( name , message , comment ) ;
70
+ } else {
71
+ logValidationWarning ( name , message , comment ) ;
72
+ }
61
73
} ) ;
62
74
} ;
63
75
@@ -69,29 +81,19 @@ export default function validateCLIOptions(
69
81
rawArgv : Array < string > = [ ] ,
70
82
) : boolean {
71
83
const yargsSpecialOptions = [ '$0' , '_' , 'help' , 'h' ] ;
72
- const deprecationEntries = options . deprecationEntries ?? { } ;
84
+
73
85
const allowedOptions = Object . keys ( options ) . reduce (
74
86
( acc , option ) =>
75
87
acc . add ( option ) . add ( ( options [ option ] . alias as string ) || option ) ,
76
88
new Set ( yargsSpecialOptions ) ,
77
89
) ;
78
- const unrecognizedOptions = Object . keys ( argv ) . filter (
79
- arg =>
80
- ! allowedOptions . has ( camelcase ( arg , { locale : 'en-US' } ) ) &&
81
- ! allowedOptions . has ( arg ) &&
82
- ( ! rawArgv . length || rawArgv . includes ( arg ) ) ,
83
- [ ] ,
84
- ) ;
85
-
86
- if ( unrecognizedOptions . length ) {
87
- throw createCLIValidationError ( unrecognizedOptions , allowedOptions ) ;
88
- }
89
90
91
+ const deprecationEntries = options . deprecationEntries ?? { } ;
90
92
const CLIDeprecations = Object . keys ( deprecationEntries ) . reduce <
91
93
Record < string , DeprecatedOptionFunc >
92
94
> ( ( acc , entry ) => {
95
+ acc [ entry ] = deprecationEntries [ entry ] ;
93
96
if ( options [ entry ] ) {
94
- acc [ entry ] = deprecationEntries [ entry ] ;
95
97
const alias = options [ entry ] . alias as string ;
96
98
if ( alias ) {
97
99
acc [ alias ] = deprecationEntries [ entry ] ;
@@ -100,12 +102,24 @@ export default function validateCLIOptions(
100
102
return acc ;
101
103
} , { } ) ;
102
104
const deprecations = new Set ( Object . keys ( CLIDeprecations ) ) ;
103
- const deprecatedOptions = Object . keys ( argv ) . filter (
104
- arg => deprecations . has ( arg ) && argv [ arg ] != null ,
105
- ) ;
105
+ const deprecatedOptions = Object . keys ( argv )
106
+ . filter ( arg => deprecations . has ( arg ) && argv [ arg ] != null )
107
+ . map ( arg => ( { fatal : ! allowedOptions . has ( arg ) , name : arg } ) ) ;
106
108
107
109
if ( deprecatedOptions . length ) {
108
- logDeprecatedOptions ( deprecatedOptions , CLIDeprecations , argv ) ;
110
+ validateDeprecatedOptions ( deprecatedOptions , CLIDeprecations , argv ) ;
111
+ }
112
+
113
+ const unrecognizedOptions = Object . keys ( argv ) . filter (
114
+ arg =>
115
+ ! allowedOptions . has ( camelcase ( arg , { locale : 'en-US' } ) ) &&
116
+ ! allowedOptions . has ( arg ) &&
117
+ ( ! rawArgv . length || rawArgv . includes ( arg ) ) ,
118
+ [ ] ,
119
+ ) ;
120
+
121
+ if ( unrecognizedOptions . length ) {
122
+ throw createCLIValidationError ( unrecognizedOptions , allowedOptions ) ;
109
123
}
110
124
111
125
return true ;
0 commit comments