-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.ts
350 lines (285 loc) · 10.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/typescript-estree"
import { Identifier, Node, VariableDeclarator, VariableDeclaration } from "@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree"
import { Traverser } from "eslint/lib/util/traverser"
import { IsolatedAnalyzerImpl } from "../IsolatedAnalyzerImpl"
import { factory } from "../../comments/comment"
import { parameterName } from '../utils/extract_parameter'
import { findAll } from "../utils/find_all"
import { findFirst } from "../utils/find_first"
import { findFirstOfType } from "../utils/find_first_of_type"
import { isNewExpression } from "../utils/find_new_expression"
import { findRawLiteral } from "../utils/find_raw_literal"
import { findTopLevelConstants } from "../utils/find_top_level_constants"
import { isBinaryExpression } from "../utils/is_binary_expression"
import { isCallExpression } from "../utils/is_call_expression"
import { isIdentifier } from "../utils/is_identifier"
import { isLiteral } from "../utils/is_literal"
import { NO_METHOD, NO_NAMED_EXPORT, NO_PARAMETER, UNEXPECTED_PARAMETER, PREFER_CONST_OVER_LET_AND_VAR } from "../../comments/shared";
import { AstParser } from "../../parsers/AstParser";
import { GigasecondSolution } from "./GigasecondSolution";
import { NoMethodError } from "~src/errors/NoMethodError";
import { NoExportError } from "~src/errors/NoExportError";
const TIP_EXPORT_INLINE = factory<'method_signature'>`
Did you know that you can export functions, classes and constants directly
inline?
\`\`\`javascript
export ${'method_signature'}
\`\`\`
`('javascript.gigasecond.export_inline')
const USE_NUMBER_COMPREHENSION = factory<'literal'>`
Large numbers like \`${'literal'}\` are easy to misread and difficult to
comprehend. Rewrite the literal \`${'literal'}\` using \`Math.pow\` or
\`10 ** n\` to make it more readable and lower the cognitive complexity.
`('javascript.gigasecond.use_number_comprehension')
const PREFER_TOP_LEVEL_CONSTANT = factory<'value' | 'name'>`
Consider extracting the gigasecond number into a constant:
\`\`\`javascript
const ${'name'} = ${'value'}
export const gigasecond = (...)
\`\`\`
`('javascript.gigasecond.prefer_top_level_constant')
const SIGNATURE_NOT_OPTIMAL = factory`
If you look at the tests, the function \`gigasecond\` only receives one
paremeter. Nothing more and nothing less.
Remove the additional parameters from your function, as their value will always
be \`undefined\` or whatever default you've assigned.
`('javascript.gigasecond.signature_not_optimal')
type Program = TSESTree.Program
const Parser: AstParser = new AstParser(undefined, 1)
export class GigasecondAnalyzer extends IsolatedAnalyzerImpl {
protected async execute(input: Input, output: WritableOutput): Promise<void> {
const [parsed] = await Parser.parse(input)
// Firstly we want to check that the structure of this solution is correct
// and that there is nothing structural stopping it from passing the tests
const solution = this.checkStructure(parsed.program, output)
// Now we want to ensure that the method signature is sane and that it has
// valid arguments.
this.checkSignature(solution, output)
// There are a handful optimal solutions for gigasecond which needs no
// comments and can just be approved. If we have it, then let's just
// acknowledge it and get out of here.
this.checkForOptimalSolutions(solution, output)
// The solution might not be optimal but still be approvable. Check these
// first and bail-out (with approval) if that's the case.
this.checkForApprovableSolutions(solution, output)
// Time to find sub-optimal code.
// The solution is automatically referred to the mentor if it reaches this
}
private checkStructure(program: Readonly<Program>, output: WritableOutput) {
try {
return new GigasecondSolution(program)
} catch (error) {
if (error instanceof NoMethodError) {
output.disapprove(NO_METHOD({ method_name: error.method }))
}
if (error instanceof NoExportError) {
output.disapprove(NO_NAMED_EXPORT({ export_name: error.namedExport }))
}
throw error
}
}
private checkSignature({ entry }: GigasecondSolution, output: WritableOutput) {
// If there is no parameter then this solution won't pass the tests.
//
if (!entry.hasAtLeastOneParameter) {
output.disapprove(NO_PARAMETER({ function_name: entry.name }))
}
// If this is not a simple parameter, but something else such as a splat,
// or a parameter with a default argument, bail out and refer to mentor.
//
if (!entry.hasSimpleParameter) {
output.redirect(UNEXPECTED_PARAMETER({ type: entry.parameterType }))
}
// If there is more than one parameter, something fishy is going on. Collect
// the comment for the student, to disapprove later on.
//
if (!entry.hasExactlyOneParameter) {
output.add(SIGNATURE_NOT_OPTIMAL())
}
// TODO: do we want to check more here?
//
//
if (output.hasCommentary) {
output.disapprove()
}
}
private checkForOptimalSolutions(solution: GigasecondSolution, output: WritableOutput) {
// The optional solution looks like this:
//
// const GIGASECOND_IN_MS = 10 ** 12
//
// export function gigasecond(input) {
// return new Date(input.getTime() + GIGASECOND_IN_MS)
// }
//
// It does not modify the input, it does not use `setXXX`, it extracts the
// large number into a const and it uses Math.pow or ** to write the large
// number.
//
if (!solution.isOptimal()) {
// continue analyzing
this.logger.log('~> Solution is not optimal')
return
}
this.checkForTips(solution, output)
output.approve()
}
private checkForApprovableSolutions(solution: GigasecondSolution, output: WritableOutput) {
if (!solution.constant) {
// This means there is no constant found. The approvable solution looks
// like this
//
// export function gigasecond(input) {
// return new Date(input.getTime() + 10 ** 12)
// }
//
// Or this
//
// export function gigasecond(input) {
// const GIGASECOND_IN_MS = 10 ** 12
// return new Date(input.getTime() + GIGASECOND_IN_MS)
// }
return
}
}
private checkForTips(solution: GigasecondSolution, output: WritableOutput) {
if (!solution.hasInlineExport) {
// export { gigasecond }
output.add(
TIP_EXPORT_INLINE({
method_signature: 'function gigasecond(...) { ... }',
})
)
}
// For the optimal solution, this may be anything
//
if (solution.hasOneConstant && !solution.areFileConstantsConst) {
const { constant } = solution
if (constant) {
// let GIGASECOND_IN_MS =
// var GIGASECOND_IN_MS =
output.add(
PREFER_CONST_OVER_LET_AND_VAR({
kind: constant.kind,
name: constant.name
})
)
}
}
}
}
/*
private checkForApprovableSolutions() {
if (!this.isOneLineSolution()) {
return
}
if (!this.isUsingGetTimeOnce() || !this.isUsingNewDateOnce()) {
return
}
this.checkForTips()
this.approve()
}
private isUsingGetTimeOnce() {
const { name } = this.mainParameter
return findAll(this.mainMethod!, (node) => isCallExpression(node, name, 'getTime')).length === 1
}
private isUsingNewDateOnce() {
return findAll(this.mainMethod!, (node) => isNewExpression(node) && isIdentifier(node.callee, 'Date')).length === 1
}
private findExtractedNumberConstant(): VariableDeclarator | undefined {
// Remove the main method (which could be a top-level constant)
const found = findTopLevelConstants(this.program)
.flatMap((constant) => constant.declarations)
.filter(declaration => declaration.init !== this.mainMethod)
if (found.length === 0) {
return undefined
}
const numberComprehension = this.findNumberComprehension()
return found.find(f => f.init === numberComprehension)
}
private isUsingLargeNumberLiteral() {
return findRawLiteral(this.mainMethod!, '1000000000000') === undefined
&& findRawLiteral(this.mainMethod!, '1000000000') === undefined
}
private isUsingIntermediateVariable() {
return findFirstOfType(this.mainMethod!, AST_NODE_TYPES.VariableDeclaration)
}
private findNumberComprehension(): Node | undefined {
if (this.memoized['number-comprehension']) {
return this.memoized['number-comprehension']
}
const self = this
return this.memoized['number-comprehension'] =
findFirst(this.mainMethod!, function(node) {
return self.isNumberComprehension.call(this, self, node)
})
}
private isNumberComprehension(this: Traverser, self: this, node: Node): boolean {
// Math.pow(10, 12)
if (isCallExpression(node, 'Math', 'pow')) {
this.skip()
if (node.arguments.length === 2) {
if (isLiteral(node.arguments[0], 10)) {
return isLiteral(node.arguments[1], 12)
}
}
return false
}
// 1e12
if (isLiteral(node, undefined, '1e12')) {
return true
}
// (a ** b) and (a * b)
if (isBinaryExpression(node)) {
// 1e9 * 1e3
// (10 ** 9) * 1000
// Math.pow(10, 9) * 1000
if (isBinaryExpression(node, '*')) {
// ... * 1000
// ... * 1e3
const rightIsLiteral = isLiteral(node.right, 1000)
if (rightIsLiteral) {
return self.isSmallerNumberComprehension.call(this, node.left)
}
// 1000 * ...
// 1e3 * ...
const leftIsLiteral = isLiteral(node.left, 1000)
if (leftIsLiteral) {
return self.isSmallerNumberComprehension.call(this, node.right)
}
// Don't return here, because of the type guard being too tight, it
// will think that `node` now must be "never"
//
// return false
}
if (isBinaryExpression(node, '**')) {
// 10 ** 12
return isLiteral(node.left, 10) && isLiteral(node.right, 12)
}
return false
}
// 1e12
if (isLiteral(node, undefined, '1e12')) {
return true
}
return false
}
private isSmallerNumberComprehension(this: Traverser, node: Node): boolean {
// 1e9
// 1000000000
if (isLiteral(node, 1e9)) {
return true
}
// Math.pow(10, 9)
if (isCallExpression(node, 'Math', 'pow')) {
this.skip()
if (node.arguments.length === 2) {
if (isLiteral(node.arguments[0], 10)) {
return isLiteral(node.arguments[1], 9)
}
}
}
return false
}
}
*/