Skip to content

Commit 823187b

Browse files
committed
Add context property to error result
1 parent 63f9deb commit 823187b

File tree

16 files changed

+59
-57
lines changed

16 files changed

+59
-57
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default tseslint.config(
2323
import: importPlugin
2424
},
2525
rules: {
26+
'import/first': 'warn',
2627
'import/no-duplicates': ['warn', { 'prefer-inline': true }],
2728
'import/order': 'warn',
2829

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
"bin": "dist/repl/index.js",
3131
"dependencies": {
3232
"@babel/parser": "^7.19.4",
33-
"@commander-js/extra-typings": "^12.0.1",
33+
"@commander-js/extra-typings": "^14.0.0",
3434
"@joeychenofficial/alt-ergo-modified": "^2.4.0",
3535
"@ts-morph/bootstrap": "^0.18.0",
3636
"acorn": "^8.8.2",
3737
"acorn-class-fields": "^1.0.0",
3838
"acorn-loose": "^8.0.0",
3939
"acorn-walk": "^8.0.0",
4040
"astring": "^1.4.3",
41-
"commander": "^12.0.0",
41+
"commander": "^14.0.0",
4242
"js-base64": "^3.7.5",
4343
"lodash": "^4.17.21",
4444
"source-map": "0.7.6"

src/cse-machine/__tests__/cse-machine-unique-id.test.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,28 @@ const getProgramEnv = (context: Context) => {
8989
return env
9090
}
9191

92-
test('Program environment id stays the same regardless of amount of steps', { timeout: 10_000 }, async () => {
93-
const code = stripIndent`
92+
test(
93+
'Program environment id stays the same regardless of amount of steps',
94+
{ timeout: 10_000 },
95+
async () => {
96+
const code = stripIndent`
9497
let x = 0;
9598
for (let i = 0; i < 10; i = i + 1) {
9699
x = [x];
97100
}
98101
`
99102

100-
let programEnvId = '47'
101-
// The above program has a total of 335 steps
102-
// Start from steps = 1 so that the program environment always exists
103-
for (let steps = 1; steps < 336; steps++) {
104-
const context = await getContextFrom(code, steps)
105-
const programEnv = getProgramEnv(context)!
106-
if (programEnv.id !== programEnvId) {
107-
programEnvId = programEnv.id
108-
break
103+
let programEnvId = '47'
104+
// The above program has a total of 335 steps
105+
// Start from steps = 1 so that the program environment always exists
106+
for (let steps = 1; steps < 336; steps++) {
107+
const context = await getContextFrom(code, steps)
108+
const programEnv = getProgramEnv(context)!
109+
if (programEnv.id !== programEnvId) {
110+
programEnvId = programEnv.id
111+
break
112+
}
109113
}
114+
expect(programEnvId).toMatchInlineSnapshot(`"47"`)
110115
}
111-
expect(programEnvId).toMatchInlineSnapshot(`"47"`)
112-
})
116+
)

src/cse-machine/interpreter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,5 +1251,5 @@ const cmdEvaluators: CommandEvaluators = {
12511251
control.push(command.body)
12521252
control.push(instr.popInstr(command.srcNode)) // Pop previous body value
12531253
}
1254-
},
1254+
}
12551255
}

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ import type {
2020
import { Chapter, type Variant } from './langs'
2121
import { assemble } from './vm/svml-assembler'
2222
import { compileToIns } from './vm/svml-compiler'
23-
export { SourceDocumentation } from './editors/ace/docTooltip'
2423

2524
import { CSEResultPromise, resumeEvaluate } from './cse-machine/interpreter'
2625
import { ModuleNotFoundError } from './modules/errors'
2726
import type { ImportOptions } from './modules/moduleTypes'
2827
import preprocessFileImports from './modules/preprocessor'
2928
import { validateFilePath } from './modules/preprocessor/filePaths'
3029
import { getKeywords, getProgramNames, type NameDeclaration } from './name-extractor'
31-
import { htmlRunner, resolvedErrorPromise, sourceFilesRunner } from './runner'
30+
import { htmlRunner, sourceFilesRunner } from './runner'
3231
import { SourceError } from './errors/base'
32+
export { SourceDocumentation } from './editors/ace/docTooltip'
3333

3434
export interface IOptions {
3535
steps: number
@@ -219,7 +219,7 @@ export async function runFilesInContext(
219219
const filePathError = validateFilePath(filePath)
220220
if (filePathError !== null) {
221221
context.errors.push(filePathError)
222-
return resolvedErrorPromise
222+
return { status: 'error', context }
223223
}
224224
}
225225

@@ -228,7 +228,7 @@ export async function runFilesInContext(
228228
const code = files[entrypointFilePath]
229229
if (code === undefined) {
230230
context.errors.push(new ModuleNotFoundError(entrypointFilePath))
231-
return resolvedErrorPromise
231+
return { status: 'error', context }
232232
}
233233
result = await htmlRunner(code, context, options)
234234
} else {

src/modules/loader/__tests__/loader.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('bundle loading', () => {
5858
describe('tab loading', () => {
5959
test("Load a module's tabs", async () => {
6060
mockedDocsImporter.mockResolvedValueOnce({
61-
default: {
61+
default: {
6262
one_module: { tabs: ['tab1', 'tab2'] }
6363
}
6464
})

src/name-extractor/index.ts

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

44
import { partition } from 'lodash'
5-
import type { Context } from '../'
65
import { UNKNOWN_LOCATION } from '../constants'
76
import { findAncestors, findIdentifierNode } from '../finder'
87
import { memoizedGetModuleDocsAsync, memoizedGetModuleManifestAsync } from '../modules/loader'
@@ -11,6 +10,7 @@ import { isSourceModule } from '../modules/utils'
1110
import syntaxBlacklist from '../parser/source/syntax'
1211
import { getImportedName, getModuleDeclarationSource } from '../utils/ast/helpers'
1312
import { isDeclaration, isImportDeclaration, isNamespaceSpecifier } from '../utils/ast/typeGuards'
13+
import type { Context, Node } from '../types'
1414

1515
export enum DeclarationKind {
1616
KIND_IMPORT = 'import',
@@ -20,7 +20,6 @@ export enum DeclarationKind {
2020
KIND_CONST = 'const',
2121
KIND_KEYWORD = 'keyword'
2222
}
23-
import { Node } from '../types'
2423

2524
export interface NameDeclaration {
2625
name: string
@@ -422,7 +421,7 @@ async function getNames(node: Node, locTest: (node: Node) => boolean): Promise<N
422421
}
423422
})
424423
)
425-
} catch (err) {
424+
} catch {
426425
// Failed to load docs for whatever reason
427426
return specs.map(spec => ({
428427
name: spec.local.name,

src/repl/__tests__/main.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, test, expect, vi } from 'vitest'
2-
import type { Command } from 'commander'
2+
import type { Command } from '@commander-js/extra-typings'
33
import { getMainCommand } from '../main'
44

55
vi.spyOn(process, 'exit').mockImplementation(code => {

src/runner/fullJSRunner.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import type { Context, NativeStorage } from '../types'
1515
import * as create from '../utils/ast/astCreator'
1616
import { getFunctionDeclarationNamesInProgram } from '../utils/uniqueIds'
1717
import { toSourceError } from './errors'
18-
import { resolvedErrorPromise } from './utils'
1918
import type { Runner } from './types'
2019

2120
function fullJSEval(code: string, nativeStorage: NativeStorage): any {
@@ -49,7 +48,7 @@ const fullJSRunner: Runner = async (program, context) => {
4948
// only process builtins and preludes if it is a fresh eval context
5049
const prelude = preparePrelude(context)
5150
if (prelude === undefined) {
52-
return resolvedErrorPromise
51+
return { status: 'error', context }
5352
}
5453
const preludeAndBuiltins: es.Statement[] = containsPrevEval(context)
5554
? []
@@ -82,7 +81,7 @@ const fullJSRunner: Runner = async (program, context) => {
8281
context.errors.push(
8382
error instanceof RuntimeSourceError ? error : await toSourceError(error, sourceMapJson)
8483
)
85-
return resolvedErrorPromise
84+
return { status: 'error', context }
8685
}
8786
}
8887

src/runner/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { parse } from '../parser/parser'
1111
import assert from '../utils/assert'
1212
import { defaultAnalysisOptions } from '../modules/preprocessor/analyzer'
1313
import { defaultLinkerOptions } from '../modules/preprocessor/linker'
14-
import { determineExecutionMethod, determineVariant, resolvedErrorPromise } from './utils'
14+
import { determineExecutionMethod, determineVariant } from './utils'
1515
import runners from './sourceRunner'
1616

1717
let previousCode: {
@@ -58,7 +58,10 @@ async function sourceRunner(
5858

5959
validateAndAnnotate(program, context)
6060
if (context.errors.length > 0) {
61-
return resolvedErrorPromise
61+
return {
62+
status: 'error',
63+
context
64+
}
6265
}
6366

6467
if (theOptions.useSubst) {
@@ -77,7 +80,7 @@ async function sourceRunner(
7780
context.unTypecheckedCode.push(context.prelude)
7881

7982
const prelude = parse(context.prelude, context)
80-
if (prelude === null) return resolvedErrorPromise
83+
if (prelude === null) return { status: 'error', context }
8184

8285
await sourceRunner(prelude, context, isVerboseErrorsEnabled, { ...options, isPrelude: true })
8386
}
@@ -122,7 +125,7 @@ export async function sourceFilesRunner(
122125

123126
if (!preprocessResult.ok) {
124127
return {
125-
result: { status: 'error' },
128+
result: { status: 'error', context },
126129
verboseErrors: preprocessResult.verboseErrors
127130
}
128131
}
@@ -181,4 +184,3 @@ export function runCodeInSource(
181184
}
182185

183186
export { htmlRunner } from './htmlRunner'
184-
export { resolvedErrorPromise }

0 commit comments

Comments
 (0)