forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Stylish formatter and make it default. Fixes eslint#517
- Loading branch information
1 parent
3c8e833
commit 5f89734
Showing
5 changed files
with
246 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @fileoverview Stylish reporter | ||
* @author Sindre Sorhus | ||
*/ | ||
"use strict"; | ||
|
||
var chalk = require("chalk"), | ||
table = require("text-table"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helper Functions | ||
//------------------------------------------------------------------------------ | ||
|
||
function getMessageType(message, rules) { | ||
if (message.fatal) { | ||
return chalk.red("error"); | ||
} | ||
|
||
var severity = rules[message.ruleId][0] || rules[message.ruleId]; | ||
|
||
if (severity === 2) { | ||
return chalk.red("error"); | ||
} | ||
|
||
return chalk.yellow("warning"); | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Public Interface | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(results, config) { | ||
|
||
var output = "\n", | ||
total = 0, | ||
rules = config.rules || {}; | ||
|
||
results.forEach(function(result) { | ||
var messages = result.messages; | ||
|
||
if (messages.length === 0) { | ||
return; | ||
} | ||
|
||
total += messages.length; | ||
output += chalk.underline(result.filePath) + "\n"; | ||
|
||
output += table( | ||
messages.map(function(message) { | ||
return [ | ||
"", | ||
message.line || 0, | ||
message.column || 0, | ||
getMessageType(message, rules), | ||
chalk.blue(message.message.replace(/\.$/, "")), | ||
chalk.gray(message.ruleId) | ||
] | ||
}), | ||
{ | ||
align: ["", "r", "l"], | ||
stringLength: function(str) { | ||
return chalk.stripColor(str).length; | ||
} | ||
} | ||
).split("\n").map(function(el) { | ||
return el.replace(/(\d+)\s+(\d+)/, function(m, p1, p2) { | ||
return chalk.gray(p1 + ":" + p2); | ||
}); | ||
}).join("\n") + "\n\n"; | ||
}); | ||
|
||
output += chalk.red.bold("\u2716 " + total + " problem" + (total === 1 ? "" : "s") + "\n"); | ||
|
||
return total > 0 ? output : ""; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/** | ||
* @fileoverview Tests for options. | ||
* @author Sindre Sorhus | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var assert = require("chai").assert, | ||
formatter = require("../../../lib/formatters/stylish"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
describe("formatter:stylish", function() { | ||
describe("when passed no messages", function() { | ||
var code = [{ | ||
filePath: "foo.js", | ||
messages: [] | ||
}]; | ||
|
||
it("should return message", function() { | ||
var config = { | ||
rules: { foo: 2 } | ||
}; | ||
|
||
var result = formatter(code, config); | ||
assert.equal("", result); | ||
}); | ||
}); | ||
|
||
describe("when passed a single message", function() { | ||
var code = [{ | ||
filePath: "foo.js", | ||
messages: [{ | ||
message: "Unexpected foo.", | ||
line: 5, | ||
column: 10, | ||
ruleId: "foo" | ||
}] | ||
}]; | ||
|
||
it("should return a string in the correct format for errors", function() { | ||
var config = { | ||
rules: { foo: 2 } | ||
}; | ||
|
||
var result = formatter(code, config); | ||
assert.equal("\nfoo.js\n 5:10 error Unexpected foo foo\n\n\u2716 1 problem\n", result); | ||
}); | ||
|
||
it("should return a string in the correct format for warnings", function() { | ||
var config = { | ||
rules: { foo: 1 } | ||
}; | ||
|
||
var result = formatter(code, config); | ||
assert.equal("\nfoo.js\n 5:10 warning Unexpected foo foo\n\n\u2716 1 problem\n", result); | ||
}); | ||
|
||
it("should return a string in the correct format for errors with options config", function() { | ||
var config = { | ||
rules: { foo: [2, "option"] } | ||
}; | ||
|
||
var result = formatter(code, config); | ||
assert.equal("\nfoo.js\n 5:10 error Unexpected foo foo\n\n\u2716 1 problem\n", result); | ||
}); | ||
}); | ||
|
||
describe("when passed a fatal error message", function() { | ||
var code = [{ | ||
filePath: "foo.js", | ||
messages: [{ | ||
fatal: true, | ||
message: "Unexpected foo.", | ||
line: 5, | ||
column: 10, | ||
ruleId: "foo" | ||
}] | ||
}]; | ||
|
||
it("should return a string in the correct format", function() { | ||
var config = {}; // doesn't matter what's in the config for this test | ||
|
||
var result = formatter(code, config); | ||
assert.equal("\nfoo.js\n 5:10 error Unexpected foo foo\n\n\u2716 1 problem\n", result); | ||
}); | ||
}); | ||
|
||
describe("when passed multiple messages", function() { | ||
var code = [{ | ||
filePath: "foo.js", | ||
messages: [{ | ||
message: "Unexpected foo.", | ||
line: 5, | ||
column: 10, | ||
ruleId: "foo" | ||
}, { | ||
message: "Unexpected bar.", | ||
line: 6, | ||
column: 11, | ||
ruleId: "bar" | ||
}] | ||
}]; | ||
|
||
it("should return a string with multiple entries", function() { | ||
var config = { | ||
rules: { foo: 2, bar: 1 } | ||
}; | ||
|
||
var result = formatter(code, config); | ||
assert.equal("\nfoo.js\n 5:10 error Unexpected foo foo\n 6:11 warning Unexpected bar bar\n\n\u2716 2 problems\n", result); | ||
}); | ||
}); | ||
|
||
describe("when passed multiple files with 1 message each", function() { | ||
var code = [{ | ||
filePath: "foo.js", | ||
messages: [{ | ||
message: "Unexpected foo.", | ||
line: 5, | ||
column: 10, | ||
ruleId: "foo" | ||
}] | ||
}, { | ||
filePath: "bar.js", | ||
messages: [{ | ||
message: "Unexpected bar.", | ||
line: 6, | ||
column: 11, | ||
ruleId: "bar" | ||
}] | ||
}]; | ||
|
||
it("should return a string with multiple entries", function() { | ||
var config = { | ||
rules: { foo: 2, bar: 1 } | ||
}; | ||
|
||
var result = formatter(code, config); | ||
assert.equal("\nfoo.js\n 5:10 error Unexpected foo foo\n\nbar.js\n 6:11 warning Unexpected bar bar\n\n\u2716 2 problems\n", result); | ||
}); | ||
}); | ||
|
||
describe("when passed one file not found message", function() { | ||
var code = [{ | ||
filePath: "foo.js", | ||
messages: [{ | ||
fatal: true, | ||
message: "Couldn't find foo.js." | ||
}] | ||
}]; | ||
|
||
it("should return a string without line and column", function() { | ||
var config = { | ||
rules: { foo: 2, bar: 1 } | ||
}; | ||
|
||
var result = formatter(code, config); | ||
assert.equal("\nfoo.js\n 0:0 error Couldn't find foo.js\n\n\u2716 1 problem\n", result); | ||
}); | ||
}); | ||
}); |