@@ -74,18 +74,13 @@ export class UnexpectedCallFound {
74
74
) { }
75
75
}
76
76
77
- export class ShouldDefineTopLevelConstant {
78
- constructor ( public readonly name : string , public readonly value : string ) { }
79
- }
80
-
81
77
type Issue =
82
78
| undefined
83
79
| MissingExpectedCall
84
80
| HelperNotOptimal
85
81
| MethodNotFound
86
82
| HelperCallNotFound
87
83
| UnexpectedCallFound
88
- | ShouldDefineTopLevelConstant
89
84
90
85
class Constant {
91
86
public readonly name : string
@@ -486,6 +481,18 @@ class Entry {
486
481
return parameterName ( this . params [ 0 ] )
487
482
}
488
483
484
+ public get nameOfConstantDefinedInBody ( ) : string | null {
485
+ const localConstants = extractVariables ( this . body ) . filter (
486
+ ( constant ) =>
487
+ constant . init ?. type === AST_NODE_TYPES . ArrayExpression ||
488
+ constant . init ?. type === AST_NODE_TYPES . ObjectExpression
489
+ )
490
+ if ( localConstants . length ) {
491
+ return localConstants [ 0 ] . name || 'COLORS'
492
+ }
493
+ return null
494
+ }
495
+
489
496
public isOptimal (
490
497
constant : Readonly < Constant > | undefined ,
491
498
program : Program
@@ -514,13 +521,9 @@ class Entry {
514
521
}
515
522
}
516
523
517
- if ( ! constant ) {
518
- const issue = this . hasConstantDefinedInBody ( )
519
- if ( issue instanceof ShouldDefineTopLevelConstant ) {
520
- logger . log ( '~> found a constant that was not declared at the top level' )
521
- this . lastIssue_ = issue
522
- return false
523
- }
524
+ if ( ! constant && ! ! this . nameOfConstantDefinedInBody ) {
525
+ logger . log ( '~> found a constant that was not declared at the top level' )
526
+ return false
524
527
}
525
528
526
529
if ( this . hasOneMap ) {
@@ -1126,18 +1129,6 @@ class Entry {
1126
1129
logger . log ( `~> constant is not optimal` )
1127
1130
return false
1128
1131
}
1129
-
1130
- private hasConstantDefinedInBody ( ) : ShouldDefineTopLevelConstant | undefined {
1131
- const localConstants = extractVariables ( this . body ) . filter (
1132
- ( constant ) =>
1133
- constant . init ?. type === AST_NODE_TYPES . ArrayExpression ||
1134
- constant . init ?. type === AST_NODE_TYPES . ObjectExpression
1135
- )
1136
- if ( localConstants . length ) {
1137
- const nameOfFirstConstant = localConstants [ 0 ] . name || 'COLORS'
1138
- return new ShouldDefineTopLevelConstant ( nameOfFirstConstant , '...' )
1139
- }
1140
- }
1141
1132
}
1142
1133
1143
1134
export class ResistorColorDuoSolution {
@@ -1208,6 +1199,10 @@ export class ResistorColorDuoSolution {
1208
1199
return this . fileConstants . length === 1
1209
1200
}
1210
1201
1202
+ public get shouldExtractTopLevelConstant ( ) : boolean {
1203
+ return ! this . mainConstant && ! ! this . entry . nameOfConstantDefinedInBody
1204
+ }
1205
+
1211
1206
public get hasOptimalEntry ( ) : boolean {
1212
1207
return this . entry . isOptimal ( this . mainConstant , this . program )
1213
1208
}
0 commit comments