@@ -74,18 +74,13 @@ export class UnexpectedCallFound {
7474 ) { }
7575}
7676
77- export class ShouldDefineTopLevelConstant {
78- constructor ( public readonly name : string , public readonly value : string ) { }
79- }
80-
8177type Issue =
8278 | undefined
8379 | MissingExpectedCall
8480 | HelperNotOptimal
8581 | MethodNotFound
8682 | HelperCallNotFound
8783 | UnexpectedCallFound
88- | ShouldDefineTopLevelConstant
8984
9085class Constant {
9186 public readonly name : string
@@ -486,6 +481,18 @@ class Entry {
486481 return parameterName ( this . params [ 0 ] )
487482 }
488483
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+
489496 public isOptimal (
490497 constant : Readonly < Constant > | undefined ,
491498 program : Program
@@ -514,13 +521,9 @@ class Entry {
514521 }
515522 }
516523
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
524527 }
525528
526529 if ( this . hasOneMap ) {
@@ -1126,18 +1129,6 @@ class Entry {
11261129 logger . log ( `~> constant is not optimal` )
11271130 return false
11281131 }
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- }
11411132}
11421133
11431134export class ResistorColorDuoSolution {
@@ -1208,6 +1199,10 @@ export class ResistorColorDuoSolution {
12081199 return this . fileConstants . length === 1
12091200 }
12101201
1202+ public get shouldExtractTopLevelConstant ( ) : boolean {
1203+ return ! this . mainConstant && ! ! this . entry . nameOfConstantDefinedInBody
1204+ }
1205+
12111206 public get hasOptimalEntry ( ) : boolean {
12121207 return this . entry . isOptimal ( this . mainConstant , this . program )
12131208 }
0 commit comments