Skip to content

Commit 17413f1

Browse files
committed
Clean up imports and exports
1 parent d3391ec commit 17413f1

3 files changed

Lines changed: 34 additions & 106 deletions

File tree

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/typescript-estree";
2-
1+
import { TSESTree } from "@typescript-eslint/typescript-estree";
32
import { factory } from "~src/comments/comment";
4-
import { NO_METHOD, NO_NAMED_EXPORT, NO_PARAMETER, BETA_COMMENTARY_PREFIX, UNEXPECTED_PARAMETER } from "~src/comments/shared";
3+
import { NO_METHOD, NO_NAMED_EXPORT, NO_PARAMETER, UNEXPECTED_PARAMETER } from "~src/comments/shared";
54
import { NoExportError } from "~src/errors/NoExportError";
65
import { NoMethodError } from "~src/errors/NoMethodError";
76
import { AstParser } from "~src/parsers/AstParser";
8-
97
import { IsolatedAnalyzerImpl } from "../IsolatedAnalyzerImpl";
10-
import { ResistorColorDuoSolution, HelperNotOptimal, HelperCallNotFound, MethodNotFound, MissingExpectedCall } from "./ResistorColorDuoSolution";
8+
import { HelperCallNotFound, HelperNotOptimal, MethodNotFound, MissingExpectedCall, ResistorColorDuoSolution } from "./ResistorColorDuoSolution";
119

1210
const TIP_EXPORT_INLINE = factory<'method.signature'>`
1311
Did you know that you can export functions, classes and constants directly

src/analyzers/resistor-color/ResistorColorSolution.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/typescript-estree";
2-
import { ArrowFunctionExpression, FunctionExpression, Statement, CallExpression, BlockStatement, ReturnStatement, Node } from "@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree";
2+
import { Node, Statement } from "@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree";
33
import { extractExport } from "~src/analyzers/utils/extract_export";
44
import { extractMainBody, MainBody } from "~src/analyzers/utils/extract_main_body";
55
import { extractMainMethod, MainMethod } from "~src/analyzers/utils/extract_main_method";
@@ -27,28 +27,6 @@ const EXPECTED_EXPORT_METHOD = 'colorCode'
2727
const EXPECTED_CONSTANT = 'COLORS'
2828
const EXPECTED_EXPORT_CONSTANT = 'COLORS'
2929

30-
export class MissingExpectedCall {
31-
constructor(public readonly methodName: string, public readonly reason: string) {}
32-
}
33-
34-
export class HelperNotOptimal {
35-
constructor(public readonly helperName: string, public readonly declaration: MainMethod<string>) {}
36-
}
37-
38-
export class MethodNotFound {
39-
constructor(public readonly methodName: string) {}
40-
}
41-
42-
export class HelperCallNotFound {
43-
constructor(public readonly callRoot: Expression | undefined) {}
44-
}
45-
46-
type Issue = undefined
47-
| MissingExpectedCall
48-
| HelperNotOptimal
49-
| MethodNotFound
50-
| HelperCallNotFound
51-
5230
class Constant {
5331
public readonly name: string
5432
public readonly signature: string;
@@ -184,7 +162,6 @@ class Entry {
184162

185163
private readonly params: readonly Parameter[];
186164
private readonly body: MainBody;
187-
private lastIssue_: Issue;
188165

189166
constructor(method: Readonly<NonNullable<MainMethod>>, source: Readonly<Source>) {
190167
this.name = (method && method.id && method.id.name) || EXPECTED_METHOD
@@ -194,10 +171,6 @@ class Entry {
194171
this.signature = source.getOuter(method.parent || method)
195172
}
196173

197-
public get lastIssue(): Issue {
198-
return this.lastIssue_
199-
}
200-
201174
public get hasAtLeastOneParameter(): boolean {
202175
return this.params.length > 0
203176
}

src/analyzers/resistor-color/index.ts

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/typescript-estree";
2-
1+
import { TSESTree } from "@typescript-eslint/typescript-estree";
32
import { factory } from "~src/comments/comment";
4-
import { NO_METHOD, NO_NAMED_EXPORT, NO_PARAMETER, BETA_COMMENTARY_PREFIX, UNEXPECTED_PARAMETER } from "~src/comments/shared";
3+
import { NO_METHOD, NO_NAMED_EXPORT, NO_PARAMETER, UNEXPECTED_PARAMETER } from "~src/comments/shared";
54
import { NoExportError } from "~src/errors/NoExportError";
65
import { NoMethodError } from "~src/errors/NoMethodError";
76
import { AstParser } from "~src/parsers/AstParser";
8-
97
import { IsolatedAnalyzerImpl } from "../IsolatedAnalyzerImpl";
10-
import { ResistorColorSolution, HelperNotOptimal, HelperCallNotFound, MethodNotFound, MissingExpectedCall } from "./ResistorColorSolution";
8+
import { ResistorColorSolution } from "./ResistorColorSolution";
119

1210
const TIP_EXPORT_INLINE = factory<'method.signature' | 'constant.signature'>`
1311
Did you know that you can export functions, classes and constants directly
@@ -21,63 +19,50 @@ export ${'method.signature'}
2119

