Skip to content

Object keys written through the proxy cannot be read back when they are not identifiers #187

Open

Description

@NgoQuocViet2001

Environment

magicast 0.5.5 (also main at e388851), Node.js 24

Reproduction

import { parseModule, generateCode } from "magicast";
import { deepMergeObject } from "magicast/helpers";

const mod = parseModule("export default {}");
mod.exports.default.alias = { "@": "./src" };
const alias = mod.exports.default.alias;

alias["@"];          // undefined
Object.keys(alias);  // []
"@" in alias;        // false

alias["@"] = "./app";
generateCode(mod).code;
// export default { alias: { "@": "./src", "@": "./app" } };

const mod2 = parseModule("export default {}");
deepMergeObject(mod2.exports.default, { server: { proxy: { "/api": { target: "http://localhost:3000" } } } });
deepMergeObject(mod2.exports.default, { server: { proxy: { "/api": { changeOrigin: true } } } });
// server.proxy now has two "/api" entries instead of one merged entry

Describe the bug

literalToAst builds object keys that are not valid identifiers with b.literal(key), which gives a Literal key node. getPropName in src/proxy/object.ts only handles Identifier, StringLiteral, NumericLiteral and BooleanLiteral, so get, set, delete, Object.keys and in cannot find those keys. A later write appends a duplicate property.

Expected: alias["@"] is "./src", and assigning it again replaces the value. Actual: undefined, and the generated code has duplicate keys.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions