Skip to content

Commit

Permalink
Add mockTypeScriptVersion option to force tslint to use the same type…
Browse files Browse the repository at this point in the history
…script version as the plugin. Fixes #58
  • Loading branch information
clusterseven\agurden committed Jan 15, 2018
1 parent 8aaffba commit 8d466a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -41,6 +41,7 @@ Your `node_modules` folder should look like this:
* `alwaysShowRuleFailuresAsWarnings` - always show rule failures as warnings, ignoring the severity configuration in the tslint.json configuration.
* `disableNoUnusedVariableRule` - disable `no-unused-variable` rule.
* `supressWhileTypeErrorsPresent` - supress tslint errors from being reported while other errors are present.
* `mockTypeScriptVersion` - force tslint to use the same version of TypeScript as this plugin. This will affect other plugins that require the typescript package.

Here a configuration sample:

Expand All @@ -53,7 +54,8 @@ Here a configuration sample:
"ignoreDefinitionFiles": true,
"configFile": "../tslint.json",
"disableNoUnusedVariableRule": false,
"supressWhileTypeErrorsPresent": false
"supressWhileTypeErrorsPresent": false,
"mockTypeScriptVersion": false
}
]
}
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -17,10 +17,13 @@
"test": "tsc && tape out/test/**/*.spec.js",
"lint": "tslint src/index.ts"
},
"dependencies": {},
"dependencies": {
"mock-require": "^2.0.2"
},
"devDependencies": {
"@types/node": "^7.0.8",
"@types/tape": "^4.2.29",
"@types/mock-require": "^2.0.0",
"tape": "^4.6.3",
"tslint": "^5.2.0",
"typescript": "^2.3.2",
Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
@@ -1,6 +1,7 @@
import * as ts_module from "../node_modules/typescript/lib/tsserverlibrary";
import * as tslint from 'tslint';
import * as path from 'path';
import * as mockRequire from 'mock-require';

// Settings for the plugin section in tsconfig.json
interface Settings {
Expand All @@ -9,13 +10,15 @@ interface Settings {
configFile?: string;
disableNoUnusedVariableRule?: boolean // support to enable/disable the workaround for https://github.com/Microsoft/TypeScript/issues/15344
supressWhileTypeErrorsPresent: boolean;
mockTypeScriptVersion: boolean;
}

//TODO we "steal"" an error code with a registered code fix. 2515 = implement inherited abstract class
const TSLINT_ERROR_CODE = 2515;

function init(modules: { typescript: typeof ts_module }) {
const ts = modules.typescript;
let tslint = require('tslint');

let codeFixActions = new Map<string, Map<string, tslint.RuleFailure>>();
let registeredCodeFixes = false;
Expand Down Expand Up @@ -63,6 +66,11 @@ function init(modules: { typescript: typeof ts_module }) {
let config: Settings = fixRelativeConfigFilePath(info.config, info.project.getCurrentDirectory());
let configuration: tslint.Configuration.IConfigurationFile = null;

if(config.mockTypeScriptVersion) {
mockRequire('typescript', ts);
tslint = mockRequire.reRequire('tslint');
}

// Set up decorator
const proxy = Object.create(null) as ts.LanguageService;
const oldLS = info.languageService;
Expand Down

0 comments on commit 8d466a6

Please sign in to comment.