Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Enable type-checking for rules that needs it #109

Closed
beckend opened this issue Aug 19, 2016 · 7 comments · Fixed by #157
Closed

Enable type-checking for rules that needs it #109

beckend opened this issue Aug 19, 2016 · 7 comments · Fixed by #157

Comments

@beckend
Copy link

beckend commented Aug 19, 2016

Getting a lot(or same) error repeatedly when using latest tslint.
It says on tslint gh:

To enable rules that work with the type checker, a TypeScript program object must be passed to the linter

palantir/tslint#1445

@dead-claudia
Copy link

This would also be nice. It shouldn't take much work to do it, considering that TSLint has a few helper functions to make this very simple.

@Arcanemagus
Copy link
Member

@isiahmeadows I'd be happy to review a PR 😉.

@dead-claudia
Copy link

I've been majorly backed up lately, or I probably would've already.

@zaggino
Copy link

zaggino commented Jan 24, 2017

Hi guys, tried to fix this with the helper mentioned above but I don't think it produces proper valid Program instance. I've already implemented type-checking in my https://github.com/zaggino/brackets-typescript repository but it's much more complex there because I also re-implemented whole TypeScript's Language Service.

My attempt diff makes linter-tslint fail deep inside node_modules\typescript\lib\typescript.js:

     );
   },

+  getProgram(Linter, configurationPath) {
+    let program;
+    try {
+      const tsconfigPath = path.resolve(path.dirname(configurationPath), 'tsconfig.json');
+      const isFile = fs.statSync(tsconfigPath).isFile();
+      if (isFile) {
+        program = Linter.createProgram('tsconfig.json', path.dirname(configurationPath));
+      }
+    } catch (err) {
+      // no-op
+    }
+    return program;
+  },
+
   provideLinter() {
     // eslint-disable-next-line import/no-dynamic-require
     tslintDef = require('loophole').allowUnsafeNewFunction(() => require(TSLINT_MODULE_NAME).Linter);
@@ -144,7 +158,7 @@ export default {
           const linter = new Linter({
             formatter: 'json',
             rulesDirectory,
-          });
+          }, this.getProgram(Linter, configurationPath));

           linter.lint(filePath, text, configuration);
           const lintResult = linter.getResult();

@newtrat
Copy link

newtrat commented Feb 3, 2017

@zaggino your solution works perfectly for me! I am hesitant to issue a pull request since the fix was your idea, and apparently it doesn't work for everyone, but I can confirm that it does work in principle. Have you been able to find a solution that works for you?

@newtrat
Copy link

newtrat commented Feb 3, 2017

Ah, and as mentioned in #93 @beckend has also solved this issue in his own plugin at https://github.com/beckend/linter-tslint2

@zaggino
Copy link

zaggino commented Feb 3, 2017

feel free to use the code however you wish @newtrat , it didn't work for me so I've switched back to Brackets for my TypeScript projects

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

Successfully merging a pull request may close this issue.

6 participants