Skip to content

Commit 46fe5be

Browse files
committed
fix: parsing negative number
Close GH-123
1 parent 20efe68 commit 46fe5be

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## unreleased
44

5+
### Bugfixes
6+
- [GH-123](https://github.com/zackad/prettier-plugin-twig/issues/123) Fix parsing negative number
7+
58
---
69
## 0.15.0 (2025-02-01)
710

src/melody/melody-parser/Lexer.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,10 @@ export default class Lexer {
354354
return this.createToken(TokenTypes.ARROW, pos);
355355
}
356356
default: {
357-
if (isDigit(input.lac(0))) {
357+
if (
358+
isDigit(input.lac(0)) ||
359+
(input.la(0) === "-" && isDigit(input.lac(1)))
360+
) {
358361
input.next();
359362
return this.matchNumber(pos);
360363
}
@@ -601,6 +604,11 @@ export default class Lexer {
601604
matchNumber(pos) {
602605
const input = this.input;
603606
let c;
607+
608+
// check for negative number
609+
if (input.la(0) === "-" && isDigit(input.lac(1))) {
610+
input.next();
611+
}
604612
while ((c = input.lac(0)) !== EOF) {
605613
if (!isDigit(c)) {
606614
break;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{# https://github.com/zackad/prettier-plugin-twig/issues/123 #}
2+
{% set x = -2 %}
3+
{% set x = -2.092 %}
4+
{{ '00001'|slice(-2, 2) }}
5+
{{ '00001'|slice(2, -2) }}

tests/Expressions/jsfmt.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ describe("Expressions", () => {
3838
});
3939
await expect(actual).toMatchFileSnapshot(snapshotFile);
4040
});
41+
42+
it("should handle negative number without space", async () => {
43+
const { actual, snapshotFile } = await run_spec(import.meta.url, {
44+
source: "negative_number.twig"
45+
});
46+
await expect(actual).toMatchFileSnapshot(snapshotFile);
47+
});
48+
4149
it("should handle mapping expressions", async () => {
4250
const { actual, snapshotFile } = await run_spec(import.meta.url, {
4351
source: "mappingExpression.twig"
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{# https://github.com/zackad/prettier-plugin-twig/issues/123 #}
2+
{% set x = -2 %}
3+
{% set x = -2.092 %}
4+
{{ "00001"|slice(-2, 2) }}
5+
{{ "00001"|slice(2, -2) }}

0 commit comments

Comments
 (0)