js-compiler: Add frozen option to freeze AST modifications
MODIFY
packages/@romejs/js-compiler/lib/Path.ts
packages/@romejs/js-compiler/lib/Path.ts
@@ -27,6 +27,7 @@ export type PathOptions = {
listKey?: number;
parentScope?: Scope;
scope?: Scope;
+ frozen?: boolean;
noArrays?: boolean;
noScopeCreation?: boolean;
hooks?: Handlers;

MODIFY
packages/@romejs/js-compiler/methods/reduce.ts
packages/@romejs/js-compiler/methods/reduce.ts
@@ -195,6 +195,7 @@ export default function reduce(
// Run transforms on this node
const newChild = reduce(child, visitors, context, {
noScopeCreation: pathOpts.noScopeCreation,
+ frozen: pathOpts.frozen,
parentScope: path.scope,
ancestryPaths: childAncestryPaths,
listKey: correctedIndex,
@@ -202,7 +203,7 @@ export default function reduce(
});
// If this item has been changed then...
- if (newChild !== child) {
+ if (newChild !== child && !pathOpts.frozen) {
// Clone the children array
children = children.slice();
@@ -262,12 +263,13 @@ export default function reduce(
parentScope: path.scope,
ancestryPaths: childAncestryPaths,
noArrays: true,
+ frozen: pathOpts.frozen,
nodeKey: key,
},
);
// If this value has been changed then...
- if (newVal !== oldVal) {
+ if (newVal !== oldVal && !pathOpts.frozen) {
// When replacing a key value, we cannot replace it with an array
if (Array.isArray(newVal)) {
throw new Error(

js-compiler: Add frozen option to freeze AST modifications