pugjs / pug

Pug – robust, elegant, feature rich template engine for Node.js
https://pugjs.org
21.7k stars 1.95k forks source link

`each` fails with variable named "offerings" #3447

Open fpereira1 opened 1 month ago

fpereira1 commented 1 month ago

Pug Version: 3.0.3

Node Version: 18.12.1

Input JavaScript Values

import pug from 'pug';

const offerings = {
  offering01: {
    title: 'Offering 01',
    description: 'This is the first offering.',
    price: 100,
  },
  offering02: {
    title: 'Offering 02',
    description: 'This is the second offering.',
    price: 200,
  }
};

const pugTemplate = `
p Offerings

each offering, key in offerings
  h2= offering.title
  p= offering.description
  p= offering.price
  a(href='https://example.com/offering/' + key) More info
`;

console.log(
  pug.render(
    pugTemplate,
    {offerings}
  )
);

Additional Comments

This issue seems related to or possible the same as this other closed issue: https://github.com/pugjs/pug/issues/3263

It would be easy to convert the structure from an object to an array as a workaround, but pug should handle both

The code above fails with the following error:

   var err = new Error(fullMessage);
              ^

Error: Pug:4:32
    2| p Offerings
    3|
  > 4| each offering, key in offerings
--------------------------------------^
    5|   h2= offering.title
    6|   p= offering.description
    7|   p= offering.price

The value variable for each must either be a valid identifier (e.g. `item`) or a pair of identifiers in square brackets (e.g. `[key, value]`).
    at makeError (/pug_bug_of/node_modules/pug-error/lib/index.js:34:15)
    at Lexer.error (/pug_bug_of/node_modules/pug-lexer/index.js:62:15)
    at Lexer.eachOf (/pug_bug_of/node_modules/pug-lexer/index.js:1108:14)
    at Lexer.callLexerFunction (/pug_bug_of/node_modules/pug-lexer/index.js:1647:23)
    at Lexer.advance (/pug_bug_of/node_modules/pug-lexer/index.js:1676:12)
    at Lexer.callLexerFunction (/pug_bug_of/node_modules/pug-lexer/index.js:1647:23)
    at Lexer.getTokens (/pug_bug_of/node_modules/pug-lexer/index.js:1706:12)
    at lex (/pug_bug_of/node_modules/pug-lexer/index.js:12:42)
    at Object.lex (/pug_bug_of/node_modules/pug/lib/index.js:104:9)
    at Function.loadString [as string] (/pug_bug_of/node_modules/pug-load/index.js:53:24) {
  code: 'PUG:MALFORMED_EACH_OF_LVAL',
  msg: 'The value variable for each must either be a valid identifier (e.g. `item`) or a pair of identifiers in square brackets (e.g. `[key, value]`).',
  line: 4,
  column: 32,
  filename: undefined,
  src: '\n' +
    'p Offerings\n' +
    '\n' +
    'each offering, key in offerings\n' +
    '  h2= offering.title\n' +
    '  p= offering.description\n' +
    '  p= offering.price\n' +
    "  a(href='https://example.com/offering/' + key) More info\n",
  toJSON: [Function (anonymous)]