Skip to content

Commit 1b7b120

Browse files
committed
[Resistor Duo analyzer (exercism#127)] Implement requested changes
1 parent 11cd9be commit 1b7b120

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

src/analyzers/practice/resistor-color-duo/ResistorColorDuoSolution.ts

+19-24
Original file line numberDiff line numberDiff line change
@@ -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-
8177
type Issue =
8278
| undefined
8379
| MissingExpectedCall
8480
| HelperNotOptimal
8581
| MethodNotFound
8682
| HelperCallNotFound
8783
| UnexpectedCallFound
88-
| ShouldDefineTopLevelConstant
8984

9085
class 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

11431134
export 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
}

src/analyzers/practice/resistor-color-duo/index.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
MethodNotFound,
2222
MissingExpectedCall,
2323
ResistorColorDuoSolution,
24-
ShouldDefineTopLevelConstant,
2524
UnexpectedCallFound,
2625
} from './ResistorColorDuoSolution'
2726

@@ -291,13 +290,6 @@ export class ResistorColorDuoAnalyzer extends IsolatedAnalyzerImpl {
291290
}
292291

293292
output.disapprove()
294-
} else if (lastIssue instanceof ShouldDefineTopLevelConstant) {
295-
output.add(
296-
PREFER_EXTRACTED_TOP_LEVEL_CONSTANT({
297-
name: lastIssue.name,
298-
value: lastIssue.value,
299-
})
300-
)
301293
} else {
302294
this.logger.error(
303295
'The analyzer did not handle the issue: ' + JSON.stringify(lastIssue)
@@ -310,6 +302,15 @@ export class ResistorColorDuoAnalyzer extends IsolatedAnalyzerImpl {
310302
solution: ResistorColorDuoSolution,
311303
output: WritableOutput
312304
): void | never {
305+
if (solution.shouldExtractTopLevelConstant) {
306+
output.add(
307+
PREFER_EXTRACTED_TOP_LEVEL_CONSTANT({
308+
name: solution.entry.nameOfConstantDefinedInBody,
309+
value: '...',
310+
})
311+
)
312+
}
313+
313314
if (solution || output) {
314315
return
315316
}

0 commit comments

Comments
 (0)