Skip to content

Commit

Permalink
fix(plugin/replace): fix object_guards option (#2322)
Browse files Browse the repository at this point in the history
### Description


[@rollup/plugin-replace](https://www.npmjs.com/package/@rollup/plugin-replace)'s
docs give this example for `objectGuards` option:

```js
replace({
  values: {
    'process.env.NODE_ENV': '"production"'
  }
});
```
```js
// Input
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') {
  console.log('production');
}
// Without `objectGuards`
if (typeof process !== 'undefined' && 'production' === 'production') {
  console.log('production');
}
// With `objectGuards`
if ('object' !== 'undefined' && 'production' === 'production') {
  console.log('production');
}
```

Rolldown's version was not following this behavior. The example above
was transformed to:

```js
if (typeof process !== "undefined" && true) {
  console.log("production");
}
```

This PR fixes that. The implementation now is almost identical to
[Rollup's
implementation](https://github.com/rollup/plugins/blob/master/packages/replace/src/index.js).
  • Loading branch information
overlookmotel authored Sep 25, 2024
1 parent 84cb2a2 commit 3899128
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 109 deletions.
126 changes: 18 additions & 108 deletions crates/rolldown_plugin_replace/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,10 @@ pub(crate) fn expand_typeof_replacements(
for key in values.keys() {
if OBJECT_RE.is_match(key) {
// Skip last part
let mut parts = key.split('.');
let mut part = parts.next().unwrap();
for next in parts {
replacements.extend([
(format!("typeof {part} ==="), "\"object\" ===".to_string()),
(format!("typeof {part}==="), "\"object\"===".to_string()),
(format!("typeof {part} !=="), "\"object\" !==".to_string()),
(format!("typeof {part}!=="), "\"object\"!==".to_string()),
(format!("typeof {part} =="), "\"object\" ===".to_string()),
(format!("typeof {part}=="), "\"object\"===".to_string()),
(format!("typeof {part}!="), "\"object\"!==".to_string()),
(format!("typeof {part} !="), "\"object\" !==".to_string()),
]);
part = next;
}
replacements.extend(key.match_indices('.').map(|(index, _)| {
let match_str = &key[..index];
(format!("typeof {match_str}"), "\"object\"".to_string())
}));
};
}

Expand Down Expand Up @@ -58,97 +47,38 @@ mod tests {
run_test(&["a"], &[]);
run_test(&["abc"], &[]);

run_test(&["abc.def"], &[("typeof abc", "\"object\"")]);

run_test(
&["abc.def"],
&[
("typeof abc===", "\"object\"==="),
("typeof abc ===", "\"object\" ==="),
("typeof abc==", "\"object\"==="),
("typeof abc ==", "\"object\" ==="),
("typeof abc!==", "\"object\"!=="),
("typeof abc !==", "\"object\" !=="),
("typeof abc!=", "\"object\"!=="),
("typeof abc !=", "\"object\" !=="),
],
&["process.env.NODE_ENV"],
&[("typeof process", "\"object\""), ("typeof process.env", "\"object\"")],
);

run_test(
&["a.b.c.d"],
&[
("typeof a===", "\"object\"==="),
("typeof a ===", "\"object\" ==="),
("typeof a==", "\"object\"==="),
("typeof a ==", "\"object\" ==="),
("typeof a!==", "\"object\"!=="),
("typeof a !==", "\"object\" !=="),
("typeof a!=", "\"object\"!=="),
("typeof a !=", "\"object\" !=="),
("typeof b===", "\"object\"==="),
("typeof b ===", "\"object\" ==="),
("typeof b==", "\"object\"==="),
("typeof b ==", "\"object\" ==="),
("typeof b!==", "\"object\"!=="),
("typeof b !==", "\"object\" !=="),
("typeof b!=", "\"object\"!=="),
("typeof b !=", "\"object\" !=="),
("typeof c===", "\"object\"==="),
("typeof c ===", "\"object\" ==="),
("typeof c==", "\"object\"==="),
("typeof c ==", "\"object\" ==="),
("typeof c!==", "\"object\"!=="),
("typeof c !==", "\"object\" !=="),
("typeof c!=", "\"object\"!=="),
("typeof c !=", "\"object\" !=="),
],
&[("typeof a", "\"object\""), ("typeof a.b", "\"object\""), ("typeof a.b.c", "\"object\"")],
);
}

#[test]
fn test_expand_unicode() {
run_test(
&["कुत्तेपरपानी.पतलूनमेंआग.मेरेशॉर्ट्सखाओ"],
&[
("typeof कुत्तेपरपानी===", "\"object\"==="),
("typeof कुत्तेपरपानी ===", "\"object\" ==="),
("typeof कुत्तेपरपानी==", "\"object\"==="),
("typeof कुत्तेपरपानी ==", "\"object\" ==="),
("typeof कुत्तेपरपानी!==", "\"object\"!=="),
("typeof कुत्तेपरपानी !==", "\"object\" !=="),
("typeof कुत्तेपरपानी!=", "\"object\"!=="),
("typeof कुत्तेपरपानी !=", "\"object\" !=="),
("typeof पतलूनमेंआग===", "\"object\"==="),
("typeof पतलूनमेंआग ===", "\"object\" ==="),
("typeof पतलूनमेंआग==", "\"object\"==="),
("typeof पतलूनमेंआग ==", "\"object\" ==="),
("typeof पतलूनमेंआग!==", "\"object\"!=="),
("typeof पतलूनमेंआग !==", "\"object\" !=="),
("typeof पतलूनमेंआग!=", "\"object\"!=="),
("typeof पतलूनमेंआग !=", "\"object\" !=="),
],
&[("typeof कुत्तेपरपानी", "\"object\""), ("typeof कुत्तेपरपानी.पतलूनमेंआग", "\"object\"")],
);
}

#[test]
fn test_expand_multiple() {
run_test(
&["a.x", "b.y"],
&["a.x", "b.y", "c.z", "d.e.f", "g.h"],
&[
("typeof a===", "\"object\"==="),
("typeof a ===", "\"object\" ==="),
("typeof a==", "\"object\"==="),
("typeof a ==", "\"object\" ==="),
("typeof a!==", "\"object\"!=="),
("typeof a !==", "\"object\" !=="),
("typeof a!=", "\"object\"!=="),
("typeof a !=", "\"object\" !=="),
("typeof b===", "\"object\"==="),
("typeof b ===", "\"object\" ==="),
("typeof b==", "\"object\"==="),
("typeof b ==", "\"object\" ==="),
("typeof b!==", "\"object\"!=="),
("typeof b !==", "\"object\" !=="),
("typeof b!=", "\"object\"!=="),
("typeof b !=", "\"object\" !=="),
("typeof a", "\"object\""),
("typeof b", "\"object\""),
("typeof c", "\"object\""),
("typeof d", "\"object\""),
("typeof d.e", "\"object\""),
("typeof g", "\"object\""),
],
);
}
Expand All @@ -171,26 +101,6 @@ mod tests {
run_test(&["a.b.cde!"], &[]);
run_test(&["a.b.c.d!e"], &[]);

run_test(
&["a.x", "!", "b.y"],
&[
("typeof a===", "\"object\"==="),
("typeof a ===", "\"object\" ==="),
("typeof a==", "\"object\"==="),
("typeof a ==", "\"object\" ==="),
("typeof a!==", "\"object\"!=="),
("typeof a !==", "\"object\" !=="),
("typeof a!=", "\"object\"!=="),
("typeof a !=", "\"object\" !=="),
("typeof b===", "\"object\"==="),
("typeof b ===", "\"object\" ==="),
("typeof b==", "\"object\"==="),
("typeof b ==", "\"object\" ==="),
("typeof b!==", "\"object\"!=="),
("typeof b !==", "\"object\" !=="),
("typeof b!=", "\"object\"!=="),
("typeof b !=", "\"object\" !=="),
],
);
run_test(&["a.x", "!", "b.y"], &[("typeof a", "\"object\""), ("typeof b", "\"object\"")]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ source: crates/rolldown_testing/src/integration_test.rs
```js
//#region input.js
if (typeof process !== "undefined" && typeof process.env === "object" && true) {
{
console.log("production");
}
Expand Down

0 comments on commit 3899128

Please sign in to comment.