Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle extends option correctly in when calling tslint #12

Merged
merged 1 commit into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
npm-debug.log
tmp/
yarn.lock
tests/fixtures/output.txt
20 changes: 7 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ var Filter = require('broccoli-persistent-filter');
var chalk = require('chalk');
var existsSync = require('exists-sync');
var path = require('path');
var Linter = require("tslint").Linter;
var Linter = require('tslint').Linter;
var Configuration = require('tslint').Configuration;
var fs = require('fs');

function TSLint(inputNode, options) {
Expand All @@ -11,7 +12,6 @@ function TSLint(inputNode, options) {
}
options = options || {};

this.configuration = { rules: {} };
this.options = {
outputFile: options.outputFile,
failBuild: options.failBuild || false,
Expand All @@ -20,24 +20,17 @@ function TSLint(inputNode, options) {
logError: options.logError
};

var tslintConfigPath = path.resolve('tslint.json');
this.tslintConfigPath = 'tslint.json';
if (options.configuration) {
tslintConfigPath = path.resolve(options.configuration);
this.tslintConfigPath = options.configuration;
} else {
console.log(this.createLogMessage('Using tslint.json as the default file for linting rules', 'blue'));
}

if (!existsSync(tslintConfigPath)) {
if (!existsSync(this.tslintConfigPath)) {
throw new Error('Cannot find tslint configuration file: ' + tslintConfigPath);
}

try {
this.configuration = JSON.parse(fs.readFileSync(tslintConfigPath, 'utf8'));
} catch (e) {
var message = 'Cannot parse configuration file: ' + tslintConfigPath;
throw new Error(this.createLogMessage(message, 'red'));
}

if (!options.formatter) {
// default formatter
this.options.formatter = 'prose';
Expand Down Expand Up @@ -92,7 +85,8 @@ TSLint.prototype.build = function () {

TSLint.prototype.processString = function(content, relativePath) {
var linter = new Linter(this.options);
linter.lint(relativePath, content, this.configuration);
var configLoad = Configuration.findConfiguration(this.tslintConfigPath, relativePath);
linter.lint(relativePath, content, configLoad.results);
var result = linter.getResult();

this.totalFiles++;
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/errorFiles/errorFile1.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var Xx = "abcd";
var Xx = "abcd";
3 changes: 3 additions & 0 deletions tests/fixtures/lintConfig/extends-format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "tslint:recommended"
}
43 changes: 13 additions & 30 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,6 @@ describe('broccoli-tslinter', function() {
assert.throws(willThrow);
});

it('should throw error during parsing of configuration file', function() {
var willThrow = function() {
var node = new TSLint('./tests/fixtures/errorFiles', {
logError: function(message) {
loggerOutput.push(message)
},
configuration: './tests/fixtures/lintConfig/parse-error.json'
});
builder = new broccoli.Builder(node);
return builder.build();
};

assert.throws(willThrow);
});

it('should throw error when configuration file does not follow format', function() {
var willThrow = function() {
var node = new TSLint('./tests/fixtures/errorFiles', {
logError: function(message) {
loggerOutput.push(message)
},
configuration: './tests/fixtures/lintConfig/incorrect-format.json'
});
builder = new broccoli.Builder(node);
return builder.build();
};

assert.throws(willThrow);
});

it('linting correct file should result in no lint errors', function() {
var node = new TSLint('./tests/fixtures/lintedFiles', {
logError: function(message) {
Expand All @@ -92,6 +62,19 @@ describe('broccoli-tslinter', function() {
});
});

it('linting error files with extends format should result in lint errors', function() {
var node = new TSLint('./tests/fixtures/errorFiles', {
logError: function(message) {
loggerOutput.push(message);
},
configuration: './tests/fixtures/lintConfig/extends-format.json'
});
builder = new broccoli.Builder(node);
return builder.build().then(function() {
assert.notEqual(loggerOutput.length, 0, 'Errors should be seen for linted files');
});
});

it('linting errors should be the same on subsequent runs', function() {
var node = new TSLint('./tests/fixtures/errorFiles', {
logError: function(message) {
Expand Down