diff --git a/src/analyzers/practice/resistor-color-duo/ResistorColorDuoSolution.ts b/src/analyzers/practice/resistor-color-duo/ResistorColorDuoSolution.ts index ab2bfc3a..f7f82bef 100644 --- a/src/analyzers/practice/resistor-color-duo/ResistorColorDuoSolution.ts +++ b/src/analyzers/practice/resistor-color-duo/ResistorColorDuoSolution.ts @@ -4,6 +4,7 @@ import { ExtractedVariable, extractExports, extractFunctions, + extractVariables, findAll, findFirst, findTopLevelConstants, @@ -73,6 +74,10 @@ export class UnexpectedCallFound { ) {} } +export class ShouldDefineTopLevelConstant { + constructor(public readonly name: string, public readonly value: string) {} +} + type Issue = | undefined | MissingExpectedCall @@ -80,6 +85,7 @@ type Issue = | MethodNotFound | HelperCallNotFound | UnexpectedCallFound + | ShouldDefineTopLevelConstant class Constant { public readonly name: string @@ -508,6 +514,15 @@ class Entry { } } + if (!constant) { + const issue = this.hasConstantDefinedInBody() + if (issue instanceof ShouldDefineTopLevelConstant) { + logger.log('~> found a constant that was not declared at the top level') + this.lastIssue_ = issue + return false + } + } + if (this.hasOneMap) { logger.log('~> is a map solution') return this.isOptimalMapSolution(logger, this.body, constant, program) @@ -1111,6 +1126,18 @@ class Entry { logger.log(`~> constant is not optimal`) return false } + + private hasConstantDefinedInBody(): ShouldDefineTopLevelConstant | undefined { + const localConstants = extractVariables(this.body).filter( + (constant) => + constant.init?.type === AST_NODE_TYPES.ArrayExpression || + constant.init?.type === AST_NODE_TYPES.ObjectExpression + ) + if (localConstants.length) { + const nameOfFirstConstant = localConstants[0].name || 'COLORS' + return new ShouldDefineTopLevelConstant(nameOfFirstConstant, '...') + } + } } export class ResistorColorDuoSolution { diff --git a/src/analyzers/practice/resistor-color-duo/index.ts b/src/analyzers/practice/resistor-color-duo/index.ts index f2356a1b..a3ac651d 100644 --- a/src/analyzers/practice/resistor-color-duo/index.ts +++ b/src/analyzers/practice/resistor-color-duo/index.ts @@ -21,6 +21,7 @@ import { MethodNotFound, MissingExpectedCall, ResistorColorDuoSolution, + ShouldDefineTopLevelConstant, UnexpectedCallFound, } from './ResistorColorDuoSolution' @@ -120,6 +121,21 @@ const ISSUE_UNEXPECTED_CALL = factory<'unexpected' | 'expected'>` CommentType.Actionable ) +const PREFER_EXTRACTED_TOP_LEVEL_CONSTANT = factory<'value' | 'name'>` +Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const ${'name'} = ${'value'} + +export const decodedValue = (...) +\`\`\` +`( + 'javascript.resistor-color-duo.prefer_extracted_top_level_constant', + CommentType.Actionable +) + type Program = TSESTree.Program export class ResistorColorDuoAnalyzer extends IsolatedAnalyzerImpl { @@ -275,6 +291,13 @@ export class ResistorColorDuoAnalyzer extends IsolatedAnalyzerImpl { } output.disapprove() + } else if (lastIssue instanceof ShouldDefineTopLevelConstant) { + output.add( + PREFER_EXTRACTED_TOP_LEVEL_CONSTANT({ + name: lastIssue.name, + value: lastIssue.value, + }) + ) } else { this.logger.error( 'The analyzer did not handle the issue: ' + JSON.stringify(lastIssue) diff --git a/test/analyzers/resistor-color-duo/__snapshots__/snapshot.ts.snap b/test/analyzers/resistor-color-duo/__snapshots__/snapshot.ts.snap index e481a05c..20fdbb03 100644 --- a/test/analyzers/resistor-color-duo/__snapshots__/snapshot.ts.snap +++ b/test/analyzers/resistor-color-duo/__snapshots__/snapshot.ts.snap @@ -86,6 +86,32 @@ lower cognitive complexity.", exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/10's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const colors = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "colors", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.prefer_number_over_parse", "message": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more @@ -588,6 +614,32 @@ call\\" with a named call, that can be documented individually.", exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/105's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const COLORS = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "COLORS", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.prefer_number_over_parse", "message": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more @@ -1187,24 +1239,59 @@ exports[`When running analysis on resistor-color-duo fixtures and expecting matc IsolatedAnalyzerOutput { "comments": Array [ CommentImpl { - "externalTemplate": "javascript.resistor-color-duo.must_use_a_helper", - "message": "📕 Mentor the student to add helper function and DRY-up this solution. The -solution to \`resistor-color\` can be used as helper method here. When using an -\`Array\` as colors source, in a years time, will the student recall why it's -the _index_ in that array? When using an \`Object\`, what does the value mean? -Re-using \`colorCode\` explains this in both cases. + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. -💬 Using a helper method is good practice, because it replaces a cryptic \\"member -call\\" with a named call, that can be documented individually.", - "template": "📕 Mentor the student to add helper function and DRY-up this solution. The -solution to \`resistor-color\` can be used as helper method here. When using an -\`Array\` as colors source, in a years time, will the student recall why it's -the _index_ in that array? When using an \`Object\`, what does the value mean? -Re-using \`colorCode\` explains this in both cases. +\`\`\`javascript +const COLORS = ... -💬 Using a helper method is good practice, because it replaces a cryptic \\"member -call\\" with a named call, that can be documented individually.", +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", "type": "actionable", + "variables": Object { + "name": "COLORS", + "value": "...", + }, + }, + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_number_over_parse", + "message": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more +strict than the \`parseXXX\` family and applies in this exercise.", + "template": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more +strict than the \`parseXXX\` family and applies in this exercise.", + "type": "actionable", + "variables": Object {}, + }, + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.limit_number_of_colors", + "message": "💬 Limit the number of input colors that are processed. If more than two colors +are passed in, only the first two colors should be used to calculate the total +\`colorCode\` value. + +📕 (At least) one test case inputs three colors instead of two. If the student +has not accounted for this, they might need to update their solution. Help them +find the button to update. The tests won't pass without limiting the number of +colors.", + "template": "💬 Limit the number of input colors that are processed. If more than two colors +are passed in, only the first two colors should be used to calculate the total +\`colorCode\` value. + +📕 (At least) one test case inputs three colors instead of two. If the student +has not accounted for this, they might need to update their solution. Help them +find the button to update. The tests won't pass without limiting the number of +colors.", + "type": "essential", "variables": Object {}, }, ], @@ -1274,6 +1361,32 @@ The tests won't pass without it.", exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/143's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const CODES = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "CODES", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.use_array_comprehensions", "message": "💬 Replace \`.forEach(...)\` with a comprehension such as \`map\`.", @@ -1820,6 +1933,32 @@ strict than the \`parseXXX\` family and applies in this exercise.", exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/177's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const COLORS = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "COLORS", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.prefer_number_over_parse", "message": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more @@ -1872,34 +2011,64 @@ exports[`When running analysis on resistor-color-duo fixtures and expecting matc IsolatedAnalyzerOutput { "comments": Array [ CommentImpl { - "externalTemplate": "javascript.resistor-color-duo.must_use_a_helper", - "message": "📕 Mentor the student to add helper function and DRY-up this solution. The -solution to \`resistor-color\` can be used as helper method here. When using an -\`Array\` as colors source, in a years time, will the student recall why it's -the _index_ in that array? When using an \`Object\`, what does the value mean? -Re-using \`colorCode\` explains this in both cases. + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. -💬 Using a helper method is good practice, because it replaces a cryptic \\"member -call\\" with a named call, that can be documented individually.", - "template": "📕 Mentor the student to add helper function and DRY-up this solution. The -solution to \`resistor-color\` can be used as helper method here. When using an -\`Array\` as colors source, in a years time, will the student recall why it's -the _index_ in that array? When using an \`Object\`, what does the value mean? -Re-using \`colorCode\` explains this in both cases. +\`\`\`javascript +const colorMap = ... -💬 Using a helper method is good practice, because it replaces a cryptic \\"member -call\\" with a named call, that can be documented individually.", +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", "type": "actionable", - "variables": Object {}, + "variables": Object { + "name": "colorMap", + "value": "...", + }, }, ], - "summary": undefined, } `; exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/183's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const colors = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "colors", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.prefer_number_over_parse", "message": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more @@ -1981,24 +2150,59 @@ exports[`When running analysis on resistor-color-duo fixtures and expecting matc IsolatedAnalyzerOutput { "comments": Array [ CommentImpl { - "externalTemplate": "javascript.resistor-color-duo.must_use_a_helper", - "message": "📕 Mentor the student to add helper function and DRY-up this solution. The -solution to \`resistor-color\` can be used as helper method here. When using an -\`Array\` as colors source, in a years time, will the student recall why it's -the _index_ in that array? When using an \`Object\`, what does the value mean? -Re-using \`colorCode\` explains this in both cases. + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. -💬 Using a helper method is good practice, because it replaces a cryptic \\"member -call\\" with a named call, that can be documented individually.", - "template": "📕 Mentor the student to add helper function and DRY-up this solution. The -solution to \`resistor-color\` can be used as helper method here. When using an -\`Array\` as colors source, in a years time, will the student recall why it's -the _index_ in that array? When using an \`Object\`, what does the value mean? -Re-using \`colorCode\` explains this in both cases. +\`\`\`javascript +const bandColors = ... -💬 Using a helper method is good practice, because it replaces a cryptic \\"member -call\\" with a named call, that can be documented individually.", +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", "type": "actionable", + "variables": Object { + "name": "bandColors", + "value": "...", + }, + }, + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_number_over_parse", + "message": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more +strict than the \`parseXXX\` family and applies in this exercise.", + "template": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more +strict than the \`parseXXX\` family and applies in this exercise.", + "type": "actionable", + "variables": Object {}, + }, + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.limit_number_of_colors", + "message": "💬 Limit the number of input colors that are processed. If more than two colors +are passed in, only the first two colors should be used to calculate the total +\`colorCode\` value. + +📕 (At least) one test case inputs three colors instead of two. If the student +has not accounted for this, they might need to update their solution. Help them +find the button to update. The tests won't pass without limiting the number of +colors.", + "template": "💬 Limit the number of input colors that are processed. If more than two colors +are passed in, only the first two colors should be used to calculate the total +\`colorCode\` value. + +📕 (At least) one test case inputs three colors instead of two. If the student +has not accounted for this, they might need to update their solution. Help them +find the button to update. The tests won't pass without limiting the number of +colors.", + "type": "essential", "variables": Object {}, }, ], @@ -2262,6 +2466,32 @@ call\\" with a named call, that can be documented individually.", exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/203's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const COLORS = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "COLORS", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.prefer_number_over_parse", "message": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more @@ -2296,6 +2526,32 @@ strict than the \`parseXXX\` family and applies in this exercise.", exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/205's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const COLORS = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "COLORS", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.use_array_comprehensions", "message": "💬 Replace \`for(...) { }\` with a comprehension such as \`map\`.", @@ -2544,6 +2800,32 @@ call\\" with a named call, that can be documented individually.", exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/224's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const COLORS = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "COLORS", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.prefer_number_over_parse", "message": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more @@ -2731,6 +3013,32 @@ IsolatedAnalyzerOutput { exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/235's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const colors = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "colors", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.prefer_number_over_parse", "message": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more @@ -3143,6 +3451,32 @@ call\\" with a named call, that can be documented individually.", exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/263's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const resistor = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "resistor", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.use_array_comprehensions", "message": "💬 Replace \`.forEach(...)\` with a comprehension such as \`map\`.", @@ -3440,25 +3774,30 @@ exports[`When running analysis on resistor-color-duo fixtures and expecting matc IsolatedAnalyzerOutput { "comments": Array [ CommentImpl { - "externalTemplate": "javascript.resistor-color-duo.must_use_a_helper", - "message": "📕 Mentor the student to add helper function and DRY-up this solution. The -solution to \`resistor-color\` can be used as helper method here. When using an -\`Array\` as colors source, in a years time, will the student recall why it's -the _index_ in that array? When using an \`Object\`, what does the value mean? -Re-using \`colorCode\` explains this in both cases. + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. -💬 Using a helper method is good practice, because it replaces a cryptic \\"member -call\\" with a named call, that can be documented individually.", - "template": "📕 Mentor the student to add helper function and DRY-up this solution. The -solution to \`resistor-color\` can be used as helper method here. When using an -\`Array\` as colors source, in a years time, will the student recall why it's -the _index_ in that array? When using an \`Object\`, what does the value mean? -Re-using \`colorCode\` explains this in both cases. +\`\`\`javascript +const COLORS = ... -💬 Using a helper method is good practice, because it replaces a cryptic \\"member -call\\" with a named call, that can be documented individually.", +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", "type": "actionable", - "variables": Object {}, + "variables": Object { + "name": "COLORS", + "value": "...", + }, }, ], "summary": undefined, @@ -3651,6 +3990,32 @@ strict than the \`parseXXX\` family and applies in this exercise.", exports[`When running analysis on resistor-color-duo fixtures and expecting matches resistor-color-duo/289's output: output 1`] = ` IsolatedAnalyzerOutput { "comments": Array [ + CommentImpl { + "externalTemplate": "javascript.resistor-color-duo.prefer_extracted_top_level_constant", + "message": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const array = ... + +export const decodedValue = (...) +\`\`\`", + "template": "Instead of defining the constant _inside_ the function, consider extracting it +to the top-level. Constants, functions, and classes that are not \`export\`ed, +are not accessible from outside the file. + +\`\`\`javascript +const %{name} = %{value} + +export const decodedValue = (...) +\`\`\`", + "type": "actionable", + "variables": Object { + "name": "array", + "value": "...", + }, + }, CommentImpl { "externalTemplate": "javascript.resistor-color-duo.prefer_number_over_parse", "message": "💬 Use \`Number(...)\` when the input is expected to be a number. It's more