Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Call constructor only once #4233

Closed
cyberhck opened this issue Oct 18, 2018 · 4 comments
Closed

Call constructor only once #4233

cyberhck opened this issue Oct 18, 2018 · 4 comments

Comments

@cyberhck
Copy link

Feature request

Is your feature request that we implement a new rule?
No

Is your feature request related to a problem? Please describe.
I'm trying to write a new rule which is a bit resource intensive. Thinking my Rule's constructor will get called exactly once while running tslint, I added the resource intensive call on it's constructor like this:

export class Rule extends Lint.Rules.TypedRule {
  constructor(options: Lint.IOptions) {
    super(options);
    console.info(options.ruleArguments); // here's my super resource intensive call
  }

  public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
    return this.applyWithWalker(new TranslationRule(sourceFile, this.getOptions(), program));
  }
}

// tslint:disable-next-line:max-classes-per-file
class TranslationRule extends Lint.RuleWalker {
  constructor(sourceFile: ts.SourceFile, option: Lint.IOptions, private program: ts.Program) {
    super(sourceFile, option);
  }

  public visitCallExpression(node: ts.CallExpression): any {
    super.visitCallExpression(node);
  }
}

When I ran npm run lint on my codebase, it logged it multiple times.

Describe the solution you'd like
Instead of doing new for each file, use the same instance everywhere.

Additional context
Or maybe I'm missing something myself, I'm trying to read another file on constructor using file system, it'd be great if I didn't have to read file a lot of times.

Or did I miss something? Or is there a bootstrap step even before that?

Only thing makes sense is that tslint is spinning multiple threads and running lint rules in each thread.

@JoshuaKGoldberg
Copy link
Contributor

@cyberhck You can try putting a console.log(new Error().stack) in the constructor and an Error.stackTraceLimit = Infinity; somewhere outside the class to see where this is coming from.

spinning multiple threads and running lint rules in each thread.

...I wish! #2328.

How are you running TSLint? Is it on the CLI? IDE extensions tend to run per-file IIRC.

@cyberhck
Copy link
Author

cyberhck commented Nov 6, 2018

Hi, thanks for the response, unfortunately I don't have the code base right now, I will get to it soon and let you know, I'm just doing a simple npm run lint which calls tslint with typescript project thing (I use the stylish option, but I don't think it has anything to do with this)

I'll try to do the console.log soon and let you know :)

@JoshuaKGoldberg
Copy link
Contributor

Ping @cyberhck - did you ever make progress on this?

@cyberhck
Copy link
Author

Sorry, this can be closed as I know tslint is moving to typescript-eslint, I left the company and have no access to the codebase since last December. Sorry.

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

No branches or pull requests

2 participants