From 29a53c5a0ae62e1369091728ede32dc9d473a067 Mon Sep 17 00:00:00 2001 From: Hana Shiro Date: Mon, 1 Mar 2021 03:19:44 +0800 Subject: [PATCH] fix: Fix pug-lexer parsed escaped interpolations incorrectly (#3299) * fix: Fix escaped interpolation returning `#{}` but not `!{}` * test: Update test case and snapshot of `pug-lexer/../interpolation.escape.pug` * style: Fix prettier format --- packages/pug-lexer/index.js | 6 +- .../test/__snapshots__/index.test.js.snap | 74 ++++++++----------- .../test/cases/interpolation.escape.pug | 5 +- 3 files changed, 37 insertions(+), 48 deletions(-) diff --git a/packages/pug-lexer/index.js b/packages/pug-lexer/index.js index 07afac50e..363a69ed9 100644 --- a/packages/pug-lexer/index.js +++ b/packages/pug-lexer/index.js @@ -579,7 +579,11 @@ Lexer.prototype = { } if (indexOfStringInterp !== Infinity) { if (matchOfStringInterp[1]) { - prefix = prefix + value.substring(0, indexOfStringInterp) + '#{'; + prefix = + prefix + + value.substring(0, indexOfStringInterp) + + matchOfStringInterp[2] + + '{'; return this.addText( type, value.substring(indexOfStringInterp + 3), diff --git a/packages/pug-lexer/test/__snapshots__/index.test.js.snap b/packages/pug-lexer/test/__snapshots__/index.test.js.snap index caea3836a..2c1910fd6 100644 --- a/packages/pug-lexer/test/__snapshots__/index.test.js.snap +++ b/packages/pug-lexer/test/__snapshots__/index.test.js.snap @@ -26702,31 +26702,17 @@ Array [ exports[`interpolation.escape.pug 1`] = ` Array [ - Object { - "loc": Object { - "end": Object { - "column": 1, - "line": 2, - }, - "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", - "start": Object { - "column": 1, - "line": 2, - }, - }, - "type": "newline", - }, Object { "buffer": false, "loc": Object { "end": Object { "column": 15, - "line": 2, + "line": 1, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 1, - "line": 2, + "line": 1, }, }, "mustEscape": false, @@ -26737,12 +26723,12 @@ Array [ "loc": Object { "end": Object { "column": 1, - "line": 3, + "line": 2, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 1, - "line": 3, + "line": 2, }, }, "type": "newline", @@ -26751,12 +26737,12 @@ Array [ "loc": Object { "end": Object { "column": 4, - "line": 3, + "line": 2, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 1, - "line": 3, + "line": 2, }, }, "type": "tag", @@ -26766,12 +26752,12 @@ Array [ "loc": Object { "end": Object { "column": 3, - "line": 4, + "line": 3, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 1, - "line": 4, + "line": 3, }, }, "type": "indent", @@ -26781,12 +26767,12 @@ Array [ "loc": Object { "end": Object { "column": 9, - "line": 4, + "line": 3, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 5, - "line": 4, + "line": 3, }, }, "type": "text", @@ -26796,12 +26782,12 @@ Array [ "loc": Object { "end": Object { "column": 3, - "line": 5, + "line": 4, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 1, - "line": 5, + "line": 4, }, }, "type": "newline", @@ -26810,12 +26796,12 @@ Array [ "loc": Object { "end": Object { "column": 13, - "line": 5, + "line": 4, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 5, - "line": 5, + "line": 4, }, }, "type": "text", @@ -26825,12 +26811,12 @@ Array [ "loc": Object { "end": Object { "column": 3, - "line": 6, + "line": 5, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 1, - "line": 6, + "line": 5, }, }, "type": "newline", @@ -26838,28 +26824,28 @@ Array [ Object { "loc": Object { "end": Object { - "column": 9, - "line": 6, + "column": 13, + "line": 5, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 5, - "line": 6, + "line": 5, }, }, "type": "text", - "val": "here", + "val": "!{here}", }, Object { "loc": Object { "end": Object { "column": 3, - "line": 7, + "line": 6, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 1, - "line": 7, + "line": 6, }, }, "type": "newline", @@ -26868,12 +26854,12 @@ Array [ "loc": Object { "end": Object { "column": 11, - "line": 7, + "line": 6, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 5, - "line": 7, + "line": 6, }, }, "type": "text", @@ -26884,12 +26870,12 @@ Array [ "loc": Object { "end": Object { "column": 31, - "line": 7, + "line": 6, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { "column": 11, - "line": 7, + "line": 6, }, }, "mustEscape": true, @@ -26899,12 +26885,12 @@ Array [ Object { "loc": Object { "end": Object { - "column": 31, + "column": 1, "line": 7, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { - "column": 31, + "column": 1, "line": 7, }, }, @@ -26913,12 +26899,12 @@ Array [ Object { "loc": Object { "end": Object { - "column": 31, + "column": 1, "line": 7, }, "filename": "/packages/pug-lexer/test/cases/interpolation.escape.pug", "start": Object { - "column": 31, + "column": 1, "line": 7, }, }, diff --git a/packages/pug-lexer/test/cases/interpolation.escape.pug b/packages/pug-lexer/test/cases/interpolation.escape.pug index cff251b6e..26db97392 100644 --- a/packages/pug-lexer/test/cases/interpolation.escape.pug +++ b/packages/pug-lexer/test/cases/interpolation.escape.pug @@ -1,7 +1,6 @@ - - var id = 42; foo | some | \#{text} - | here - | My ID #{"is {" + id + "}"} \ No newline at end of file + | \!{here} + | My ID #{"is {" + id + "}"}