2220
const SIGNATURE_NOT_OPTIMAL = factory`
2321
📕 If you look at the tests, the function \`colorCode\` only receives one
24-
parameter. Nothing more and nothing less.
25-
26-
📕 Remove the additional parameters from your function, as their value will
27-
always be \`undefined\` or whatever default you've assigned.
22+
parameter. Nothing more and nothing less. Suggest that the student
23+
removes the additional parameters from your function, as their value will
24+
always be \`undefined\` or whatever default they've assigned.
2825
`('javascript.resistor-color.signature_not_optimal')
2926

3027
const USE_INDEX_OF = factory<'current'>`
28+
📕 The analyzer expected \`indexOf\`, instead of \`${'current'}\`.
29+
3130
💬 Replace \`${'current'}\` with a different built-in function, a function
3231
which does exactly what is now explicitly programmed in: finding the index
3332
of a given value in an \`Array\`.
3433
`('javascript.resistor-color.use_index_of')
3534

35+
const USE_ARRAY_COMPREHENSIONS = factory<'current'>`
36+
📕 The analyzer expected \`indexOf\`, instead of \`${'current'}\`.
37+
38+
💬 Replace \`${'current'}\` with a single function call that _directly_
39+
finds the index of a given input value.
40+
`('javascript.resistor-color.use_array_comprehensions')
41+
3642
const USE_IMPLICIT_INCLUDES = factory<'current'>`
37-
💬 Remove the explicit existence check \`.includes\`. When the color-code
38-
is received, there is a special value that is returned if the color is not
39-
present. Use that special value instead.
43+
📕 Using \`includes\` iterates _twice_ over the array. Performance is not
44+
an issue in this exercise, but the logic is unnecessary. **Please note**:
45+
throwing an \`Error\`, when a color does not exist, is perfectly fine and
46+
should not be discouraged.
47+
48+
💬 Remove the explicit existence check \`.includes\`. When the color
49+
string is converted to a color code (number), there is already a special
50+
value which is returned if the color is not present. Look for and use
51+
that value instead.
4052
`('javascript.resistor-color.use_implicit_includes')
4153

4254
const DONT_NORMALISE_INPUTS = factory`
55+
📕 It's always fine to be defensive about inputs, so that a program will
56+
run correctly under more circumstances. However, arbitary input
57+
normalisation is discouraged on Exercism. Later exercises will have
58+
defined inputs that are denormalised where we'll expect sanitizing and
59+
normalising the input. Keep it simple for now.
60+
4361
💬 Remove the call to \`.toLowerCase\`. The tests only provide the inputs
4462
in lower case, and the colors should be defined in lower case. There is
4563
no need to manually normalise the inputs.
4664
`('javascript.resistor-color.dont_normalise_inputs')
4765

