Skip to content

Commit

Permalink
Throw an error when a format ends with a dangling "%"
Browse files Browse the repository at this point in the history
  • Loading branch information
pineapplemachine committed May 20, 2018
1 parent d435314 commit c325bbc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ TimestampParser.parseFormatString = function parseFormatString(format){
addCharacter(ch);
}
}
if(directive) throw new TimestampParseError(
"Found unterminated directive at the end of the format string.", {
format: formatString
}
);
if(tokens.length && tokens[tokens.length - 1].string === "Z"){
tokens.zuluTimezone = true;
}
Expand Down
10 changes: 10 additions & 0 deletions test/canary-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,16 @@ function createTests(strtime){
message: `Failed with format "%Y-%m-%?": Unknown directive "%?".`,
});
});
this.test("unterminated directive produces a readable error", function(){
assert.throws(() => strftime(new Date(), "%"), {
name: "TimestampParseError",
message: `Failed with format "%": Found unterminated directive at the end of the format string.`,
});
assert.throws(() => strptime("2000-01-01", "%"), {
name: "TimestampParseError",
message: `Failed with format "%": Found unterminated directive at the end of the format string.`,
});
});
this.test("parse format is an empty string", function(){
assert.throws(() => strptime("2000-01-01", ""), {
name: "TimestampParseError",
Expand Down

0 comments on commit c325bbc

Please sign in to comment.