Skip to content

Adds LWC Parser to support unquoted interop attributes#5800

Merged
ikatyang merged 12 commits intoprettier:masterfrom
ntotten:lwc
Feb 1, 2019
Merged

Adds LWC Parser to support unquoted interop attributes#5800
ikatyang merged 12 commits intoprettier:masterfrom
ntotten:lwc

Conversation

@ntotten
Copy link
Copy Markdown
Member

@ntotten ntotten commented Jan 25, 2019

Fixes #5627

This pull requests adds a new parser option lwc. This is the same as the HTML parser, but it adds support for unquoted HTML attributes per the needs of LWC. See: #5627

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • (If not an internal change) I’ve added my changes to the CHANGELOG.unreleased.md file following the template.
  • I’ve read the contributing guidelines.

Try the playground for this PR

Comment thread CHANGELOG.unreleased.md
Comment thread CHANGELOG.unreleased.md Outdated
Comment thread CHANGELOG.unreleased.md
Comment thread CHANGELOG.unreleased.md Outdated
Comment thread src/language-html/index.js Outdated
Comment thread src/language-html/index.js Outdated
Comment thread src/language-html/index.js Outdated
Comment thread src/language-html/printer-html.js Outdated
return concat([node.rawName, "=", node.value]);
}

// lwc: html`<my-element data-for={value}></my-elememt>`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though the current implementation is fine, what do you think if we add a more advanced support that tries to parse and print the content in {here}? we could normalize quotes ({a+'str'} -> {a+"str"}) for example.

Copy link
Copy Markdown
Member Author

@ntotten ntotten Jan 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, that would certainly be nice. Is there any example of this somewhere? The value is just javascript so I'd imagine we could use the javascript parser/printer with some modifications (i.e. no trailing semicolon).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be just a matter to use textToDoc to get the Doc from regular JavaScript printer, remove all whitespaces and force printing it in flat mode. Something like:

try {
  const jsDoc = textToDoc(/* key={code} */ code, {
    parser: "__js_expression"
  });
  const jsDocWithoutWhitespaces = mapDoc(jsDoc, doc => 
    doc.type === "line" || doc.type === "break-parent"
      ? ""
      : doc.type === "if-break"
      ? doc.flatContents
      : typeof doc === "string"
      ? doc.replace(/\s/g, "")
      : doc
  });
  return concat([node.rawName, "={", jsDocWithoutWhitespaces, "}"]);
} catch (e) {
  // fallback to the current logic if it cannot be parsed
  return ...
}

Copy link
Copy Markdown
Member Author

@ntotten ntotten Jan 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ikatyang I tried this out and it seems that any time I put a quote or space inside the attribute value it breaks the parser completely because it isn't valid HTML. For example, I can't use:

<div data-one={foo["baz"]} data-two={foo[ "baz" ]}></div>

Neither of those options is successfully parsed. I am not sure what other cases would be fixed by this so I'm not sure if the textToDoc is needed at all. Let me know what you think.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's indeed an invalid HTML if there're spaces, which is expected. But it should be valid for quotes, this looks like a bug in the HTML parser, we probably need to open another issue to track this bug and add this feature in other PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened two bugs to track this. #5815 and #5816 to track these issues.

@ikatyang ikatyang added this to the 1.17 milestone Jan 30, 2019
@ikatyang ikatyang merged commit a093bb3 into prettier:master Feb 1, 2019
@ikatyang
Copy link
Copy Markdown
Member

ikatyang commented Feb 1, 2019

Thanks!

@lock lock Bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label May 2, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators May 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTML Templating requires unquoted attributes

2 participants