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

Babel compile html-entities@1.2.1 code generate invalid JavaScript code #11338

Closed
1 task
copyrighthero opened this issue Mar 26, 2020 · 1 comment · Fixed by #11339
Closed
1 task

Babel compile html-entities@1.2.1 code generate invalid JavaScript code #11338

copyrighthero opened this issue Mar 26, 2020 · 1 comment · Fixed by #11339
Assignees
Labels
i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@copyrighthero
Copy link

copyrighthero commented Mar 26, 2020

Bug Report

  • I would like to work on a fix!

Current Behavior

When using babel to compile html-entities from node_modules, specifically when using the the following environment and configuration file, using the command babel --config-file ./babel.config.js node_modules/html-entities/lib/html5-entities.js > ~/Desktop/html5-entities.js, the generated code is invalid.

Original source code:

Html5Entities.prototype.encode = function(str) {
    if (!str || !str.length) {
        return '';
    }
    var strLength = str.length;
    var result = '';
    var i = 0;
    while (i < strLength) {
        var charInfo = charIndex[str.charCodeAt(i)];
        if (charInfo) {
            var alpha = charInfo[str.charCodeAt(i + 1)];
            if (alpha) {
                i++;
            } else {
                alpha = charInfo[''];
            }
            if (alpha) {
                result += "&" + alpha + ";";
                i++;
                continue;
            }
        }
        result += str.charAt(i);
        i++;
    }
    return result;
};

Generated source code

31}, Html5Entities.prototype.encode = function (str) {
...
40      if (alpha ? i++ : alpha = charInfo., alpha) { // <- this line
41        result += "&" + alpha + ";", i++;
42        continue;
43      }

Input Code

  • package.json dependencies:
{
    "@babel/cli": "^7.8.4",
    "@babel/core": "^7.9.0",
    "@babel/preset-env": "^7.9.0",
    "@babel/preset-react": "^7.9.4",
    "@babel/preset-typescript": "^7.9.0",
    "babel-eslint": "^10.1.0",
    "babel-jest": "^25.2.1",
    "babel-loader": "^8.1.0",
    "babel-preset-minify": "^0.5.1",
    ...
}

Expected behavior/code
Should not convert subscription format into dot format when the key is an empty string.

charInfo[''] // => charInfo[''] instead of charInfo.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
DEBUG = true;

module.exports = {
  // codegen options
  compact: !DEBUG,
  minified: !DEBUG,
  comments: DEBUG,
  sourceMaps: DEBUG,
  auxiliaryCommentBefore: '',
  auxiliaryCommentAfter: '',
  plugins: [],
  presets: [
    ['minify', {
      mangle: false,
      builtIns: false,
      removeConsole: false,
      removeDebugger: false,
    }],
    ['@babel/preset-env', {
      spec: false,
      modules: 'auto',
      targets: {
        node: true,
        browsers: [
          'last 2 chrome versions',
          'last 2 firefox versions',
        ],
      },
      useBuiltIns: false,
    }],
    ['@babel/preset-typescript', {
      isTSX: true,
      jsxPragma: 'React',
      allExtensions: true,
    }],
    ['@babel/preset-react', {
      pragma: 'React.createElement',
      pragmaFrag: 'React.Fragment',
      useBuiltIns: false,
      throwIfNamespace: true,
    }],
  ],
};

Environment

  System:
    OS: macOS 10.15.3
  Binaries:
    Node: 12.16.1 - ~/.nvm/versions/node/v12.16.1/bin/node
    npm: 6.14.3 - ~/.nvm/versions/node/v12.16.1/bin/npm
  npmPackages:
    @babel/cli: ^7.8.4 => 7.8.4 
    @babel/core: ^7.9.0 => 7.9.0 
    @babel/preset-env: ^7.9.0 => 7.9.0 
    @babel/preset-react: ^7.9.4 => 7.9.4 
    @babel/preset-typescript: ^7.9.0 => 7.9.0 
    babel-eslint: ^10.1.0 => 10.1.0 
    babel-jest: ^25.2.1 => 25.2.1 
    babel-loader: ^8.1.0 => 8.1.0 
    babel-preset-minify: ^0.5.1 => 0.5.1 
    eslint: ^6.8.0 => 6.8.0 
    jest: ^25.2.2 => 25.2.2 
    webpack: ^4.42.1 => 4.42.1 

  • How you are using Babel: cli, babel-loader, both displays the same result
    Possible Solution
    Determine if key to an object is an empty string, if it is then it should not be converted to . (dot notation).
charInfo[''] // => charInfo[''] instead of charInfo.

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

@babel-bot
Copy link
Collaborator

Hey @copyrighthero! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@JLHwung JLHwung self-assigned this Mar 26, 2020
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 26, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants