@@ -9,10 +9,17 @@ import camelcase = require('camelcase');
99import chalk = require( 'chalk' ) ;
1010import type { Options } from 'yargs' ;
1111import 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' ;
1623
1724const BULLET : string = chalk . bold ( '\u25cf' ) ;
1825export const DOCUMENTATION_NOTE = ` ${ chalk . bold ( 'CLI Options Documentation:' ) }
@@ -48,16 +55,21 @@ const createCLIValidationError = (
4855 return new ValidationError ( title , message , comment ) ;
4956} ;
5057
51- const logDeprecatedOptions = (
52- deprecatedOptions : Array < string > ,
58+ const validateDeprecatedOptions = (
59+ deprecatedOptions : Array < DeprecationItem > ,
5360 deprecationEntries : DeprecatedOptions ,
5461 argv : Config . Argv ,
5562) => {
5663 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+ }
6173 } ) ;
6274} ;
6375
@@ -69,29 +81,19 @@ export default function validateCLIOptions(
6981 rawArgv : Array < string > = [ ] ,
7082) : boolean {
7183 const yargsSpecialOptions = [ '$0' , '_' , 'help' , 'h' ] ;
72- const deprecationEntries = options . deprecationEntries ?? { } ;
84+
7385 const allowedOptions = Object . keys ( options ) . reduce (
7486 ( acc , option ) =>
7587 acc . add ( option ) . add ( ( options [ option ] . alias as string ) || option ) ,
7688 new Set ( yargsSpecialOptions ) ,
7789 ) ;
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- }
8990
91+ const deprecationEntries = options . deprecationEntries ?? { } ;
9092 const CLIDeprecations = Object . keys ( deprecationEntries ) . reduce <
9193 Record < string , DeprecatedOptionFunc >
9294 > ( ( acc , entry ) => {
95+ acc [ entry ] = deprecationEntries [ entry ] ;
9396 if ( options [ entry ] ) {
94- acc [ entry ] = deprecationEntries [ entry ] ;
9597 const alias = options [ entry ] . alias as string ;
9698 if ( alias ) {
9799 acc [ alias ] = deprecationEntries [ entry ] ;
@@ -100,12 +102,24 @@ export default function validateCLIOptions(
100102 return acc ;
101103 } , { } ) ;
102104 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 } ) ) ;
106108
107109 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 ) ;
109123 }
110124
111125 return true ;
0 commit comments