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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: property access on negative number isn't preserved as negative number in the output #15801

Open
1 task
Andarist opened this issue Jul 20, 2023 · 3 comments 路 Fixed by #15802
Open
1 task

Comments

@Andarist
Copy link
Member

Andarist commented Jul 20, 2023

馃捇

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

const result = require("@babel/core").transformSync(
  `
  export default 'hello';
`,
  {
    plugins: [
      function (babel) {
        const { types: t } = babel;

        return {
          visitor: {
            StringLiteral(path) {
              path.replaceWith(
                t.unaryExpression(
                  "typeof",
                  t.callExpression(
                    t.memberExpression(
                      t.numericLiteral(-1),
                      t.identifier("toString")
                    ),
                    []
                  )
                )
              );
            },
          },
        };
      },
    ],
  }
);

console.log(result.code);

Configuration file name

No response

Configuration

none

Current and expected behavior

current

export default typeof -1 .toString(); // evaluated as 'number'

expected

export default typeof (-1).toString(); // evaluated as 'string'

Environment

System:
OS: macOS 12.3
Binaries:
Node: 20.2.0 - ~/Library/Caches/fnm_multishells/2251_1687933194251/bin/node
Yarn: 1.22.17 - /usr/local/bin/yarn
npm: 9.6.6 - ~/Library/Caches/fnm_multishells/2251_1687933194251/bin/npm
pnpm: 7.27.1 - ~/Library/pnpm/pnpm
npmPackages:
@babel/core: ^7.22.9 => 7.22.9

Possible solution

wrap negative numbers in member expressions with parentheses

Additional context

No response

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Jul 20, 2023

-1 is a t.unaryExpression("-", t.numericLiteral(1)), not a t.numericLiteral(-1). JavaScript does not have literals for negative numbers.

https://astexplorer.net/#/gist/6861e112ab5faa5d7b0f4078b627c29a/ac7ca72bc22445946003404a371fbb51b1a6d5e6

We should add some validation to the numericLiteral function.

@JLHwung
Copy link
Contributor

JLHwung commented Jul 21, 2023

Should we also apply the non-negative restriction to t.bigIntLiteral and t.decimalLiteral? Though the value accepts string only, but we can always check if it starts with -.

@nicolo-ribaudo
Copy link
Member

Maybe we could also start requiring the number to be a bigint rather than a string (in babel 8), given that it's now supported everywhere.

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

Successfully merging a pull request may close this issue.

3 participants