Skip to content

Commit

Permalink
Addressed code review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
airportyh committed Aug 21, 2019
1 parent 5b73140 commit 4d94b61
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 59 deletions.
9 changes: 3 additions & 6 deletions moo.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@
}
}

function pad(n, length) {
var s = String(n)
function pad(s, length) {
if (s.length > length) {
return n
return s
}
return Array(length - s.length + 1).join(" ") + s
}
Expand Down Expand Up @@ -593,8 +592,6 @@
}
}

Lexer.prototype.lastNLines = lastNLines

Lexer.prototype.formatError = function(token, message) {
if (token == null) {
// An undefined token indicates EOF
Expand Down Expand Up @@ -623,7 +620,7 @@
for (var i = 0; i < displayedLines.length; i++) {
var line = displayedLines[i]
var lineNo = firstDisplayedLine + i
errorLines.push(pad(lineNo, lastLineDigits) + " " + line);
errorLines.push(pad(String(lineNo), lastLineDigits) + " " + line);
if (lineNo === token.line) {
errorLines.push(pad("", lastLineDigits + token.col + 1) + "^")
}
Expand Down
53 changes: 0 additions & 53 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1261,56 +1261,3 @@ describe('unicode flag', () => {
})

})

describe("lastNLines", () => {
const lexer = moo.compile({})

test("grabs last n lines if more than needed", () => {
const input = "line 1\nline 2\nline 3"
expect(lexer.lastNLines(input, 2))
.toEqual([
"line 2",
"line 3"
])
})

test("grabs all lines if not enough", () => {
const input = "line 1\nline 2\nline 3"
expect(lexer.lastNLines(input, 10))
.toEqual([
"line 1",
"line 2",
"line 3"
])
})

test("grabs last empty line", () => {
const input = "line 1\nline 2\nline 3\n"
expect(lexer.lastNLines(input, 2))
.toEqual([
"line 3",
""
])
})

test("grabs first empty line", () => {
const input = "\nline 1\nline 2\nline 3"
expect(lexer.lastNLines(input, 10))
.toEqual([
"",
"line 1",
"line 2",
"line 3"
])
})

test("does not grab first empty line if unneeded", () => {
const input = "\nline 1\nline 2\nline 3"
expect(lexer.lastNLines(input, 3))
.toEqual([
"line 1",
"line 2",
"line 3"
])
})
})

0 comments on commit 4d94b61

Please sign in to comment.