Skip to content

Commit

Permalink
report extra comma in if test as an error. fixes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
jlchmura committed Feb 19, 2024
1 parent b46c0a5 commit b385c69
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/parser/lpcParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ export interface LPCDocument {
}

export const enum ParseExpressionFlag {
StatementOnly = 0b0000,
StatementOnly = 0b0000,
AllowDeclaration = 0b0001,
AllowFunction = 0b0010,
AllowMultiDecl = 0b0100,
AllowFunction = 0b0010,
AllowMultiDecl = 0b0100,
NoCommas = 0b1000,
}

export class ParserError extends Error {
Expand Down Expand Up @@ -424,6 +425,8 @@ export class LPCParser {
);
const children: LPCNode[] = [];

const allowCommaSeparator = !(flags & ParseExpressionFlag.NoCommas);

let t: TokenType,
lastToken: TokenType = 0;
// scan until we get a paren block end
Expand All @@ -432,7 +435,7 @@ export class LPCParser {
let possibleImpliedBinary = false;
while ((t = this.scanner.peek()) && t != endsWith && t != TokenType.EOS) {
if (
t == TokenType.Comma ||
(allowCommaSeparator && t == TokenType.Comma) ||
t == TokenType.BlankLines ||
t == TokenType.Whitespace
) {
Expand Down Expand Up @@ -780,7 +783,7 @@ export class LPCParser {
if (!nd.consequent) {
nd.test = this.parseParenBlock(
nd,
ParseExpressionFlag.StatementOnly
ParseExpressionFlag.StatementOnly | ParseExpressionFlag.NoCommas
);
this.eatWhitespaceAndNewlines();
break;
Expand Down
14 changes: 12 additions & 2 deletions src/plugin/print/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as prettier from "prettier";
import * as prettierPlugin from "../..";
import { ParseLPC } from "../../../parser/lpcParser";
import { ParseLPC, ParserError } from "../../../parser/lpcParser";
import {
assign_exp_suffix_comment,
for_loop_various,
Expand Down Expand Up @@ -38,6 +38,7 @@ describe("prettier-lpc plugin", () => {

// always validating that the result of formatting is backward compatible with JSONata
expect(() => ParseLPC(formatted)).not.toThrow();

return formatted;
};

Expand Down Expand Up @@ -581,7 +582,7 @@ describe("prettier-lpc plugin", () => {
formatted = format(`int i=0; test() { indices = ({i, index++}); }`);
expect(formatted).toMatchSnapshot("assignment_inside_array");
});

describe("If statements", () => {
test("formats if statements", () => {
let formatted = format(if_condense_test);
Expand All @@ -592,6 +593,15 @@ describe("prettier-lpc plugin", () => {
let formatted = format(ifWithExtraCurlyBrackets);
expect(formatted).toMatchSnapshot("ifWithExtraCurlyBrackets");
});

test("Ifs with invalid commas", () => {
// this should throw an error because of the comma after present()
expect(() =>
format(
`void test() { if(present("string"), this_object()) { return 1; } }`
)
).toThrow(ParserError);
});
});

test("formats for loops", () => {
Expand Down

0 comments on commit b385c69

Please sign in to comment.