@@ -3117,65 +3117,54 @@ private RubyNode addNewlineIfNeeded(ParseNode jrubyNode, RubyNode node) {
3117
3117
3118
3118
private static ArgumentsDescriptor getKeywordArgumentsDescriptor (RubyLanguage language , ParseNode [] arguments ) {
3119
3119
// A simple empty set of arguments is always an empty descriptor
3120
-
3121
3120
if (arguments .length == 0 ) {
3122
3121
return EmptyArgumentsDescriptor .INSTANCE ;
3123
3122
}
3124
3123
3125
3124
// Find the keyword argument hash parse node
3125
+ var lastArgument = arguments [arguments .length - 1 ];
3126
+ final HashParseNode keywordHashArgumentNode = findLastHashParseNode (lastArgument );
3126
3127
3127
- int keywordHashArgumentIndex = 0 ;
3128
- HashParseNode keywordHashArgumentNode = null ;
3129
-
3130
- if (arguments [0 ] instanceof ArgsPushParseNode ) {
3131
- final ArgsPushParseNode argsPushParseNode = (ArgsPushParseNode ) arguments [0 ];
3132
-
3133
- if (argsPushParseNode .getFirstNode () instanceof HashParseNode ) {
3134
- keywordHashArgumentNode = (HashParseNode ) argsPushParseNode .getFirstNode ();
3135
- } else if (argsPushParseNode .getSecondNode () instanceof HashParseNode ) {
3136
- keywordHashArgumentNode = (HashParseNode ) argsPushParseNode .getSecondNode ();
3137
- }
3138
- } else {
3139
- if (arguments [arguments .length - 1 ] instanceof HashParseNode ) {
3140
- keywordHashArgumentIndex = arguments .length - 1 ;
3141
- keywordHashArgumentNode = (HashParseNode ) arguments [keywordHashArgumentIndex ];
3142
- }
3143
- }
3144
-
3145
- // If there's no hash parse node of any kind, then there are no keyword arguments
3146
-
3147
- if (keywordHashArgumentNode == null ) {
3128
+ if (keywordHashArgumentNode == null || !keywordHashArgumentNode .isKeywordArguments ()) {
3148
3129
return EmptyArgumentsDescriptor .INSTANCE ;
3149
3130
}
3150
3131
3151
3132
final List <String > keywords = new ArrayList <>();
3152
3133
boolean alsoSplat = false ;
3153
3134
3154
3135
for (ParseNodeTuple pair : keywordHashArgumentNode .getPairs ()) {
3155
- if (pair instanceof ParseNodeTuple ) {
3156
- final ParseNode key = pair .getKey ();
3157
- final ParseNode value = pair .getValue ();
3158
-
3159
- if (key instanceof SymbolParseNode &&
3160
- ((SymbolParseNode ) key ).getName () != null ) {
3161
- if (keywordHashArgumentNode .isKeywordArguments ()) {
3162
- keywords .add (((SymbolParseNode ) key ).getName ());
3163
- }
3164
- } else if (key == null && value != null ) {
3165
- // A splat keyword hash
3166
- alsoSplat = true ;
3167
- } else {
3168
- // For non-symbol keys
3169
- alsoSplat = true ;
3170
- }
3136
+ final ParseNode key = pair .getKey ();
3137
+ final ParseNode value = pair .getValue ();
3138
+
3139
+ if (key instanceof SymbolParseNode &&
3140
+ ((SymbolParseNode ) key ).getName () != null ) {
3141
+ keywords .add (((SymbolParseNode ) key ).getName ());
3142
+ } else if (key == null && value != null ) {
3143
+ // A splat keyword hash
3144
+ alsoSplat = true ;
3145
+ } else {
3146
+ // For non-symbol keys
3147
+ alsoSplat = true ;
3171
3148
}
3172
3149
}
3173
3150
3174
- if (keywords .isEmpty () && !alsoSplat ) {
3151
+ if (!keywords .isEmpty () || alsoSplat ) {
3152
+ return KeywordArgumentsDescriptor .INSTANCE ;
3153
+ } else {
3175
3154
return EmptyArgumentsDescriptor .INSTANCE ;
3176
3155
}
3156
+ }
3177
3157
3178
- return KeywordArgumentsDescriptor .INSTANCE ;
3158
+ private static HashParseNode findLastHashParseNode (ParseNode node ) {
3159
+ if (node instanceof HashParseNode ) {
3160
+ return (HashParseNode ) node ;
3161
+ } else if (node instanceof ArgsPushParseNode ) {
3162
+ return findLastHashParseNode (((ArgsPushParseNode ) node ).getSecondNode ());
3163
+ } else {
3164
+ // SplatParseNode: cannot contain kwargs (*array)
3165
+ // ArgsCatParseNode: cannot contain kwargs (RHS is *array)
3166
+ return null ;
3167
+ }
3179
3168
}
3180
3169
3181
3170
@ Override
0 commit comments