Lightning Web Components and Aura¶
Standards for building a Lightning component, whatever its languages. The JavaScript a component runs is held to the JavaScript standards, and the markup it renders to the HTML and Visualforce standards, Jest tests included (Testing). This page holds what is true of the component itself.
Lightning Web Components Before Aura¶
- Lightning Web Components is the default framework for custom UI. The Aura Components framework is used only where a Lightning web component cannot do the same thing.
- Lightning Web Components performs better and is easier to develop, and the feature gap between Aura and LWC is shrinking (Introducing Aura Components, Aura Development with Salesforce Extensions).
- Lightning Web Components must be used unless a feature that the component needs is supported only in Aura (How to Choose Lightning Web Components or Aura, Use Lightning Web Components instead of Aura Components).
- The Centre of Excellence (CoE) checks the choice between Lightning Web Components and Aura Components at design time, when it approves the design document.
- Aura components are held to these standards all the same. ESLint checks their JavaScript code (Version Control: ESLint), and PMD HTML rules check Aura components as well (Version Control: PMD).
Sources:
- Aura Development with Salesforce Extensions - Salesforce Extensions for Visual Studio Code
- How to Choose Lightning Web Components or Aura - Lightning Web Components Developer Guide
- Introducing Aura Components - Lightning Aura Components Developer Guide
- Use Lightning Web Components instead of Aura Components - Lightning Aura Components Developer Guide
Lightning Web Security¶
Lightning Web Security is the security architecture of Lightning components. It runs the components of each namespace in a JavaScript sandbox of their own, so that a component cannot read or change what belongs to the platform or to a component of another namespace, and it alters the APIs that would let one try: cookies, browser storage, and the globals of the page are partitioned by namespace, and HTML that a component inserts is sanitised (How Lightning Web Security Works). It replaces Lightning Locker, it is on by default in a new org, and it is on or off for the whole org: once a component relies on a feature that only Lightning Web Security offers, the org cannot go back to Lightning Locker (How Lightning Web Security Compares to Lightning Locker, Considerations Before Using LWS-Only Features). The org setting is a standard of the Settings page (Lightning Web Security). The standards below keep a component working under it, and come from Salesforce's own guidance on building components to work with Lightning Web Security.
Strict Mode¶
Lightning Web Security and Lightning Locker enforce JavaScript strict mode everywhere, whether or not the code says "use strict", so an unsafe action that a browser would let pass is an error at run time (JavaScript Strict Mode Enforcement).
- Every variable is declared, with
const,let, orvar. A variable that is used and never declared blocks the pull request (severity 2). Enforced byno-undef. - Nothing is assigned to a global, and no top-level declaration becomes one. An assignment to a read-only global blocks the pull request (severity 2), and a top-level declaration that makes a global is reported at severity 3. Enforced by
no-global-assignandno-implicit-globals.
Objects the Component Does Not Own¶
A change to an object that the component's namespace does not own is not propagated out of the sandbox, so the code that receives the object sees a property as undefined (Avoid Mutating Objects).
- A function does not add, remove, or change a property of an argument it receives, and a handler does not change the data of an event. Clone the object, change the clone, and pass or dispatch that. A property assigned to a parameter is reported at severity 3. Enforced by
no-param-reassign. - No property is added to a standard JavaScript object or to a DOM element. Data that belongs with an element goes in its
dataset(Avoid Custom Properties on Standard JavaScript Objects). A property added to a built-in object blocks the pull request (severity 2). Enforced byno-extend-native.
The Globals top and location¶
Lightning Web Security gives a component top and location through the globals, and rewrites the code that reaches them, so a variable with one of those names makes the rewrite fail (Lightning Web Security Limitations).
topandlocationare reached aswindow.top,window.location,document.location,globalThis.location, orself.location, and no variable shadows or overrides them:const loc = window.location.href, notconst location = window.location.href. A variable, parameter, function, class, or import namedtoporlocationblocks the pull request (severity 2), and so does a variable that shadows a restricted name such asundefined. Enforced byno-restricted-syntax, with the selectors of the sample ESLint file, andno-shadow-restricted-names.
Aura Globals¶
- A Lightning web component uses no
$A,Aura,Sfdc, orsforce. Lightning Web Security blocks them, because they are deprecated or not supported for Lightning web components (Salesforce Global Variables Blocked by LWS). A reference to any of them, or an import of an Aura library, blocks the pull request (severity 2). Enforced by@lwc/lwc-platform/no-aurafor$A,no-undeffor the other three, and@lwc/lwc-platform/no-aura-libsfor an Aura library.
Downloads and Blobs¶
- A download link is made from a
Blob, withURL.createObjectURL, and the object URL is revoked once the link has been used. Adata:URI in a link is blocked by Lightning Web Security, because it embeds content in the URL with no origin to bind it to (Use Blob Objects for Download Links). No rule checks this. Reviewers do. - A
Blobis created with an explicit MIME type from the list that Lightning Web Security permits, such astext/plain,application/json, orapplication/octet-streamfor any other binary file. Without a type the object is not created with the expected content, and a type outside the list is blocked (Specify a MIME Type for Blob Objects). No rule checks this. Reviewers do.
Input and Inserted HTML¶
Lightning Web Security sanitises the HTML and SVG that a component inserts into the page, and never its input (Lightning Web Security Sanitization).
- A component validates its own input, before it is used and before it is sent to Apex.
- No HTML is set from a string: no
innerHTML,outerHTML, orinsertAdjacentHTML. The template renders the markup, andlwc:dom="manual"with a sanitised string is the exception a reviewer approves. HTML set from a string blocks the pull request (severity 2). Enforced by@lwc/lwc/no-inner-html.
Scripts and Resources¶
The Content Security Policy of the platform allows a script only from the org, and every other external resource only over HTTPS from the org or from a trusted URL, with no inline script and no inline event handler (Content Security Policy Overview).
- A JavaScript library is uploaded to the org as a static resource and loaded from there. A font, an image, a frame, or a stylesheet from outside the org is loaded over HTTPS from a trusted URL. No
scripttag and no inline event handler is written in markup. The policy enforces this in the browser. In Aura markup, an event attribute that holds a script, such asonclick="alert('x')", in place of a controller action such as{!c.handleClick}, is reported at severity 3. Enforced byNoInlineEventHandlerAura. Ajavascript:value is reported byAvoidJavaScriptUrlHtmlLwcAurainstead, and a reviewer checks ascripttag. For a Visualforce page the same standard is on the HTML and Visualforce page (External Scripts and Stylesheets).
Sources:
- Avoid Custom Properties on Standard JavaScript Objects - Security for Lightning Components
- Avoid Mutating Objects - Security for Lightning Components
- Considerations Before Using LWS-Only Features - Security for Lightning Components
- Content Security Policy (CSP) - Security for Lightning Components
- How Lightning Web Security (LWS) Compares to Lightning Locker - Security for Lightning Components
- How LWS Works - Security for Lightning Components
- JavaScript Strict Mode Enforcement - Security for Lightning Components
- LWS Limitations - Security for Lightning Components
- LWS Sanitization - Security for Lightning Components
- Salesforce Global Variables Blocked by LWS - Security for Lightning Components
- Specify a MIME Type for Blob Objects - Security for Lightning Components
- Use Blob Objects for Download Links - Security for Lightning Components
Lightning Web Components¶
Readability¶
Code Style¶
- No trailing comma after the last item of an object literal or an array literal. Prettier removes it (
trailingCommaisnonein the Prettier configuration of these standards), so that every component reads alike. One that remains blocks the pull request (severity 2); enforced by the PMD JavaScript ruleAvoidTrailingComma.
Formatting¶
TODO
Stub. The Prettier and purely cosmetic standards of Lightning Web Components, such as bracket spacing and tabs (Content item 142).
Required Formatting¶
TODO
Stub. Formatting that the platform requires, as against formatting that these standards choose (Content item 142).
Error Prevention¶
TODO
Stub. Code choices in Lightning Web Components that work today and could cause a problem later (Content item 142).
-
An event is dispatched with
bubbles: falseandcomposed: false, the defaults, and never with both set totrue. An event with both set totruecrosses every shadow boundary, so it becomes part of the API of every component above it (Events Best Practices). -
An event with both set to
trueblocks the pull request (severity 2); enforced byAvoidLwcBubblesComposedTrueandno-restricted-syntax, with a selector of the sample ESLint file. Keep both rules:AvoidLwcBubblesComposedTruealso finds options kept in a variable, and theno-restricted-syntaxselector also finds an event that is the first statement of a method. - A component that Lightning Out 2.0 runs needs both, and, after approval, gets a suppression with a documented reason (Handle Lightning Out 2.0 App Events).
Accessibility¶
Salesforce's guidance for accessible Lightning web components is in the Lightning Web Components Developer Guide. It advises following WCAG, and using a base component where one fits, because base components have accessibility built in (Component Accessibility). It advises a label on every control, and aria-label only where no label shows or where a screen reader needs more text than the label gives (Accessibility Attributes). Its base components give images and icons an alternative-text attribute for WCAG 1.1.1 (Base Components Accessibility). The standards for alternative text and for the names of buttons and links are on the General: Accessibility page. The standards for tables, form groups, frames, lists, and media in the markup of a component are on the HTML and Visualforce: Accessibility page. A template tag may sit between a list and its items, and an li at the root of a component's template is not reported, because its list is in the parent component.
Sources:
- Accessibility Attributes - Lightning Web Components Developer Guide
- Base Components Accessibility - Lightning Web Components Developer Guide
- Component Accessibility - Lightning Web Components Developer Guide
- Events Best Practices - Lightning Web Components Developer Guide
- Handle Lightning Out 2.0 App Events - Lightning Web Components Developer Guide
Aura¶
- Every Aura component is at API version 40.0 or later. Below 40.0 an Aura component runs with Lightning Locker off, outside the security architecture of the org (AvoidAuraWithLockerDisabled). The API version standard already asks for far more than 40.0; this rule is kept for what the lower number means, and blocks the pull request (severity 1). Enforced by
AvoidAuraWithLockerDisabled. - An Aura application should not be a Lightning Out dependency app, which extends
ltng:outApporltng:outAppUnstyled. Such an app is Lightning Out (beta), which requires Lightning Locker; Lightning Out 2.0 is recommended for a component in an external app (How Lightning Out 2.0 Compares to Lightning Out (Beta), Lightning Web Security). A Lightning Out dependency app is reported and does not block (severity 3), and an organisation may allow one with a suppression and a documented reason. Enforced byNoLightningOutApplicationAura.
TODO (Content item 116)
The Aura standards that Lightning Web Security Limitations sets out: no async/await and no dynamic import in an Aura component or in a static resource it loads, since they fail at run time under Lightning Web Security; and $A is not available to eval() or the Function constructor.
Readability¶
Code Style¶
TODO
Stub. Standards that make Aura components easier to read and that may not change what they do: stylistic choices (Content item 142).
Formatting¶
TODO
Stub. The Prettier and purely cosmetic standards of Aura components, such as bracket spacing and tabs (Content item 142).
Required Formatting¶
- No trailing comma after the last item of an object literal or an array literal in an Aura component: Salesforce requires it (Prettier Code Formatter). One that remains blocks the pull request (severity 2); enforced by the PMD JavaScript rule
AvoidTrailingComma.
Error Prevention¶
TODO
Stub. Code choices in Aura components that work today and could cause a problem later (Content item 142).
Accessibility¶
Salesforce's guidance for accessible Aura components starts at Supporting Accessibility. It advises keeping the code that makes a component accessible, such as its aria attributes, when the component is changed, and following WCAG. Its pages cover the accessibility of the base components (Accessibility for Lightning Base Components), informational and decorative images and icons (Using Images and Icons), button labels (Button Labels), and tests that check a rendered component for common faults, such as an image with no alt attribute (Write Aura Component Accessibility Tests). The standards for alternative text and for the names of buttons and links are on the General: Accessibility page. The standards for tables, form groups, frames, lists, and media in the markup of a component are on the HTML and Visualforce: Accessibility page. An aura:iteration or aura:if tag may sit between a list and its items, and an li at the root of an aura:component is not reported, because its list is in the parent component. In an aura:application, an li with no list above it is reported.
An Organisation Without Aura¶
These standards recommend Lightning Web Components over Aura (Lightning Web Components Before Aura). A project that may still get an Aura component keeps every Aura item, so that the first Aura component is checked from its first commit (Version Control: ESLint). An organisation that has moved off Aura, and forbids new Aura components, may:
- Leave out the
@salesforce/eslint-plugin-aurapackage, and the Aura blocks ofeslint.config.js. - Remove the second PMD run that reads Aura markup, which an organisation needs only if it runs PMD without Code Analyzer (Appendix F.2: Aura Markup and PMD Alone).
An organisation forbids Aura by making the Aura component bundle a Tier 0 type, in every Tier 0 layer (Tier 0 Enforcement). An Aura file is then blocked, not just skipped.
**/aura/in.forceignoreand in.gitignore, so that the Salesforce CLI never deploys or retrieves an Aura bundle, and Git does not stage one.'**/aura/**'in thetier0list ofmetadata-risk-tiers.ymland in the Tier 0 push ruleset, so that a push or a pull request with an Aura file is rejected.AuraDefinitionBundlein themetadataTypesproperty ofProhibitedMetadataType.
In an organisation that forbids Aura, no Aura bundles are committed. The metadata file of an Aura bundle blocks the pull request (severity 1). Enforced by ProhibitedMetadataType.
Sources:
- Accessibility for Lightning Base Components - Lightning Aura Components Developer Guide
- Button Labels - Lightning Aura Components Developer Guide
- How Lightning Out 2.0 Compares to Lightning Out (Beta) - Lightning Web Components Developer Guide
- PMD AppExchange Rules Reference - Salesforce Code Analyzer
- Prettier Code Formatter - Salesforce Extensions for Visual Studio Code
- Supporting Accessibility - Lightning Aura Components Developer Guide
- Using Images and Icons - Lightning Aura Components Developer Guide
- Write Aura Component Accessibility Tests - Lightning Aura Components Developer Guide
Comments¶
The General: Comments standards apply to Lightning web components and Aura components. Each part of a component is read with the files of its kind:
- A Lightning Web Component template and Aura markup are read as markup (HTML and Visualforce: Comments).
- The JavaScript of a Lightning web component or an Aura component is read as JavaScript (JavaScript: Comments).
Rules¶
The rules that this page mentions. The Standard column gives each rule's standard in one line, and the Section column links to the section that mentions the rule; "Not yet written" marks a rule of this page that no section mentions yet. Appendix F.6 holds the full cross-reference.
| Rule | Kind | Severity | Standard | Section |
|---|---|---|---|---|
Avoid |
Stock Code Analyzer PMD | 1 | Every Aura component is at API version 40.0 or later | Aura |
Prohibited |
Custom PMD XPath | 1 | No metadata file of a Tier 0 type is committed to version control | An Organisation Without Aura |
@lwc/ |
Stock Code Analyzer ESLint | 2 | No $A, Aura, Sfdc, or sforce in a Lightning web component |
Aura Globals |
@lwc/ |
Stock Code Analyzer ESLint | 2 | No import of an Aura library in a Lightning web component | Aura Globals |
@lwc/ |
Stock Code Analyzer ESLint | 2 | No HTML set from a string: no innerHTML, outerHTML, or insertAdjacentHTML |
Input and Inserted HTML |
Avoid |
Custom PMD XPath | 2 | No attribute in Lightning markup or plain HTML holds a javascript: URL, except javascript:void(0) |
Scripts and Resources |
Avoid |
Stock Code Analyzer PMD | 2 | No custom event with both bubbles and composed set to true |
Error Prevention |
Avoid |
Stock PMD | 2 | No trailing comma after the last item of an object literal or an array literal | Code Style Required Formatting |
no-extend-native |
Stock Code Analyzer ESLint | 2 | No property is added to a built-in JavaScript object | Objects the Component Does Not Own |
no-global-assign |
Stock Code Analyzer ESLint | 2 | Nothing is assigned to a read-only global | Strict Mode |
no-restricted-syntax |
Stock Code Analyzer ESLint | 2 | No variable, parameter, function, class, or import named top or location |
The Globals top and location Error Prevention |
no-shadow-restricted-names |
Stock Code Analyzer ESLint | 2 | No variable named undefined, NaN, Infinity, arguments, or eval |
The Globals top and location |
no-undef |
Stock Code Analyzer ESLint | 2 | Every variable is declared, with const, let, or var |
Strict Mode Aura Globals |
no-implicit-globals |
Stock Code Analyzer ESLint | 3 | No top-level declaration becomes a global | Strict Mode |
no-param-reassign |
Stock Code Analyzer ESLint | 3 | A function does not change a property of an argument it receives | Objects the Component Does Not Own |
No |
Custom PMD XPath | 3 | An event attribute in Aura markup names a controller action, not a script | Scripts and Resources |
No |
Custom PMD XPath | 3 | An Aura application should not be a Lightning Out (beta) dependency app | Aura |
No |
Custom PMD XPath | 3 | A Visualforce page or component should not use Lightning Out (beta) | HTML and Visualforce: External Scripts and Stylesheets |