From 01077529d0f6a397915378e69dc4bbbc42e245df Mon Sep 17 00:00:00 2001 From: yihong Date: Sun, 3 Sep 2023 15:51:00 +0800 Subject: [PATCH] fix: `not declared` errors in source native variant REPL (#1479) * fix: only add func and var identifiers instead of all identifiers * fix: add function declarations into `previousProgramIdentifiers` --------- Co-authored-by: Martin Henz --- src/runner/fullJSRunner.ts | 14 +++++++++++--- src/transpiler/transpiler.ts | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/runner/fullJSRunner.ts b/src/runner/fullJSRunner.ts index ea9ad0d16..97f8c3b79 100644 --- a/src/runner/fullJSRunner.ts +++ b/src/runner/fullJSRunner.ts @@ -9,10 +9,15 @@ import { RuntimeSourceError } from '../errors/runtimeSourceError' import { hoistAndMergeImports } from '../localImports/transformers/hoistAndMergeImports' import { getRequireProvider, RequireProvider } from '../modules/requireProvider' import { parse } from '../parser/parser' -import { evallerReplacer, getBuiltins, transpile } from '../transpiler/transpiler' +import { + evallerReplacer, + getBuiltins, + getGloballyDeclaredIdentifiers, + transpile +} from '../transpiler/transpiler' import type { Context, NativeStorage } from '../types' import * as create from '../utils/astCreator' -import { getIdentifiersInProgram } from '../utils/uniqueIds' +import { getFunctionDeclarationNamesInProgram } from '../utils/uniqueIds' import { toSourceError } from './errors' import { appendModulesToContext, resolvedErrorPromise } from './utils' @@ -70,7 +75,10 @@ export async function fullJSRunner( ...preludeAndBuiltins, evallerReplacer(create.identifier(NATIVE_STORAGE_ID), new Set()) ]) - getIdentifiersInProgram(preEvalProgram).forEach(id => + getFunctionDeclarationNamesInProgram(preEvalProgram).forEach(id => + context.nativeStorage.previousProgramsIdentifiers.add(id) + ) + getGloballyDeclaredIdentifiers(preEvalProgram).forEach(id => context.nativeStorage.previousProgramsIdentifiers.add(id) ) const preEvalCode: string = generate(preEvalProgram) diff --git a/src/transpiler/transpiler.ts b/src/transpiler/transpiler.ts index b33bb6b66..9cb8b0796 100644 --- a/src/transpiler/transpiler.ts +++ b/src/transpiler/transpiler.ts @@ -12,6 +12,7 @@ import { ModuleDocumentation } from '../modules/moduleTypes' import { AllowedDeclarations, Chapter, Context, NativeStorage, Variant } from '../types' import * as create from '../utils/astCreator' import { + getFunctionDeclarationNamesInProgram, getIdentifiersInNativeStorage, getIdentifiersInProgram, getUniqueId @@ -678,6 +679,9 @@ function transpileToFullJS( globalIds.native ) + getFunctionDeclarationNamesInProgram(program).forEach(id => + context.nativeStorage.previousProgramsIdentifiers.add(id) + ) getGloballyDeclaredIdentifiers(program).forEach(id => context.nativeStorage.previousProgramsIdentifiers.add(id) )