-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from Realytics/feature/diagnostic-formatter
Add diagnostics/lints formatters
- Loading branch information
Showing
9 changed files
with
253 additions
and
13 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
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,39 @@ | ||
var os = require('os'); | ||
var codeFrame = require('babel-code-frame'); | ||
var chalk = require('chalk'); | ||
var fs = require('fs'); | ||
|
||
/** | ||
* Create new code frame formatter. | ||
* | ||
* @param options Options for babel-code-frame - see https://www.npmjs.com/package/babel-code-frame | ||
* @returns {codeframeFormatter} | ||
*/ | ||
module.exports = function createCodeframeFormatter(options) { | ||
return function codeframeFormatter(message, useColors) { | ||
var colors = new chalk.constructor({enabled: useColors}); | ||
var messageColor = message.isWarningSeverity() ? colors.bold.yellow : colors.bold.red; | ||
var positionColor = colors.dim; | ||
|
||
var source = message.getFile() && fs.existsSync(message.getFile()) && fs.readFileSync(message.getFile(), 'utf-8'); | ||
var frame = ''; | ||
|
||
if (source) { | ||
frame = codeFrame( | ||
source, | ||
message.line, | ||
message.character, | ||
Object.assign({}, options || {}, { highlightCode: useColors }) | ||
) | ||
.split('\n') | ||
.map(function (str) { return ' ' + str; }) | ||
.join(os.EOL); | ||
} | ||
|
||
return ( | ||
messageColor(message.getSeverity().toUpperCase() + ' at ' + message.getFile()) + os.EOL + | ||
positionColor(message.getLine() + ':' + message.getCharacter()) + ' ' + message.getContent() + | ||
(frame ? os.EOL + frame : '') | ||
); | ||
}; | ||
}; |
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,23 @@ | ||
|
||
var chalk = require('chalk'); | ||
var os = require('os'); | ||
|
||
/** | ||
* Creates new default formatter. | ||
* | ||
* @returns {defaultFormatter} | ||
*/ | ||
module.exports = function createDefaultFormatter() { | ||
return function defaultFormatter(message, useColors) { | ||
var colors = new chalk.constructor({enabled: useColors}); | ||
var messageColor = message.isWarningSeverity() ? colors.bold.yellow : colors.bold.red; | ||
var numberColor = colors.bold.cyan; | ||
var codeColor = colors.grey; | ||
|
||
return [ | ||
messageColor(message.getSeverity().toUpperCase() + ' at ' + message.getFile()) + | ||
'(' + numberColor(message.getLine()) + ',' + numberColor(message.getCharacter()) + '): ', | ||
codeColor(message.getFormattedCode() + ': ') + message.getContent() | ||
].join(os.EOL); | ||
}; | ||
}; |
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,105 @@ | ||
|
||
var describe = require('mocha').describe; | ||
var it = require('mocha').it; | ||
var os = require('os'); | ||
var beforeEach = require('mocha').beforeEach; | ||
var afterEach = require('mocha').afterEach; | ||
var expect = require('chai').expect; | ||
var mockFs = require('mock-fs'); | ||
var NormalizedMessage = require('../../lib/NormalizedMessage'); | ||
var createCodeframeFormatter = require('../../lib/formatter/codeframeFormatter'); | ||
|
||
describe('[UNIT] formatter/codeframeFormatter', function () { | ||
|
||
beforeEach(function () { | ||
mockFs({ | ||
some: { | ||
'file.ts': [ | ||
'class SomeClass {', | ||
' private someProperty: boolean;', | ||
' constructor() {', | ||
' console.log(\'anything special\');', | ||
' }', | ||
'}' | ||
].join('\n') | ||
} | ||
}); | ||
}); | ||
|
||
afterEach(function () { | ||
mockFs.restore(); | ||
}); | ||
|
||
it('should format normalized diagnostic message', function () { | ||
var message = new NormalizedMessage({ | ||
type: NormalizedMessage.TYPE_DIAGNOSTIC, | ||
code: 123, | ||
severity: NormalizedMessage.SEVERITY_ERROR, | ||
content: 'Some diagnostic content', | ||
file: 'some/file.ts', | ||
line: 1, | ||
character: 7 | ||
}); | ||
var formatter = createCodeframeFormatter({ | ||
linesAbove: 1, | ||
linesBelow: 1 | ||
}); | ||
var formattedMessage = formatter(message, false); | ||
|
||
expect(formattedMessage).to.be.equal( | ||
'ERROR at some/file.ts' + os.EOL + | ||
'1:7 Some diagnostic content' + os.EOL + | ||
' > 1 | class SomeClass {' + os.EOL + | ||
' | ^' + os.EOL + | ||
' 2 | private someProperty: boolean;' | ||
); | ||
}); | ||
|
||
it('should format normalized lint message', function () { | ||
var message = new NormalizedMessage({ | ||
type: NormalizedMessage.TYPE_LINT, | ||
code: 'some-lint-rule', | ||
severity: NormalizedMessage.SEVERITY_WARNING, | ||
content: 'Some lint content', | ||
file: 'some/file.ts', | ||
line: 2, | ||
character: 11 | ||
}); | ||
var formatter = createCodeframeFormatter({ | ||
linesAbove: 1, | ||
linesBelow: 1 | ||
}); | ||
var formattedMessage = formatter(message, false); | ||
|
||
expect(formattedMessage).to.be.equal( | ||
'WARNING at some/file.ts' + os.EOL + | ||
'2:11 Some lint content' + os.EOL + | ||
' 1 | class SomeClass {' + os.EOL + | ||
' > 2 | private someProperty: boolean;' + os.EOL + | ||
' | ^' + os.EOL + | ||
' 3 | constructor() {' | ||
); | ||
}); | ||
|
||
it('should format normalized message without file', function () { | ||
var message = new NormalizedMessage({ | ||
type: NormalizedMessage.TYPE_LINT, | ||
code: 'some-lint-rule', | ||
severity: NormalizedMessage.SEVERITY_WARNING, | ||
content: 'Some lint content', | ||
file: 'some/unknown-file.ts', | ||
line: 2, | ||
character: 11 | ||
}); | ||
var formatter = createCodeframeFormatter({ | ||
linesAbove: 1, | ||
linesBelow: 1 | ||
}); | ||
var formattedMessage = formatter(message, false); | ||
|
||
expect(formattedMessage).to.be.equal( | ||
'WARNING at some/unknown-file.ts' + os.EOL + | ||
'2:11 Some lint content' | ||
); | ||
}); | ||
}); |
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,48 @@ | ||
|
||
var describe = require('mocha').describe; | ||
var it = require('mocha').it; | ||
var os = require('os'); | ||
var expect = require('chai').expect; | ||
var NormalizedMessage = require('../../lib/NormalizedMessage'); | ||
var createDefaultFormatter = require('../../lib/formatter/defaultFormatter'); | ||
|
||
describe('[UNIT] formatter/defaultFormatter', function () { | ||
|
||
it('should format normalized diagnostic message', function () { | ||
var message = new NormalizedMessage({ | ||
type: NormalizedMessage.TYPE_DIAGNOSTIC, | ||
code: 123, | ||
severity: NormalizedMessage.SEVERITY_ERROR, | ||
content: 'Some diagnostic content', | ||
file: '/some/file.ts', | ||
line: 1, | ||
character: 5 | ||
}); | ||
var formatter = createDefaultFormatter(); | ||
var formattedMessage = formatter(message, false); | ||
|
||
expect(formattedMessage).to.be.equal( | ||
'ERROR at /some/file.ts(1,5): ' + os.EOL + | ||
'TS123: Some diagnostic content' | ||
); | ||
}); | ||
|
||
it('should format normalized lint message', function () { | ||
var message = new NormalizedMessage({ | ||
type: NormalizedMessage.TYPE_LINT, | ||
code: 'some-lint-rule', | ||
severity: NormalizedMessage.SEVERITY_WARNING, | ||
content: 'Some lint content', | ||
file: '/some/file.ts', | ||
line: 2, | ||
character: 6 | ||
}); | ||
var formatter = createDefaultFormatter(); | ||
var formattedMessage = formatter(message, false); | ||
|
||
expect(formattedMessage).to.be.equal( | ||
'WARNING at /some/file.ts(2,6): ' + os.EOL + | ||
'some-lint-rule: Some lint content' | ||
); | ||
}); | ||
}); |
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