Eslint-plugin-react: rules/no-unescaped-entities: `isInvalidEntity` never returns a truthy value, so there is a piece of dead code.

Created on 20 May 2017  路  2Comments  路  Source: yannickcr/eslint-plugin-react

https://github.com/yannickcr/eslint-plugin-react/blob/f0dcaca92418bb791f585e867d720a96dd43a277/lib/rules/no-unescaped-entities.js#L73

  create: function(context) {
    function isInvalidEntity(node) {
      var configuration = context.options[0] || {};
      var entities = configuration.forbid || DEFAULTS;

      // HTML entites are already escaped in node.value (as well as node.raw),
      // so pull the raw text from context.getSourceCode()
      for (var i = node.loc.start.line; i <= node.loc.end.line; i++) {
        var rawLine = context.getSourceCode().lines[i - 1];
        var start = 0;
        var end = rawLine.length;
        if (i === node.loc.start.line) {
          start = node.loc.start.column;
        }
        if (i === node.loc.end.line) {
          end = node.loc.end.column;
        }
        rawLine = rawLine.substring(start, end);
        for (var j = 0; j < entities.length; j++) {
          for (var index = 0; index < rawLine.length; index++) {
            var c = rawLine[index];
            if (c === entities[j]) {
              context.report({
                loc: {line: i, column: start + index},
                message: 'HTML entities must be escaped.',
                node: node
              });
            }
          }
        }
      }
    }

    return {
      Literal: function(node) {
        if (node.type === 'Literal' && node.parent.type === 'JSXElement') {
          if (isInvalidEntity(node)) {
            context.report(node, 'HTML entities must be escaped.');
          }
        }
      }
    };

This condition always evaluates to false:

          if (isInvalidEntity(node)) {
            context.report(node, 'HTML entities must be escaped.');
          }

Likely, the original intent was to return true or false from isInvalidEntity, but then the code was rewritten so that isInvalidEntity reports errors on its own. So now the more correct name for it would be reportInvalidEntity function than isInvalidEntity. And, of course, the no-op if block should be removed.

help wanted

Most helpful comment

Fixed in #1227

All 2 comments

Good catch!

Fixed in #1227

Was this page helpful?
0 / 5 - 0 ratings