Skip to content

Commit

Permalink
Fixed escape regex unparsing
Browse files Browse the repository at this point in the history
Fixed custom keyword for if.
  • Loading branch information
MikaelMayer committed Mar 15, 2019
1 parent db836d4 commit 3e75af9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ export class Identifier {
}
export class IfStatement {
readonly type: string;
readonly ifKeyword: string;
readonly test: Expression;
readonly consequent: Statement;
readonly alternate: Statement | null;
Expand All @@ -1068,7 +1069,7 @@ export class IfStatement {
readonly wsBeforeElse: string;
wsAfter: string = "";
unparse(parent?: Unparsable): string {
return this.wsBefore + "if" +
return this.wsBefore + this.ifKeyword +
this.wsBeforeOpening + "(" +
unparseChild(this)(this.test) +
this.wsBeforeClosing + ")" +
Expand All @@ -1077,12 +1078,13 @@ export class IfStatement {
this.wsBeforeElse + "else" + unparseChild(this)(this.alternate) : "") +
this.wsAfter;
}
constructor(wsBefore: string, wsBeforeOpening: string, test: Expression,
constructor(wsBefore: string, ifKeyword: string, wsBeforeOpening: string, test: Expression,
wsBeforeClosing: string, consequent: Statement, wsBeforeElse: string, alternate: Statement | null) {
this.type = Syntax.IfStatement;
this.test = test;
this.consequent = consequent;
this.alternate = alternate;
this.ifKeyword = ifKeyword;
this.wsBefore = wsBefore;
this.wsBeforeOpening = wsBeforeOpening;
this.wsBeforeClosing = wsBeforeClosing;
Expand Down Expand Up @@ -1521,7 +1523,7 @@ export class RegexLiteral {
wsAfter: string = "";
unparse(parent?: Unparsable): string {
return this.wsBefore + "/" +
this.regex.pattern.replace(/\\/g, "\\\\").replace(/\//g, "\\/") +
this.regex.pattern +
"/" +
this.regex.flags + this.wsAfter;
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,7 @@ export class Parser {
const node = this.createNode();
let consequent: Node.Statement;
let alternate: Node.Statement | null = null;

let ifraw = this.getTokenRaw(this.lookahead);
const wsBefore = this.expectKeyword('if');
const wsBeforeOpening = this.expect('(');
var wsBeforeElse = "";
Expand All @@ -2375,7 +2375,7 @@ export class Parser {
}
}

return this.finalize(node, new Node.IfStatement(wsBefore, wsBeforeOpening, test, wsBeforeClosing, consequent, wsBeforeElse, alternate));
return this.finalize(node, new Node.IfStatement(wsBefore, ifraw, wsBeforeOpening, test, wsBeforeClosing, consequent, wsBeforeElse, alternate));
}

// https://tc39.github.io/ecma262/#sec-do-while-statement
Expand Down

0 comments on commit 3e75af9

Please sign in to comment.