Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacer doesn't work for nested values #27

Closed
Akryum opened this issue Nov 4, 2019 · 3 comments
Closed

Replacer doesn't work for nested values #27

Akryum opened this issue Nov 4, 2019 · 3 comments
Labels

Comments

@Akryum
Copy link

Akryum commented Nov 4, 2019

2.0.0 has a regression compared to 1.6.0. The replacer function doesn't seem to work on nested values, only on root-level ones.

Reproduction

This example code doesn't produce the desired output:

import { stringify } from "javascript-stringify";

function makeRaw(str) {
  const fn = () => {};
  fn.__expression = str;
  return fn;
}

function str(value) {
  return stringify(
    value,
    (val, indent, stringify) => {
      if (val && val.__expression) {
        return val.__expression;
      }
      return stringify(val);
    },
    2
  );
}

console.log(str({
  "no-console": makeRaw(`process.env.NODE_ENV === 'production' ? 'error' : 'off'`),
  "no-debugger": makeRaw(`process.env.NODE_ENV === 'production' ? 'error' : 'off'`)
}))

It outputs () => {} instead of process.env.NODE_ENV === 'production' ? 'error' : 'off' for example.

@Akryum
Copy link
Author

Akryum commented Nov 4, 2019

cc @sodatea

@blakeembrey
Copy link
Owner

Thanks for the flag. It should be possible to fix this, it's unfortunately caused by a lot of new ways to write functions in ES2015 which resulted in having to write a custom function replacer that only worked with the object key.

@blakeembrey blakeembrey added the bug label Nov 4, 2019
@blakeembrey
Copy link
Owner

Fixed with 6ce8383 and released as 2.0.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants