typescript-eslint: [restrict-plus-operands] False positive reported when adding two numbers

Repro

{
  "rules": {
    "@typescript-eslint/restrict-plus-operands": "error"
  }
}
namespace Other {
  export abstract class ILegend
  {
    protected legendWidth: number = 244;

    public get width(): number { return this.legendWidth; }
  }

  export class Canvas
  {
    protected width: number;
  }

  export class FoLegend extends ILegend
  {
  }

  export class Legend extends ILegend
  {
  }


  export abstract class Chart extends Canvas
  {
    protected static readonly svgcMargin: number = 10;

    protected legend: ILegend;

    constructor(foreignObjectSupported: boolean)
    {
      super();

      if (foreignObjectSupported)
        this.legend = new FoLegend();
      else
        this.legend = new Legend();
    }

    // This is the line it should error on.
    protected get legendXPosition(): number { return this.width - (Chart.svgcMargin + this.legend.width); }
  }

  export class PieChart extends Chart
  {
    public getAvailableWidth(): number
    {
      // This is the line it should error on.
      return this.width - (this.legend.width + Chart.svgcMargin);
    }
  }

  export class BarChart extends Chart
  {
    public getAvailableWidth(): number
    {
      // This is the line it should error on.
      return this.width - (this.legend.width + Chart.svgcMargin);
    }
  }

  export class PointChart extends Chart
  {
    public getAvailableWidth(): number
    {
      // This is the line it should error on.
      return this.width - (this.legend.width + Chart.svgcMargin);
    }
  }
}

Expected Result No error

Actual Result this.legend.width + Chart.svgcMargin reports a false positive

Versions

package version
@typescript-eslint/eslint-plugin 1.10.2
@typescript-eslint/parser 1.10.2
TypeScript 3.4.3
ESLint 5.16.0

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 22 (18 by maintainers)

Most upvoted comments

No problem 😃 I will start a PR for it then.

We should also mention here that project can accept an array of strings (and a glob pattern when the PR get’s merged). I went hunting for the projects option and now I find it’s actually an internal option. lol

@uniqueiniquity I got a few more questions:

Does the parser have any IO functionality? I have looked at the source and it doesn’t seem to be the case. If the current implementation is not dependent on IO, I think the glob stuff should only be implemented for the parseForESLint function https://github.com/typescript-eslint/typescript-eslint/blob/39e41b56ca26047cc1a1fdf7330e0bee928dc720/packages/parser/src/parser.ts#L51


Does parseForESLint run on every file? I think so because of the code arg but not sure. If it does run on every file, I think from a performance standpoint it’s necessary to cache the output of the glob function (it’s IO bound so it adds overhead)


On a side-note, I am planning to use is-glob to find out if parserOptions.project is a glob pattern or not. I imagine the majority of people don’t need to provide globs so this should take care of that.

Regarding this, the context was that I wanted to convert my eslint config from JS to YAML but I can’t because of the runtime component (the glob that searches for all tsconfigs). Passing a glob would solve this.

Sorry, I wasn’t clear - I fully support it taking a glob 😃