Skip to content

Commit 27bba9d

Browse files
committed
Fix some type errors
1 parent ee7b2e3 commit 27bba9d

File tree

7 files changed

+50
-31
lines changed

7 files changed

+50
-31
lines changed

src/stepper/converter.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type es from 'estree'
22

3-
import { mockContext, mockImportDeclaration } from '../utils/testing/mocks'
43
import { parse } from '../parser/parser'
54
import { Chapter, Context, substituterNodes } from '../types'
5+
import { mockContext, mockImportDeclaration } from '../utils/testing/mocks'
66
import * as builtin from './lib'
77
import { javascriptify } from './stepper'
88
import * as util from './util'
@@ -53,7 +53,7 @@ export function nodeToValue(node: substituterNodes): any {
5353
return node.type === 'Literal'
5454
? node.value
5555
: util.isBuiltinFunction(node)
56-
? builtin[(node as es.Identifier).name]
56+
? builtin[(node as es.Identifier).name as keyof typeof builtin]
5757
: // tslint:disable-next-line
5858
eval(javascriptify(node))
5959
}
@@ -62,7 +62,7 @@ export function nodeToValueWithContext(node: substituterNodes, context: Context)
6262
return node.type === 'Literal'
6363
? node.value
6464
: util.isBuiltinFunction(node)
65-
? builtin[(node as es.Identifier).name]
65+
? builtin[(node as es.Identifier).name as keyof typeof builtin]
6666
: node.type === 'Identifier' && util.isImportedFunction(node, context)
6767
? context.runtime.environments[0].head[node.name]
6868
: // tslint:disable-next-line
@@ -77,11 +77,11 @@ function evaluateFunctionObject(node: substituterNodes, context: Context) {
7777
}
7878
visited.add(node)
7979
if (node.type === 'Identifier' && builtinFunctions[node.name]) {
80-
global[node.name] = builtinFunctions[node.name]
80+
;(global as any)[node.name] = builtinFunctions[node.name]
8181
}
8282
for (const key in node) {
83-
if (node[key] && typeof node[key] === 'object') {
84-
lookUpIdentifiers(node[key], visited)
83+
if (node[key as keyof typeof node] && typeof node[key as keyof typeof node] === 'object') {
84+
lookUpIdentifiers(node[key as keyof typeof node] as unknown as substituterNodes, visited)
8585
}
8686
}
8787
}

src/stepper/stepper.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type es from 'estree'
33

44
import type { IOptions } from '..'
55
import * as errors from '../errors/errors'
6+
import { RuntimeSourceError } from '../errors/runtimeSourceError'
67
import { parse } from '../parser/parser'
78
import type {
89
BlockExpression,
@@ -25,7 +26,6 @@ import { filterImportDeclarations } from '../utils/ast/helpers'
2526
import { evaluateBinaryExpression, evaluateUnaryExpression } from '../utils/operators'
2627
import * as rttc from '../utils/rttc'
2728
import { checkProgramForUndefinedVariables } from '../validator/validator'
28-
import { RuntimeSourceError } from '../errors/runtimeSourceError'
2929
import { nodeToValue, objectToString, valueToExpression } from './converter'
3030
import * as builtin from './lib'
3131
import {
@@ -210,7 +210,7 @@ function findMain(
210210

211211
const freeNames: any[] = []
212212

213-
const finders = {
213+
const finders: Partial<Record<any, (target: any) => void>> = {
214214
Identifier(target: es.Identifier): void {
215215
seenBefore.set(target, target)
216216
let bound = false
@@ -418,7 +418,7 @@ function substituteMain(
418418
* and push the appropriate access string into the path
419419
* 3. Return the dummyReplacement
420420
*/
421-
const substituters = {
421+
const substituters: Partial<Record<substituterNodes['type'], any>> = {
422422
// if name to be replaced is found,
423423
// push endMarker into path
424424
Identifier(
@@ -1325,7 +1325,7 @@ function reduceMain(
13251325

13261326
// converts body of code to string
13271327
function bodify(target: substituterNodes): string {
1328-
const bodifiers = {
1328+
const bodifiers: Partial<Record<substituterNodes['type'], (target: any) => string>> = {
13291329
Literal: (target: es.Literal): string =>
13301330
target.raw !== undefined ? target.raw : String(target.value),
13311331

@@ -1409,7 +1409,7 @@ function reduceMain(
14091409

14101410
// generates string to explain current step
14111411
function explain(target: substituterNodes): string {
1412-
const explainers = {
1412+
const explainers: Partial<Record<substituterNodes['type'], (target: any) => string>> = {
14131413
BinaryExpression: (target: es.BinaryExpression): string =>
14141414
'Binary expression ' + bodify(target) + ' evaluated',
14151415

@@ -1496,7 +1496,7 @@ function reduceMain(
14961496
return explainer === undefined ? '...' : explainer(target)
14971497
}
14981498

1499-
const reducers = {
1499+
const reducers: Partial<Record<substituterNodes['type'], any>> = {
15001500
// source 0
15011501
Identifier(
15021502
node: es.Identifier,
@@ -1738,8 +1738,10 @@ function reduceMain(
17381738
paths,
17391739
explain(node)
17401740
]
1741+
// @ts-expect-error implicitAnyIndexError
17411742
} else if (typeof builtin[(callee as es.Identifier).name] === 'function') {
17421743
// Source specific built-in function
1744+
// @ts-expect-error implicitAnyIndexError
17431745
return [builtin[(callee as es.Identifier).name](...args), context, paths, explain(node)]
17441746
} else {
17451747
// Common built-in function
@@ -1827,10 +1829,12 @@ function reduceMain(
18271829
)
18281830

18291831
// Fix path highlighting after preserving first statement
1830-
path.forEach(pathStep => {
1832+
path.forEach((pathStep: any[]) => {
18311833
pathStep.forEach((_, i) => {
18321834
if (i == 0) {
1833-
pathStep[i] = pathStep[i].replace(/\d+/g, match => String(Number(match) + 1))
1835+
pathStep[i] = pathStep[i].replace(/\d+/g, (match: any) =>
1836+
String(Number(match) + 1)
1837+
)
18341838
}
18351839
})
18361840
})
@@ -2055,10 +2059,12 @@ function reduceMain(
20552059
)
20562060

20572061
// Fix path highlighting after preserving first statement
2058-
path.forEach(pathStep => {
2062+
path.forEach((pathStep: any[]) => {
20592063
pathStep.forEach((_, i) => {
20602064
if (i == 0) {
2061-
pathStep[i] = pathStep[i].replace(/\d+/g, match => String(Number(match) + 1))
2065+
pathStep[i] = pathStep[i].replace(/\d+/g, (match: any) =>
2066+
String(Number(match) + 1)
2067+
)
20622068
}
20632069
})
20642070
})
@@ -2277,10 +2283,12 @@ function reduceMain(
22772283
)
22782284

22792285
// Fix path highlighting after preserving first statement
2280-
path.forEach(pathStep => {
2286+
path.forEach((pathStep: any[]) => {
22812287
pathStep.forEach((_, i) => {
22822288
if (i == 0) {
2283-
pathStep[i] = pathStep[i].replace(/\d+/g, match => String(Number(match) + 1))
2289+
pathStep[i] = pathStep[i].replace(/\d+/g, (match: any) =>
2290+
String(Number(match) + 1)
2291+
)
22842292
}
22852293
})
22862294
})
@@ -2491,7 +2499,7 @@ function treeifyMain(target: substituterNodes): substituterNodes {
24912499
// has an identifier: replace with the name
24922500
// else: replace with an identifer "=>"
24932501
let verboseCount = 0
2494-
const treeifiers = {
2502+
const treeifiers: Partial<Record<substituterNodes['type'], (target: any) => any>> = {
24952503
// Identifier: return
24962504
ExpressionStatement: (target: es.ExpressionStatement): es.ExpressionStatement => {
24972505
return ast.expressionStatement(treeify(target.expression) as es.Expression)
@@ -2643,7 +2651,7 @@ function jsTreeifyMain(
26432651
// visited before recursing to this target: replace with the name
26442652
// else: replace with a FunctionExpression
26452653
let verboseCount = 0
2646-
const treeifiers = {
2654+
const treeifiers: Partial<Record<substituterNodes['type'], (target: any) => substituterNodes>> = {
26472655
Identifier: (target: es.Identifier): es.Identifier => {
26482656
if (readOnly && target.name.startsWith('anonymous_')) {
26492657
return ast.identifier('[Function]')
@@ -2825,7 +2833,7 @@ function pathifyMain(
28252833
let endIndex = path === undefined ? 0 : path.length - 1
28262834
const redexMarker = ast.identifier('@redex') as substituterNodes
28272835
const withBrackets = ast.identifier('(@redex)') as substituterNodes
2828-
const pathifiers = {
2836+
const pathifiers: Partial<Record<substituterNodes['type'], any>> = {
28292837
ExpressionStatement: (target: es.ExpressionStatement): es.ExpressionStatement => {
28302838
let exp = jsTreeifyMain(target.expression, visited, true) as es.Expression
28312839
if (path[pathIndex] === 'expression') {
@@ -3269,10 +3277,10 @@ function substPredefinedFns(program: es.Program, context: Context): [es.Program,
32693277
}
32703278

32713279
function substPredefinedConstants(program: es.Program): es.Program {
3272-
const constants = [['undefined', undefined]]
3280+
const constants: any[] = [['undefined', undefined]]
32733281
const mathConstants = Object.getOwnPropertyNames(Math)
3274-
.filter(name => typeof Math[name] !== 'function')
3275-
.map(name => ['math_' + name, Math[name]])
3282+
.filter(name => typeof Math[name as keyof typeof Math] !== 'function')
3283+
.map(name => ['math_' + name, Math[name as keyof typeof Math]])
32763284
let substed = program
32773285
for (const nameValuePair of constants.concat(mathConstants)) {
32783286
substed = substituteMain(

src/stepper/util.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as es from 'estree'
22

33
import { Context } from '..'
4+
import { UNKNOWN_LOCATION } from '../constants'
45
import * as errors from '../errors/errors'
56
import { RuntimeSourceError } from '../errors/runtimeSourceError'
67
import { BlockExpression, Environment, Node, substituterNodes, Value } from '../types'
7-
import { UNKNOWN_LOCATION } from '../constants'
88
import * as builtin from './lib'
99

1010
export function prettyPrintError(error: RuntimeSourceError): string {
@@ -17,10 +17,11 @@ export function isBuiltinFunction(node: substituterNodes): boolean {
1717
return (
1818
node.type === 'Identifier' &&
1919
// predeclared, except for evaluateMath
20-
((typeof builtin[node.name] === 'function' && node.name !== 'evaluateMath') ||
20+
((typeof builtin[node.name as keyof typeof builtin] === 'function' &&
21+
node.name !== 'evaluateMath') ||
2122
// one of the math functions
2223
Object.getOwnPropertyNames(Math)
23-
.filter(name => typeof Math[name] === 'function')
24+
.filter(name => typeof Math[name as keyof typeof Math] === 'function')
2425
.map(name => 'math_' + name)
2526
.includes(node.name))
2627
)

src/utils/ast/astToString.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const sourceGen = Object.assign({}, astring.GENERATOR, {
5757
formatComments(state, statement.comments, indent, lineEnd)
5858
}
5959
state.write(indent)
60-
this[statement.type](statement, state)
60+
this[statement.type as keyof typeof this](statement, state)
6161
state.write(lineEnd)
6262
}
6363
state.write(indent)

src/utils/testing/sanitizer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const sanitizers = Object.entries(propertiesToDelete).reduce(
2121
...res,
2222
[nodeType](node: es.Node) {
2323
for (const prop of props) {
24-
delete node[prop]
24+
delete node[prop as keyof typeof node]
2525
}
2626
}
2727
}),

src/vm/svml-compiler.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,17 @@ function compileStatements(
485485
}
486486

487487
// each compiler should return a maxStackSize
488-
const compilers = {
488+
const compilers: Partial<
489+
Record<
490+
Node['type'],
491+
(
492+
node: Node,
493+
indexTable: Map<string, EnvEntry>[],
494+
insertFlag: boolean,
495+
isTailCallPosition?: boolean
496+
) => ReturnType<typeof compileStatements>
497+
>
498+
> = {
489499
// wrapper
490500
Program(node: Node, indexTable: Map<string, EnvEntry>[], insertFlag: boolean) {
491501
node = node as es.Program

src/vm/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ const OPCODES_STR = {
151151

152152
// get name of opcode for debugging
153153
export function getName(op: number) {
154-
return OPCODES_STR[op] // need to add guard in case op does not exist
154+
return OPCODES_STR[op as keyof typeof OPCODES_STR] // need to add guard in case op does not exist
155155
}
156156

157157
// pretty-print the program

0 commit comments

Comments
 (0)