pugjs / pug

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

Exception thrown on compile: Error parsing body of the with expression #3277

Open Bikeman868 opened 4 years ago

Bikeman868 commented 4 years ago

Pug version 3.0.0 Node version 12.16.3

Input JavaScript Values

  documentSchema(schema: Schema): Promise<string> {
    const render = pug.compileFile(path.join(__dirname, '../templates/schema.pug'));
    return render({ schema: schema });
  }
});

Input Pug

-  var camelToTitle = function(camel: string): string {
-    var result = camel.replace(/[\/\-]/g, " ");
-    result = result.replace(/([a-z0-9])([A-Z])/g, "$1 $2");
-    result = result.replace("  ", " ");
-    result = result.trim();
-    result = result.charAt(0).toUpperCase() + result.slice(1)
-    return result;
-  }
-  var formatDescription = function(description: any): string {
-    return description.replace(/\[([^[]+)\]\(([^(]+)\)/g, "<a href='$2'>$1</a>");
-  }

mixin asString(elem)
  | It's a string

mixin asNumber(elem)
  | It's a number

mixin asObject(elem)
  | It's an object

mixin asArray(elem)
  | It's an array

mixin asElement(elem)
  case elem.type
    when "number"
      +asNumber(elem)
    when "string"
      +asString(elem)
    when "array"
      +asArray(elem)
    default
      +asObject(elem)

mixin title(elem)
  if elem
    if elem.title
      #{elem.title}
    else if elem.id
      #{camelToTitle(elem.id)}
    else if elem["$id"]
      #{elem["$id"]}

mixin description(elem)
  if elem.description
    p(class="description")=formatDescription(elem.description)

mixin location
  h3(class="location") Schema references
  p You can use either of these formats to refer to this schema from within another schema.
  p(class="location") Schema URI
    a(href=schema.uri)= schema.uri
  if(schema.jsonSchema.id)
    p(class="location") Schema id
      b: a(href=schema.uri)= schema.jsonSchema.id

html
  head
    style(type="text/css").
        table, th, td {
            border: 1px solid black;
            border-collapse: collapse;
        }
        th, td {
            padding: 10px;
        }
  body
    div(class="schema")
      h2
        +title(schema.jsonSchema)
      +description(schema.jsonSchema)
      +location
      +asElement(schema.jsonSchema)

Additional Comments

When the Pug is compiled, it throws an exception "Error parsing body of the with expression". The stack trace provides no useful information for debugging my issue. My template does not contain any "with" expressions.

I tried commenting out various parts of my Pug template to narrow down the issue but was not able to find the part of the template that is triggering this exception.

Bikeman868 commented 4 years ago

I finally figured out what causes this. The JavaScript functions were copied from my app source and are written using TypeScript. Once I removed the type information the template compiles successfully.

It would be great if the exception thrown by Pug contained something that would at least identify the part of the script that was causing a problem. Thanks.

riskoviv commented 4 years ago

I encountered the same error today. The reason of that was in this part of pug file:

mixin textField(options)
  -
    var inputClassName = "text-field__input"
    if options.isMasked
      inputClassName += " text-field__input_masked"
    if options.isForceHoverStyle
      inputClassName += " text-field__input_force-focus-hover"

and that error is disappeared after I rewrote conditions to ternary operators:

mixin textField(options)
  -
    var inputClassName = "text-field__input"
    inputClassName += options.isMasked ? " text-field__input_masked" : ""
    inputClassName += options.isForceHoverStyle ? " text-field__input_force-focus-hover" : ""
rotsee commented 3 years ago

More generally, it seems like this error can be caused by any syntactical error in a multiline code block.

ngdangtu-vn commented 3 years ago

In my case this is cause by a typo const keyword of JavaScript. In short, this error message needs update for more detail. Too general message could cause dev 'panik'.

Kurohyou commented 2 years ago

This really needs to get fixed. At minimum, it needs a file reference when the problem is in an included pug file instead of only referencing the top most file in the include chain like it does now and a line/column number for the problem.

prodkt commented 1 year ago

I'm lost as to why this has never been addressed. This is clearly still open as I've encountered this myself. Am I to expect that closed tickets are really open tickets just brushed under the rug with this package?

PeterV989 commented 1 year ago

I agree with @prodkt as I just encountered it myself.

m10 commented 1 year ago

I'm lost as to why this has never been addressed. This is clearly still open as I've encountered this myself. Am I to expect that closed tickets are really open tickets just brushed under the rug with this package?

The status is "Open" since 2020. The message above is about a referencing Bug in webdiscus / pug-loader which won't be fixed since this is an upstream bug in pug.