Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Salesforce LWC Markup #3849

Open
rsoesemann opened this issue Mar 16, 2022 · 5 comments
Open

Support Salesforce LWC Markup #3849

rsoesemann opened this issue Mar 16, 2022 · 5 comments
Labels
an:enhancement An improvement on existing features / rules

Comments

@rsoesemann
Copy link
Member

Salesforce has 3 UI / Markup dialects from which only 2 can be parsed by PMD.

1) Visualforce: SUPPORTED using own Visualforce language module

Sample code: https://github.com/rsoesemann/salesforce-recipes/blob/master/force-app/main/default/pages/activateAccountButton.page parses using Visualforce language module
Screenshot_2022-03-16_at_11_03_09

2) Aura Component Markup: SUPPORTED

Sample code: https://github.com/rsoesemann/salesforce-recipes/blob/master/force-app/main/default/aura/resultToast/resultToast.cmp parses using Visualforce language module
Cursor_and_PMD_Rule_Designer__v_6_37_0_

3) LWC Component Markup: NOT SUPPORTED

Sample code: https://raw.githubusercontent.com/trailheadapps/lwc-recipes-oss/main/src/modules/ui/app/app.html
doesn't parse. Neighter as XML nor anything else.
Cursor_and_PMD_Rule_Designer__v_6_37_0_-2

@rsoesemann rsoesemann added the an:enhancement An improvement on existing features / rules label Mar 16, 2022
@rsoesemann rsoesemann changed the title PMD to parse and analyse Salesforce LWC markup Support Salesforce LWC Markup Mar 16, 2022
@adangel
Copy link
Member

adangel commented Mar 17, 2022

The error message is:

 org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 23; Open quote is expected for attribute "nav-items" associated with an  element type  "ui-navbar".

It's not a valid XML format, e.g.

<ui-navbar nav-items={navigationItems} ...

should be

<ui-navbar nav-items="{navigationItems}" ...

These curly braces look like this is actually a template (also the "if:true" pseudo attributes later on) and the "XML" is probably not used as-is.
We would need to preprocess it if we want to use the standard XML parser or would need to write an own parser.
Preprocessing has the downside, that the analysis happens not on the original source, and it could introduce other errors.

In the documentation they use "HTML" for the description of this format: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.create_components_html

"A template is valid HTML with a root tag."

But HTML is not supported by PMD today (#3521).

@rsoesemann
Copy link
Member Author

rsoesemann commented Mar 17, 2022

@adangel thanks for looking into this. So there is still something in this world like not well-formed HTML.
I know Salesforce LWC is following industry Web Components standards and had hoped this mean well-formed XML. But no...

Can you elaborate a bit on why PMD never supported HTML? Because there exist no compatible parsers or because nobody contributed it or what else?

I asked in salesforce/lwc#2749 which format they use and if there is a public parser?

@adangel
Copy link
Member

adangel commented Mar 18, 2022

nobody contributed

Yes, nobody contributed.

I had a look a https://jsoup.org/ - this is a lot more relaxed for parsing. I could successfully parse the snippet you mentioned via Parser.xmlParser().parseInput(...). Like the DOM from plain java xml, this DOM also doesn't support positional info. But we had created a solution back then already (which involves going through the file once more): DOMLineNumbers

@oowekyala
Copy link
Member

For PMD 7 I wrote a javadoc parser, which as you know includes HTML. This supports position information correctly, maybe we could extract it. I like Jsoup, but its API also allows mutating everything, which PMD rules shouldn't be doing.

@niteshCopado
Copy link

Hi @rsoesemann @adangel Will there be a plan for PMD to support LWC?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
an:enhancement An improvement on existing features / rules
Projects
None yet
Development

No branches or pull requests

4 participants