Browse Source

Fix evaluator bug when expression starts with let or const (#2224)

* fix evaluate regex to allow let* const* type variables in

* fix test

* test for const*

* save two chars per @SimoTod
danddanddand 3 years ago
parent
commit
253fd8fc14

+ 1 - 1
packages/alpinejs/src/evaluator.js

@@ -60,7 +60,7 @@ function generateFunctionFromString(expression, el) {
         // Support expressions starting with "if" statements like: "if (...) doSomething()"
         || /^[\n\s]*if.*\(.*\)/.test(expression)
         // Support expressions starting with "let/const" like: "let foo = 'bar'"
-        || /^(let|const)/.test(expression)
+        || /^(let|const)\s/.test(expression)
             ? `(() => { ${expression} })()`
             : expression
 

+ 30 - 0
tests/cypress/integration/directives/x-for.spec.js

@@ -446,3 +446,33 @@ test('x-for works with undefined',
         get('span').should(haveLength('1'))
     }
 )
+
+test('x-for works with variables that start with let',
+    `
+        <ul x-data="{ letters: ['a','b','c'] }">
+          <template x-for="letter in letters">
+            <li x-text="letter"></li>
+          </template>
+        </ul>
+    `,
+    ({ get }) => {
+        get('li:nth-of-type(1)').should(haveText('a'))
+        get('li:nth-of-type(2)').should(haveText('b'))
+        get('li:nth-of-type(3)').should(haveText('c'))
+    }
+)
+
+test('x-for works with variables that start with const',
+    `
+        <ul x-data="{ constants: ['a','b','c'] }">
+          <template x-for="constant in constants">
+            <li x-text="constant"></li>
+          </template>
+        </ul>
+    `,
+    ({ get }) => {
+        get('li:nth-of-type(1)').should(haveText('a'))
+        get('li:nth-of-type(2)').should(haveText('b'))
+        get('li:nth-of-type(3)').should(haveText('c'))
+    }
+)