-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
tree.error
, tree.warn
&& tree.dependency
(Messages API)
- Loading branch information
1 parent
4381164
commit 12e1f0a
Showing
5 changed files
with
243 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const chalk = require('chalk') | ||
|
||
const frame = (err) => { | ||
err.message = err.name === 'PluginError' | ||
? `❌ ${err.name}: ${err.plugin}\n\n` | ||
: `⚠️ ${err.name}: ${err.plugin}\n\n` | ||
|
||
err.message += `[${err.location.line}:${err.location.column}] ${ | ||
err.message.length > 80 | ||
? `${err.message.substr(0, 70)}\n${ | ||
err.message.substr(70, err.message.length) | ||
}` | ||
: err.message | ||
}\n\n` | ||
|
||
let _ = '' | ||
while (_.length < 80) _ += ' ' | ||
|
||
err.message += chalk.underline( | ||
`${err.file}${_.substr(err.file.length, 80)}\n\n` | ||
) | ||
|
||
const frames = err.src.split('\n') | ||
|
||
const format = (src, line) => src.slice(Math.max(0, line - 3), line + 2) | ||
|
||
format(frames, err.location.line).forEach((frame, i) => { | ||
const line = err.location.line + i - 2 | ||
|
||
let offset = '' | ||
const cursor = err.location.column | ||
|
||
while (offset.length < cursor + 5) offset += ' ' | ||
|
||
err.message += line === err.location.line | ||
? chalk.inverse(`\n > ${err.location.line}| ${frame} \n`) + `${offset}🔺\n` | ||
: chalk.white(`${err.location.line}| ${frame}\n`) | ||
}) | ||
|
||
err.message += chalk.underline(`${_}\n`) | ||
|
||
return err.name === 'PluginError' | ||
? chalk.bold.red(`${err.message}\n`) | ||
: chalk.bold.yellow(`${err.message}\n`) | ||
} | ||
|
||
module.exports = 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,16 @@ | ||
const code = require('./frame') | ||
|
||
class PluginError extends Error { | ||
constructor (err) { | ||
super(err) | ||
|
||
this.name = err.name | ||
this.message = err.location ? code(err) : err.message | ||
|
||
if (!err.stack) this.stack = false | ||
|
||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} | ||
|
||
module.exports = PluginError |
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,71 @@ | ||
var it = require('mocha').it | ||
var expect = require('chai').expect | ||
var describe = require('mocha').describe | ||
|
||
var posthtml = require('../lib') | ||
|
||
var html = '<div class="messages">Messages</div>' | ||
var messages = [ | ||
{ | ||
type: 'warning', | ||
file: './index.html', | ||
plugin: 'posthtml-plugin', | ||
message: "I'm a warning" | ||
}, | ||
{ | ||
type: 'error', | ||
file: './index.html', | ||
plugin: 'posthtml-plugin', | ||
message: "I'm an Error" | ||
}, | ||
{ | ||
type: 'dependency', | ||
file: 'path/to/1.html', | ||
from: './index.html' | ||
} | ||
] | ||
|
||
function test (html, done) { | ||
posthtml() | ||
.use(function (tree) { | ||
tree.warn('posthtml-plugin', 'I\'m a warning', false) | ||
|
||
return tree | ||
}) | ||
.use(function (tree) { | ||
tree.error('posthtml-plugin', 'I\'m an Error', false) | ||
|
||
return tree | ||
}) | ||
.use(function (tree) { | ||
tree.dependency('path/to/1.html') | ||
|
||
return tree | ||
}) | ||
.process(html, { from: './index.html' }) | ||
.then(function (result) { | ||
result.messages = result.messages.map(function (msg) { | ||
if (msg.message) { | ||
msg.message = typeof msg.message === 'function' | ||
? msg.message() | ||
: msg.message | ||
} | ||
|
||
return msg | ||
}) | ||
|
||
expect(html).to.eql(result.html) | ||
expect(messages).to.eql(result.messages) | ||
|
||
done() | ||
}) | ||
.catch(function (error) { | ||
done(error) | ||
}) | ||
} | ||
|
||
describe('Messages', function () { | ||
it('should expose messages via result.messages', function (done) { | ||
test(html, done) | ||
}) | ||
}) |