Skip to content

Support renaming properties in in expressions #12

Merged
andrewiggins merged 3 commits intomasterfrom
in-expressions
Jan 22, 2026
Merged

Support renaming properties in in expressions #12
andrewiggins merged 3 commits intomasterfrom
in-expressions

Conversation

@andrewiggins
Copy link
Member

Adds a BinaryExpression visitor that renames string literal properties used with the in operator (e.g., 'foo' in obj'__FOO__' in obj).

This change aims to match Terser's behavior here. Put the following options and example code in the Terser REPL to see it's behavior.

Options

// edit terser options
{
  module: true,
  compress: {},
  mangle: {
    properties: {
      regex: "^_[^_]"
    }
  },
  output: {},
  parse: {},
  rename: {},
  nameCache: {
    props: {
      "cname": 6,
      "props": {
        "$_prop1": "p1"
      }
    }
  }
}

Input

export const obj = {};

// Does transform
console.log("_prop1" in obj);
console.log('_prop1' in obj);
console.log(`_prop1` in obj);

// Does not transform
const s = "_prop1";
console.log(s); // Note: it will transform if you remove this line as it'll inline `s`
console.log(s in obj);
console.log("_prop2" in obj);

Expected Output

Note: newlines added by me for easier reading.

export const obj={};
console.log("p1"in obj),
console.log("p1"in obj),
console.log("p1"in obj);

const o="_prop1";
console.log(o),
console.log(o in obj),
console.log("o"in obj);

andrewiggins and others added 3 commits January 20, 2026 17:20
Adds a BinaryExpression visitor that renames string literal properties
used with the `in` operator (e.g., `'foo' in obj` → `'__FOO__' in obj`).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Extends the BinaryExpression visitor to also rename constant template
literals (e.g., `foo` in obj → `__FOO__` in obj). Non-constant template
literals with expressions are correctly ignored.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Refactor constant template literal handling into reusable helper
functions with JSDoc documentation.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@andrewiggins andrewiggins merged commit 0ab45f9 into master Jan 22, 2026
1 check passed
@andrewiggins andrewiggins deleted the in-expressions branch January 22, 2026 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants