Skip to content

Commit 4a6845a

Browse files
authored
Use UNKNOWN_LOCATION if node location does not exist (#1378)
1 parent 8b63af3 commit 4a6845a

38 files changed

+136
-106
lines changed

src/ec-evaluator/interpreter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import * as es from 'estree'
1010
import { uniqueId } from 'lodash'
1111

12-
import * as constants from '../constants'
12+
import { UNKNOWN_LOCATION } from '../constants'
1313
import * as errors from '../errors/errors'
1414
import { RuntimeSourceError } from '../errors/runtimeSourceError'
1515
import Closure from '../interpreter/closure'
@@ -629,7 +629,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
629629
// The error could've arisen when the builtin called a source function which errored.
630630
// If the cause was a source error, we don't want to include the error.
631631
// However if the error came from the builtin itself, we need to handle it.
632-
const loc = command.srcNode ? command.srcNode.loc! : constants.UNKNOWN_LOCATION
632+
const loc = command.srcNode.loc ?? UNKNOWN_LOCATION
633633
handleRuntimeError(context, new errors.ExceptionError(error, loc))
634634
}
635635
}

src/errors/errors.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { baseGenerator, generate } from 'astring'
44
import * as es from 'estree'
55

6+
import { UNKNOWN_LOCATION } from '../constants'
67
import { ErrorSeverity, ErrorType, SourceError, Value } from '../types'
78
import { stringify } from '../utils/stringify'
89
import { RuntimeSourceError } from './runtimeSourceError'
@@ -24,8 +25,11 @@ export class InterruptedError extends RuntimeSourceError {
2425
export class ExceptionError implements SourceError {
2526
public type = ErrorType.RUNTIME
2627
public severity = ErrorSeverity.ERROR
28+
public location: es.SourceLocation
2729

28-
constructor(public error: Error, public location: es.SourceLocation) {}
30+
constructor(public error: Error, location?: es.SourceLocation | null) {
31+
this.location = location ?? UNKNOWN_LOCATION
32+
}
2933

3034
public explain() {
3135
return this.error.toString()
@@ -216,7 +220,7 @@ export class GetInheritedPropertyError extends RuntimeSourceError {
216220

217221
constructor(node: es.Node, private obj: Value, private prop: string) {
218222
super(node)
219-
this.location = node.loc!
223+
this.location = node.loc ?? UNKNOWN_LOCATION
220224
}
221225

222226
public explain() {

src/errors/runtimeSourceError.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class RuntimeSourceError implements SourceError {
99
public location: es.SourceLocation
1010

1111
constructor(node?: es.Node) {
12-
this.location = node ? node.loc! : UNKNOWN_LOCATION
12+
this.location = node?.loc ?? UNKNOWN_LOCATION
1313
}
1414

1515
public explain() {

src/errors/typeErrors.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { generate } from 'astring'
22
import * as es from 'estree'
33

4+
import { UNKNOWN_LOCATION } from '../constants'
45
import * as tsEs from '../typeChecker/tsESTree'
56
import { ErrorSeverity, ErrorType, NodeWithInferredType, SArray, SourceError, Type } from '../types'
67
import { simplify, stripIndent } from '../utils/formatters'
@@ -15,7 +16,7 @@ export class InvalidArrayIndexType implements SourceError {
1516
constructor(public node: NodeWithInferredType<es.Node>, public receivedType: Type) {}
1617

1718
get location() {
18-
return this.node.loc!
19+
return this.node.loc ?? UNKNOWN_LOCATION
1920
}
2021

2122
public explain() {
@@ -38,7 +39,7 @@ export class ArrayAssignmentError implements SourceError {
3839
) {}
3940

4041
get location() {
41-
return this.node.loc!
42+
return this.node.loc ?? UNKNOWN_LOCATION
4243
}
4344

4445
public explain() {
@@ -58,7 +59,7 @@ export class ReassignConstError implements SourceError {
5859
constructor(public node: NodeWithInferredType<es.AssignmentExpression>) {}
5960

6061
get location() {
61-
return this.node.loc!
62+
return this.node.loc ?? UNKNOWN_LOCATION
6263
}
6364

6465
public explain() {
@@ -82,7 +83,7 @@ export class DifferentAssignmentError implements SourceError {
8283
) {}
8384

8485
get location() {
85-
return this.node.loc!
86+
return this.node.loc ?? UNKNOWN_LOCATION
8687
}
8788

8889
public explain() {
@@ -115,7 +116,7 @@ export class CyclicReferenceError implements SourceError {
115116
constructor(public node: NodeWithInferredType<es.Node>) {}
116117

117118
get location() {
118-
return this.node.loc!
119+
return this.node.loc ?? UNKNOWN_LOCATION
119120
}
120121

121122
public explain() {
@@ -148,7 +149,7 @@ export class DifferentNumberArgumentsError implements SourceError {
148149
) {}
149150

150151
get location() {
151-
return this.node.loc!
152+
return this.node.loc ?? UNKNOWN_LOCATION
152153
}
153154

154155
public explain() {
@@ -171,7 +172,7 @@ export class InvalidArgumentTypesError implements SourceError {
171172
) {}
172173

173174
get location() {
174-
return this.node.loc!
175+
return this.node.loc ?? UNKNOWN_LOCATION
175176
}
176177

177178
public explain() {
@@ -268,7 +269,7 @@ export class InvalidTestConditionError implements SourceError {
268269
) {}
269270

270271
get location() {
271-
return this.node.loc!
272+
return this.node.loc ?? UNKNOWN_LOCATION
272273
}
273274

274275
public explain() {
@@ -293,7 +294,7 @@ export class UndefinedIdentifierError implements SourceError {
293294
constructor(public node: NodeWithInferredType<es.Identifier>, public name: string) {}
294295

295296
get location() {
296-
return this.node.loc!
297+
return this.node.loc ?? UNKNOWN_LOCATION
297298
}
298299

299300
public explain() {
@@ -320,7 +321,7 @@ export class ConsequentAlternateMismatchError implements SourceError {
320321
) {}
321322

322323
get location() {
323-
return this.node.loc!
324+
return this.node.loc ?? UNKNOWN_LOCATION
324325
}
325326

326327
public explain() {
@@ -348,7 +349,7 @@ export class CallingNonFunctionType implements SourceError {
348349
constructor(public node: NodeWithInferredType<es.CallExpression>, public callerType: Type) {}
349350

350351
get location() {
351-
return this.node.loc!
352+
return this.node.loc ?? UNKNOWN_LOCATION
352353
}
353354

354355
public explain() {
@@ -379,7 +380,7 @@ export class InconsistentPredicateTestError implements SourceError {
379380
) {}
380381

381382
get location() {
382-
return this.node.loc!
383+
return this.node.loc ?? UNKNOWN_LOCATION
383384
}
384385

385386
public explain() {
@@ -413,7 +414,7 @@ export class TypeMismatchError implements SourceError {
413414
) {}
414415

415416
get location() {
416-
return this.node.loc!
417+
return this.node.loc ?? UNKNOWN_LOCATION
417418
}
418419

419420
public explain() {
@@ -432,7 +433,7 @@ export class TypeNotFoundError implements SourceError {
432433
constructor(public node: tsEs.Node, public name: string) {}
433434

434435
get location() {
435-
return this.node.loc!
436+
return this.node.loc ?? UNKNOWN_LOCATION
436437
}
437438

438439
public explain() {
@@ -451,7 +452,7 @@ export class FunctionShouldHaveReturnValueError implements SourceError {
451452
constructor(public node: tsEs.FunctionDeclaration | tsEs.ArrowFunctionExpression) {}
452453

453454
get location() {
454-
return this.node.loc!
455+
return this.node.loc ?? UNKNOWN_LOCATION
455456
}
456457

457458
public explain() {
@@ -470,7 +471,7 @@ export class TypeNotCallableError implements SourceError {
470471
constructor(public node: tsEs.CallExpression, public typeName: string) {}
471472

472473
get location() {
473-
return this.node.loc!
474+
return this.node.loc ?? UNKNOWN_LOCATION
474475
}
475476

476477
public explain() {
@@ -493,7 +494,7 @@ export class TypecastError implements SourceError {
493494
) {}
494495

495496
get location() {
496-
return this.node.loc!
497+
return this.node.loc ?? UNKNOWN_LOCATION
497498
}
498499

499500
public explain() {
@@ -512,7 +513,7 @@ export class TypeNotAllowedError implements SourceError {
512513
constructor(public node: tsEs.TSType, public name: string) {}
513514

514515
get location() {
515-
return this.node.loc!
516+
return this.node.loc ?? UNKNOWN_LOCATION
516517
}
517518

518519
public explain() {
@@ -531,7 +532,7 @@ export class UndefinedVariableTypeError implements SourceError {
531532
constructor(public node: tsEs.Node, public name: string) {}
532533

533534
get location() {
534-
return this.node.loc!
535+
return this.node.loc ?? UNKNOWN_LOCATION
535536
}
536537

537538
public explain() {
@@ -558,7 +559,7 @@ export class InvalidNumberOfArgumentsTypeError implements SourceError {
558559
}
559560

560561
get location() {
561-
return this.node.loc!
562+
return this.node.loc ?? UNKNOWN_LOCATION
562563
}
563564

564565
public explain() {
@@ -582,7 +583,7 @@ export class InvalidNumberOfTypeArgumentsForGenericTypeError implements SourceEr
582583
constructor(public node: tsEs.Node, public name: string, public expected: number) {}
583584

584585
get location() {
585-
return this.node.loc!
586+
return this.node.loc ?? UNKNOWN_LOCATION
586587
}
587588

588589
public explain() {
@@ -601,7 +602,7 @@ export class TypeNotGenericError implements SourceError {
601602
constructor(public node: tsEs.Node, public name: string) {}
602603

603604
get location() {
604-
return this.node.loc!
605+
return this.node.loc ?? UNKNOWN_LOCATION
605606
}
606607

607608
public explain() {
@@ -620,7 +621,7 @@ export class TypeAliasNameNotAllowedError implements SourceError {
620621
constructor(public node: tsEs.TSTypeAliasDeclaration, public name: string) {}
621622

622623
get location() {
623-
return this.node.loc!
624+
return this.node.loc ?? UNKNOWN_LOCATION
624625
}
625626

626627
public explain() {
@@ -639,7 +640,7 @@ export class TypeParameterNameNotAllowedError implements SourceError {
639640
constructor(public node: tsEs.TSTypeParameter, public name: string) {}
640641

641642
get location() {
642-
return this.node.loc!
643+
return this.node.loc ?? UNKNOWN_LOCATION
643644
}
644645

645646
public explain() {
@@ -658,7 +659,7 @@ export class InvalidIndexTypeError implements SourceError {
658659
constructor(public node: tsEs.MemberExpression, public typeName: string) {}
659660

660661
get location() {
661-
return this.node.loc!
662+
return this.node.loc ?? UNKNOWN_LOCATION
662663
}
663664

664665
public explain() {
@@ -677,7 +678,7 @@ export class InvalidArrayAccessTypeError implements SourceError {
677678
constructor(public node: tsEs.MemberExpression, public typeName: string) {}
678679

679680
get location() {
680-
return this.node.loc!
681+
return this.node.loc ?? UNKNOWN_LOCATION
681682
}
682683

683684
public explain() {
@@ -696,7 +697,7 @@ export class ConstNotAssignableTypeError implements SourceError {
696697
constructor(public node: tsEs.AssignmentExpression, public name: string) {}
697698

698699
get location() {
699-
return this.node.loc!
700+
return this.node.loc ?? UNKNOWN_LOCATION
700701
}
701702

702703
public explain() {
@@ -715,7 +716,7 @@ export class DuplicateTypeAliasError implements SourceError {
715716
constructor(public node: tsEs.TSTypeAliasDeclaration, public name: string) {}
716717

717718
get location() {
718-
return this.node.loc!
719+
return this.node.loc ?? UNKNOWN_LOCATION
719720
}
720721

721722
public explain() {

src/errors/validityErrors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as es from 'estree'
22

3+
import { UNKNOWN_LOCATION } from '../constants'
34
import { ErrorSeverity, ErrorType, SourceError } from '../types'
45

56
export class NoAssignmentToForVariable implements SourceError {
@@ -9,7 +10,7 @@ export class NoAssignmentToForVariable implements SourceError {
910
constructor(public node: es.AssignmentExpression) {}
1011

1112
get location() {
12-
return this.node.loc!
13+
return this.node.loc ?? UNKNOWN_LOCATION
1314
}
1415

1516
public explain() {

src/gpu/transfomer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class GPUTransformer {
155155
kernelFunction,
156156
create.literal(currentKernelId++)
157157
],
158-
node.loc!
158+
node.loc
159159
)
160160

161161
create.mutateToExpressionStatement(node, createKernelSourceCall)

src/interpreter/closure.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export default class Closure extends Callable {
6060
return body.type !== 'BlockStatement'
6161
}
6262
const functionBody = isExpressionBody(node.body)
63-
? [returnStatement(node.body, node.body.loc!)]
63+
? [returnStatement(node.body, node.body.loc)]
6464
: dummyReturn
6565
? [node.body, returnStatement(identifier('undefined', dummyLocation()), dummyLocation())]
6666
: node.body
6767
const closure = new Closure(
68-
blockArrowFunction(node.params as es.Identifier[], functionBody, node.loc!),
68+
blockArrowFunction(node.params as es.Identifier[], functionBody, node.loc),
6969
environment,
7070
context
7171
)

src/interpreter/interpreter-non-det.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import * as es from 'estree'
33
import { cloneDeep, uniqueId } from 'lodash'
44

5-
import * as constants from '../constants'
6-
import { CUT } from '../constants'
5+
import { CUT, UNKNOWN_LOCATION } from '../constants'
76
import * as errors from '../errors/errors'
87
import { RuntimeSourceError } from '../errors/runtimeSourceError'
98
import { Context, Environment, Frame, Value } from '../types'
@@ -252,9 +251,9 @@ function* getAmbArgs(context: Context, call: es.CallExpression) {
252251

253252
function transformLogicalExpression(node: es.LogicalExpression): es.ConditionalExpression {
254253
if (node.operator === '&&') {
255-
return conditionalExpression(node.left, node.right, literal(false), node.loc!)
254+
return conditionalExpression(node.left, node.right, literal(false), node.loc)
256255
} else {
257-
return conditionalExpression(node.left, literal(true), node.right, node.loc!)
256+
return conditionalExpression(node.left, literal(true), node.right, node.loc)
258257
}
259258
}
260259

@@ -679,7 +678,7 @@ export function* apply(
679678
-context.numberOfOuterEnvironments
680679
)
681680

682-
const loc = node ? node.loc! : constants.UNKNOWN_LOCATION
681+
const loc = node.loc ?? UNKNOWN_LOCATION
683682
if (!(e instanceof RuntimeSourceError || e instanceof errors.ExceptionError)) {
684683
// The error could've arisen when the builtin called a source function which errored.
685684
// If the cause was a source error, we don't want to include the error.

0 commit comments

Comments
 (0)