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

Change lines to 1based. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ npm i warnie
const Warnie = require('warnie');
const fs = require('fs');

console.log(new Warnie('Hello world!', __filename, 3, 12)
console.log(new Warnie('Hello world!', __filename, 4, 12)
.explain(fs.readFileSync(__filename, 'utf-8').split('\n')));
```

**NB**. Line and column begin from 1. As for humans.

### Customization

Best use with [`chalk`](https://www.npmjs.com/package/chalk) (install it with `npm i chalk`).
Expand Down Expand Up @@ -50,8 +52,8 @@ Warnie.renderPointer = column => `${new Array(column).join(' ')}↑`;
/**
* @param {string} message - text message of error
* @param {string} filename - name of file the error belongs
* @param {number} [line=0] - errored line
* @param {number} [column=0] - errored column
* @param {number} [line=1] - errored line
* @param {number} [column=1] - errored column
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It can be 0:0 by default (as undefined)

* @param {number} [severity=0] - severity of the error, one of [-1, 0, 1, 2]
* @param {Object} [data] - variable data of the error
*/
Expand Down
23 changes: 12 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ module.exports = Warnie;
* @name Warnie
* @param {string} message - text message of error
* @param {string} filename - name of file the error belongs
* @param {number} [line=0] - errored line
* @param {number} [column=0] - errored column
* @param {number} [severity=0] - severity of the error, one of [-1, 0, 1, 2]
* @param {number} [line=1] - errored line
* @param {number} [column=1] - errored column
* @param {number} [severity=0] - severity of the error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let it be another issue, please?

* @param {Object} [data] - variable data of the error
*/
function Warnie(message, filename, line, column, severity, data) {
this.message = message + '';
this.filename = filename + '';
this.line = line|0;
this.column = column|0;
this.line = line||1;
this.column = column||1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|0 here used to convert to number, not for default value.

this.severity = severity|0;
this.data = data;
}
Expand All @@ -43,28 +43,29 @@ Warnie.linesAround = 2;
* @returns {string} - pretty message with pointer and lines around
*/
Warnie.prototype.explain = function(lines) {
var humanLine = this.line - 1;
var pointer = Warnie.renderPointer(Warnie.renderLineNumber(0).length + this.column);
var result = [
renderLine(this.line, lines[this.line]),
renderLine(humanLine, lines[humanLine]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

humanLine is one-based now. Here you need zero-based-line.

Warnie.shadowDye(pointer.slice(0, -1)) + Warnie.pointerDye(pointer.slice(-1))
];

// Prepend lines before the current one
var i = this.line - 1;
while (i >= 0 && i >= (this.line - Warnie.linesAround)) {
var i = humanLine - 1;
while (i >= 0 && i >= (humanLine - Warnie.linesAround)) {
result.unshift(renderLine(i, lines[i]));
i--;
}

// Append lines after the current one
i = this.line + 1;
while (i < lines.length && i <= (this.line + Warnie.linesAround)) {
i = humanLine + 1;
while (i < lines.length && i <= (humanLine + Warnie.linesAround)) {
result.push(renderLine(i, lines[i]));
i++;
}

// Prepend error info
var loc = this.line ? (':' + this.line + (this.column ? ':' + this.column : '')) : '';
var loc = humanLine ? (':' + humanLine + (this.column ? ':' + this.column : '')) : '';
result.unshift(Warnie.messageDye(this.message) + ' at '
+ Warnie.filenameDye(this.filename) + loc + ' :');

Expand Down