@@ -1059,23 +1059,42 @@ for (const reverse of [false, true]) {
10591059 }
10601060}
10611061
1062- for ( const nonEnumerable of [ 'logfile' , 'no-logfile' ] ) {
1063- for ( const args of [ [ ] , [ '--no-logfile' ] ] ) {
1064- test ( `strict mode detects a non-enumerable conflict: ${ nonEnumerable } ${ args . join ( ' ' ) } ` , ( ) => {
1065- const options = {
1066- 'logfile' : { type : 'string' } ,
1067- 'no-logfile' : { type : 'boolean' } ,
1068- } ;
1069- Object . defineProperty ( options , nonEnumerable , { enumerable : false } ) ;
1070- assert . throws ( ( ) => {
1071- parseArgs ( { args, options, allowNegative : true } ) ;
1072- } , {
1073- code : 'ERR_INVALID_ARG_VALUE'
1074- } ) ;
1062+ for ( const args of [ [ ] , [ '--no-logfile' ] ] ) {
1063+ test ( `strict mode detects a conflict with a non-enumerable target: ${ args . join ( ' ' ) } ` , ( ) => {
1064+ const options = {
1065+ 'logfile' : { type : 'string' } ,
1066+ 'no-logfile' : { type : 'boolean' } ,
1067+ } ;
1068+ Object . defineProperty ( options , 'logfile' , { enumerable : false } ) ;
1069+ assert . throws ( ( ) => {
1070+ parseArgs ( { args, options, allowNegative : true } ) ;
1071+ } , {
1072+ code : 'ERR_INVALID_ARG_VALUE'
10751073 } ) ;
1076- }
1074+ } ) ;
10771075}
10781076
1077+ test ( 'strict mode accepts an unused non-enumerable conflicting negative option' , ( ) => {
1078+ const options = {
1079+ 'logfile' : { type : 'string' } ,
1080+ 'no-logfile' : { type : 'boolean' } ,
1081+ } ;
1082+ Object . defineProperty ( options , 'no-logfile' , { enumerable : false } ) ;
1083+ const expected = { values : { __proto__ : null } , positionals : [ ] } ;
1084+ assert . deepStrictEqual ( parseArgs ( { args : [ ] , options, allowNegative : true } ) , expected ) ;
1085+ } ) ;
1086+
1087+ test ( 'strict mode rejects a used non-enumerable conflicting negative option' , ( ) => {
1088+ const options = {
1089+ 'logfile' : { type : 'string' } ,
1090+ 'no-logfile' : { type : 'boolean' } ,
1091+ } ;
1092+ Object . defineProperty ( options , 'no-logfile' , { enumerable : false } ) ;
1093+ assert . throws ( ( ) => {
1094+ parseArgs ( { args : [ '--no-logfile' ] , options, allowNegative : true } ) ;
1095+ } , { code : 'ERR_PARSE_ARGS_UNKNOWN_OPTION' } ) ;
1096+ } ) ;
1097+
10791098for ( const [ description , prototype ] of [
10801099 [ 'absent' , null ] ,
10811100 [ 'inherited' , { foo : { type : 'string' } } ] ,
@@ -1102,18 +1121,75 @@ for (const [description, option, code, message] of [
11021121 [ 'short' , { type : 'boolean' , short : 'xy' } , 'ERR_INVALID_ARG_VALUE' , / o p t i o n s \. i n v a l i d \. s h o r t / ] ,
11031122 [ 'default' , { type : 'boolean' , default : 'invalid' } , 'ERR_INVALID_ARG_TYPE' , / o p t i o n s \. i n v a l i d \. d e f a u l t / ] ,
11041123] ) {
1105- test ( `invalid ${ description } is reported before a configuration conflict` , ( ) => {
1124+ test ( `invalid ${ description } is reported before a later configuration conflict` , ( ) => {
11061125 const options = {
1126+ 'invalid' : option ,
11071127 'logfile' : { type : 'string' } ,
11081128 'no-logfile' : { type : 'boolean' } ,
1109- 'invalid' : option ,
11101129 } ;
11111130 assert . throws ( ( ) => {
11121131 parseArgs ( { args : [ ] , options, allowNegative : true } ) ;
11131132 } , { code, message } ) ;
11141133 } ) ;
11151134}
11161135
1136+ test ( 'configuration conflict is reported before a later invalid option' , ( ) => {
1137+ const options = {
1138+ 'logfile' : { type : 'string' } ,
1139+ 'no-logfile' : { type : 'boolean' } ,
1140+ 'invalid' : null ,
1141+ } ;
1142+ assert . throws ( ( ) => {
1143+ parseArgs ( { args : [ ] , options, allowNegative : true } ) ;
1144+ } , { code : 'ERR_INVALID_ARG_VALUE' , message : / o p t i o n s \. n o - l o g f i l e .* c o n f l i c t s w i t h s t r i n g o p t i o n l o g f i l e / } ) ;
1145+ } ) ;
1146+
1147+ for ( const [ description , target ] of [
1148+ [ 'null' , null ] ,
1149+ [ 'undefined' , undefined ] ,
1150+ [ 'array' , Object . assign ( [ ] , { type : 'string' } ) ] ,
1151+ ] ) {
1152+ for ( const reverse of [ false , true ] ) {
1153+ test ( `invalid ${ description } target is reported in either order: ${ reverse } ` , ( ) => {
1154+ const entries = [
1155+ [ 'alpha' , target ] ,
1156+ [ 'no-alpha' , { type : 'boolean' } ] ,
1157+ ] ;
1158+ const options = Object . fromEntries ( reverse ? entries . reverse ( ) : entries ) ;
1159+ assert . throws ( ( ) => {
1160+ parseArgs ( { args : [ ] , options, allowNegative : true } ) ;
1161+ } , { code : 'ERR_INVALID_ARG_TYPE' , message : / o p t i o n s \. a l p h a / } ) ;
1162+ } ) ;
1163+ }
1164+ }
1165+
1166+ test ( 'a negative option is validated before checking its configuration conflict' , ( ) => {
1167+ const options = {
1168+ 'no-alpha' : { type : 'boolean' , default : 'invalid' } ,
1169+ 'alpha' : { type : 'string' } ,
1170+ } ;
1171+ assert . throws ( ( ) => {
1172+ parseArgs ( { args : [ ] , options, allowNegative : true } ) ;
1173+ } , { code : 'ERR_INVALID_ARG_TYPE' , message : / o p t i o n s \. n o - a l p h a \. d e f a u l t / } ) ;
1174+ } ) ;
1175+
1176+ test ( 'negative option type getter is read once during configuration validation' , ( ) => {
1177+ let reads = 0 ;
1178+ const options = {
1179+ 'alpha' : { type : 'boolean' } ,
1180+ 'no-alpha' : {
1181+ get type ( ) {
1182+ reads ++ ;
1183+ if ( reads > 1 ) throw new Error ( 'negative type was read again' ) ;
1184+ return 'boolean' ;
1185+ } ,
1186+ } ,
1187+ } ;
1188+ const expected = { values : { __proto__ : null } , positionals : [ ] } ;
1189+ assert . deepStrictEqual ( parseArgs ( { args : [ ] , options, allowNegative : true } ) , expected ) ;
1190+ assert . strictEqual ( reads , 1 ) ;
1191+ } ) ;
1192+
11171193test ( 'strict mode accepts an explicit negative boolean without a target when unused' , ( ) => {
11181194 const options = { 'no-alpha' : { type : 'boolean' , short : 'A' } } ;
11191195 const expected = { values : { __proto__ : null } , positionals : [ ] } ;
0 commit comments