48-
const USE_ARRAY_COMPREHENSIONS = factory<'current'>`
49-
💬 Replace \`${'current'}\` with a single function call that _directly_
50-
finds the index of a given input value.
51-
`('javascript.resistor-color.use_array_comprehensions')
52-
53-
const ISSUE_OPTIMISE_HELPER = factory<'method.name'>`
54-
⚡ The helper method \`${'method.name'}\` is not optimal. The helper can
55-
probably be the same as the solution to \`resistor-color\`. Mentor the student
56-
to retrieve their solution and/or optimise their helper.
57-
`('javascript.resistor-color.must_optimise_helper')
58-
59-
const ISSUE_USE_A_HELPER = factory`
60-
📕 Mentor the student to add helper function and DRY-up this solution. The
61-
solution to \`resistor-color\` can be used as helper method here. When using an
62-
\`Array\` as colors source, in a years time, will the student recall why it's
63-
the _index_ in that array? When using an \`Object\`, what does the value mean?
64-
Re-using \`colorCode\` explains this in both cases.
65-
66-
💬 Using a helper method is good practice, because it replaces a cryptic "member
67-
call" with a named call, that can be documented individually.
68-
`('javascript.resistor-color.must_use_a_helper')
69-
70-
const ISSUE_METHOD_NOT_FOUND = factory<'method.name'>`
71-
⚡ Ensure the method \`${'method/name'}\` exists. It was not found when
72-
analysing this solution. If it does not exist, point this out to the student.
73-
74-
`('javascript.resistor-color.must_declare_function')
75-
76-
const ISSUE_EXPECTED_CALL = factory<'method.name' | 'expected.reason'>`
77-
📕 In order to ${'expected.reason'}, expected a \`${'method.name'}\` call. If
78-
that reasoning applies, mentor the student to add this call.
79-
`('javascript.resistor-color.must_add_missing_call')
80-
8166
type Program = TSESTree.Program
8267

8368
const Parser: AstParser = new AstParser(undefined, 1)
@@ -171,41 +156,13 @@ export class ResistorColorAnalyzer extends IsolatedAnalyzerImpl {
171156
if (!solution.isOptimal()) {
172157
// continue analyzing
173158
this.logger.log('~> solution is not optimal')
174-
this.processLastIssue(solution, output)
175159
return
176160
}
177161

178162
this.checkForTips(solution, output)
179163
output.approve()
180164
}
181165

182-
private processLastIssue(solution: ResistorColorSolution, output: WritableOutput): void | never {
183-
const lastIssue = solution.entry.lastIssue
184-
if (!lastIssue) {
185-
this.logger.log('~> no entry issue found')
186-
return
187-
}
188-
189-
if (lastIssue instanceof HelperNotOptimal) {
190-
// output.add(BETA_COMMENTARY_PREFIX())
191-
output.disapprove(ISSUE_OPTIMISE_HELPER({ 'method.name': lastIssue.helperName }))
192-
} else if (lastIssue instanceof HelperCallNotFound) {
193-
// output.add(BETA_COMMENTARY_PREFIX())
194-
output.disapprove(ISSUE_USE_A_HELPER())
195-
} else if (lastIssue instanceof MethodNotFound) {
196-
// output.add(BETA_COMMENTARY_PREFIX())
197-
output.disapprove(ISSUE_METHOD_NOT_FOUND({ 'method.name': lastIssue.methodName }))
198-
} else if (lastIssue instanceof MissingExpectedCall) {
199-
// output.add(BETA_COMMENTARY_PREFIX())
200-
output.add(ISSUE_EXPECTED_CALL({ 'method.name': lastIssue.methodName, 'expected.reason': lastIssue.reason }))
201-
202-
output.disapprove()
203-
} else {
204-
this.logger.error('The analyzer did not handle the issue: ' + JSON.stringify(lastIssue))
205-
output.redirect()
206-
}
207-
}
208-
209166
private checkForApprovableSolutions(solution: ResistorColorSolution, output: WritableOutput): void | never {
210167
if (solution || output) {
211168
return

0 commit comments

Comments
 (0)