Skip to content

Commit ac10f9e

Browse files
committed
Fix constant lookup
1 parent 17413f1 commit ac10f9e

5 files changed

Lines changed: 32 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 0.5.3
4+
5+
- Fix `resistor-color` constant lookup, when name doesn't match "probably"
6+
- Fix `resistor-color-duo` constant lookup, when name doesn't match "probably"
7+
- Add message to `resistor-color-duo` for `param.shift()`
8+
9+
## 0.5.2
10+
11+
- Cleanup imports and exports
12+
313
## 0.5.1
414

515
- Fix `resistor-color-duo` helper code (with `resistor-color` rewrite changed)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@exercism/javascript-analyzer",
3-
"version": "0.5.1",
3+
"version": "0.5.3",
44
"description": "Exercism analyzer for javascript",
55
"repository": "https://github.com/exercism/javascript-analyzer",
66
"author": "Derk-Jan Karrenbeld <derk-jan+github@karrenbeld.info>",

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ export class HelperCallNotFound {
4747
constructor(public readonly callRoot: Expression | undefined) {}
4848
}
4949

50+
export class UnexpectedCallFound {
51+
constructor(public readonly unexpected: string, public readonly expected: string) {}
52+
}
53+
5054
type Issue = undefined
5155
| MissingExpectedCall
5256
| HelperNotOptimal
5357
| MethodNotFound
5458
| HelperCallNotFound
59+
| UnexpectedCallFound
5560

5661
class Constant {
5762
public readonly name: string
@@ -651,6 +656,12 @@ class Entry {
651656
&& isLiteral(innerLeft, 10)
652657
}
653658

659+
if (findFirst(argument, (node) => isIdentifier(param) && isCallExpression(node, param.name, 'shift'))) {
660+
logger.log('~> should not be shifting')
661+
this.lastIssue_ = new UnexpectedCallFound('.shift()', `${parameterName(param)}[<index>]\` or \`[destruc, turing]`)
662+
return false
663+
}
664+
654665
logger.log('~> body of expected math solution is not math')
655666
return false
656667
}
@@ -769,7 +780,7 @@ export class ResistorColorDuoSolution {
769780

770781
const expectedConstant = this.fileConstants.find((constant) => isIdentifier(constant.id, PROBABLE_CONSTANT)) ||
771782
// Or find the first array or object assignment
772-
this.fileConstants.find((constant) => constant.init && [AST_NODE_TYPES.ArrayExpression, AST_NODE_TYPES.ObjectExpression].indexOf(constant.init.type))
783+
this.fileConstants.find((constant) => constant.init && [AST_NODE_TYPES.ArrayExpression, AST_NODE_TYPES.ObjectExpression].indexOf(constant.init.type) !== -1)
773784

774785
this.mainConstant = expectedConstant && new Constant(expectedConstant, this.source) || undefined
775786
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { NoExportError } from "~src/errors/NoExportError";
55
import { NoMethodError } from "~src/errors/NoMethodError";
66
import { AstParser } from "~src/parsers/AstParser";
77
import { IsolatedAnalyzerImpl } from "../IsolatedAnalyzerImpl";
8-
import { HelperCallNotFound, HelperNotOptimal, MethodNotFound, MissingExpectedCall, ResistorColorDuoSolution } from "./ResistorColorDuoSolution";
8+
import { HelperCallNotFound, HelperNotOptimal, MethodNotFound, MissingExpectedCall, UnexpectedCallFound, ResistorColorDuoSolution } from "./ResistorColorDuoSolution";
99

1010
const TIP_EXPORT_INLINE = factory<'method.signature'>`
1111
Did you know that you can export functions, classes and constants directly
@@ -84,6 +84,10 @@ const ISSUE_EXPECTED_CALL = factory<'method.name' | 'expected.reason'>`
8484
that reasoning applies, mentor the student to add this call.
8585
`('javascript.resistor-color-duo.must_add_missing_call')
8686

87+
const ISSUE_UNEXPECTED_CALL = factory<'unexpected' | 'expected'>`
88+
📕 Found \`${'unexpected'}\`, expected \`${'expected'}\`.
89+
`('javascript.resistor-color-duo.expected_different_call')
90+
8791
type Program = TSESTree.Program
8892

8993
const Parser: AstParser = new AstParser(undefined, 1)
@@ -201,6 +205,9 @@ export class ResistorColorDuoAnalyzer extends IsolatedAnalyzerImpl {
201205
} else if (lastIssue instanceof MethodNotFound) {
202206
// output.add(BETA_COMMENTARY_PREFIX())
203207
output.disapprove(ISSUE_METHOD_NOT_FOUND({ 'method.name': lastIssue.methodName }))
208+
} else if (lastIssue instanceof UnexpectedCallFound) {
209+
output.add(ISSUE_UNEXPECTED_CALL({ 'unexpected': lastIssue.unexpected, 'expected': lastIssue.expected }))
210+
204211
} else if (lastIssue instanceof MissingExpectedCall) {
205212
// output.add(BETA_COMMENTARY_PREFIX())
206213
output.add(ISSUE_EXPECTED_CALL({ 'method.name': lastIssue.methodName, 'expected.reason': lastIssue.reason }))

src/analyzers/resistor-color/ResistorColorSolution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ export class ResistorColorSolution {
345345
// Find expected name
346346
const expectedConstant = this.fileConstants.find((constant) => isIdentifier(constant.id, EXPECTED_CONSTANT)) ||
347347
// Or find the first array or object assignment
348-
this.fileConstants.find((constant) => constant.init && [AST_NODE_TYPES.ArrayExpression, AST_NODE_TYPES.ObjectExpression].indexOf(constant.init.type))
348+
this.fileConstants.find((constant) => constant.init && [AST_NODE_TYPES.ArrayExpression, AST_NODE_TYPES.ObjectExpression].indexOf(constant.init.type) === -1)
349349

350350
this.mainConstant = expectedConstant && new Constant(expectedConstant, this.source) || undefined
351351
}

0 commit comments

Comments
 (0)