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

Support "project" in "Linter.lint" API #4161

Closed
bennycode opened this issue Sep 9, 2018 · 2 comments
Closed

Support "project" in "Linter.lint" API #4161

bennycode opened this issue Sep 9, 2018 · 2 comments

Comments

@bennycode
Copy link
Contributor

bennycode commented Sep 9, 2018

Feature request

Is your feature request related to a problem? Please describe.
I want to run TSLint from a Gulp task. Therefor I am using TSLint as a Node.js library. Unfortunately I could not find out how to pass a tsconfig.json file (--project) to the Linter to determine which files will be linted. The Linter.lint method only seems to support a configuration.

Describe the solution you'd like
I need an API equivalent to tslint --project tsconfig.json --config tslint.json.

The signature of the current lint method looks like this:

lint(fileName, source, configuration)

It could be overloaded to:

lint(project, configuration)

Another good approach could be this:

const linter = new tslint.Linter({
  project: 'tsconfig.json',
  config: 'tslint.json',
  fix: false
});

Describe alternatives you've considered
At the moment I am doing the following to achieve my tasks (but it involves a huge chunk of code because of the current API all files must be traversed by hand):

const tslint = require('tslint');
const gutil = {
  PluginError: require('plugin-error'),
};

gulp.task('lint:ts', done => {
  const program = tslint.Linter.createProgram(`${process.cwd()}/tsconfig.json`);
  const linter = new tslint.Linter({
    fix: false,
    formatter: "prose",
  }, program);

  const files = tslint.Linter.getFileNames(program);
  files.forEach(fileName => {
    const fileContents = program.getSourceFile(fileName).getFullText();
    const configuration = tslint.Configuration.findConfiguration(`${process.cwd()}/tslint.json`, fileName).results;
    linter.lint(fileName, fileContents, configuration);
  });

  const results = linter.getResult();
  if (results.errorCount === 0) {
    done();
  } else {
    throw new gutil.PluginError('tslint', new Error(results.output));
  }
});
@JoshuaKGoldberg
Copy link
Contributor

@bennyn Linter accepts a program in its constructor, then lint will use files from the program. Is that what you're looking for? It's unclear to me from your description why that isn't acceptable?

#3797 tracks cleaning this area up.

@bennycode
Copy link
Contributor Author

@JoshuaKGoldberg program is exactly what I was looking for. Thanks! I just couldn't find it because the parameter for the TSLint CLI is called project (and not program).

My code now works okay:

const tslint = require('tslint');
const program = tslint.Linter.createProgram(`${process.cwd()}/tsconfig.json`);
const linter = new tslint.Linter(
  {
    fix: false,
    formatter: 'prose',
  },
  program
);

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