|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | + |
| 8 | +const { readFileSync } = require('fs'); |
| 9 | +const { join } = require('path'); |
| 10 | +const { Linter } = require('eslint'); |
| 11 | +const lwcGraphAnalyzer = require('../../../lib/index'); |
| 12 | +const bundleAnalyzer = require('../../../lib/processor'); |
| 13 | +const baseConfig = require('../../../lib/configs/base'); |
| 14 | + |
| 15 | +/** |
| 16 | + * Test to verify programmatic linting of both HTML and JS content at runtime |
| 17 | + * using setLwcBundleFromContent() approach |
| 18 | + */ |
| 19 | +describe('Programmatic Bundle Linting', () => { |
| 20 | + it('should lint both HTML and JS files with runtime content', () => { |
| 21 | + // Create JS content with a getter that violates the rule |
| 22 | + // The getter has more than just a return statement AND is referenced in the template |
| 23 | + const jsContent = ` |
| 24 | +import { LightningElement, track } from 'lwc'; |
| 25 | +
|
| 26 | +export default class TestComponent extends LightningElement { |
| 27 | + data = []; |
| 28 | +
|
| 29 | + @track |
| 30 | + descriptionValue = ''; |
| 31 | +
|
| 32 | + get displayValue() { |
| 33 | + const value = this.data[0]; |
| 34 | + if (value) { |
| 35 | + return value.name; |
| 36 | + } |
| 37 | + return 'N/A'; |
| 38 | + } |
| 39 | +
|
| 40 | + handleDescriptionInputChange(event) { |
| 41 | + this.descriptionValue = event.detail.value; |
| 42 | + } |
| 43 | + |
| 44 | +}`; |
| 45 | + |
| 46 | + // HTML content that references the getter |
| 47 | + const htmlContent = `<template> |
| 48 | + <div>{displayValue}</div> |
| 49 | + <lightning-input |
| 50 | + type="text" |
| 51 | + label="Description" |
| 52 | + value={descriptionValue} |
| 53 | + onchange={handleDescriptionInputChange} |
| 54 | + ></lightning-input> |
| 55 | +</template>`; |
| 56 | + |
| 57 | + // 1. Set up the bundle with all content |
| 58 | + bundleAnalyzer.setLwcBundleFromContent( |
| 59 | + 'testComponent', // component base name |
| 60 | + jsContent, // JS content |
| 61 | + htmlContent // HTML content |
| 62 | + ); |
| 63 | + |
| 64 | + // 2. Configure ESLint with the rule that has violations in both files |
| 65 | + const pluginPrefix = '@salesforce/lwc-graph-analyzer'; |
| 66 | + const config = { |
| 67 | + ...baseConfig, |
| 68 | + plugins: { [pluginPrefix]: lwcGraphAnalyzer }, |
| 69 | + rules: { |
| 70 | + [`${pluginPrefix}/no-getter-contains-more-than-return-statement`]: 'error', |
| 71 | + [`${pluginPrefix}/no-composition-on-unanalyzable-property-non-public`]: 'error' |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + const linter = new Linter(); |
| 76 | + |
| 77 | + // 3. Lint JS file |
| 78 | + const jsErrors = linter.verify(jsContent, config, { |
| 79 | + filename: 'testComponent.js' |
| 80 | + }); |
| 81 | + |
| 82 | + // 4. Set up the bundle with all content again since the bundle is cleared after each lint call |
| 83 | + bundleAnalyzer.setLwcBundleFromContent( |
| 84 | + 'testComponent', // component base name |
| 85 | + jsContent, // JS content |
| 86 | + htmlContent // HTML content |
| 87 | + ); |
| 88 | + |
| 89 | + // 5. Lint HTML file |
| 90 | + const htmlErrors = linter.verify(htmlContent, config, { |
| 91 | + filename: 'testComponent.html' |
| 92 | + }); |
| 93 | + |
| 94 | + // Verify we got violations for the JS file |
| 95 | + // The displayValue getter violates the rule because it's referenced in HTML |
| 96 | + expect(jsErrors.length).toBeGreaterThan(0); |
| 97 | + const jsViolation = jsErrors.find( |
| 98 | + (err) => |
| 99 | + err.ruleId === |
| 100 | + '@salesforce/lwc-graph-analyzer/no-getter-contains-more-than-return-statement' |
| 101 | + ); |
| 102 | + expect(jsViolation).toBeDefined(); |
| 103 | + expect(jsViolation.ruleId).toBe( |
| 104 | + '@salesforce/lwc-graph-analyzer/no-getter-contains-more-than-return-statement' |
| 105 | + ); |
| 106 | + expect(jsViolation.message).toBe('Getters can only contain a return statement.'); |
| 107 | + expect(jsViolation.line).toBeGreaterThan(0); |
| 108 | + |
| 109 | + expect(htmlErrors.length).toBe(1); |
| 110 | + const htmlViolation = htmlErrors[0]; |
| 111 | + expect(htmlViolation).toBeDefined(); |
| 112 | + expect(htmlViolation.ruleId).toBe( |
| 113 | + '@salesforce/lwc-graph-analyzer/no-composition-on-unanalyzable-property-non-public' |
| 114 | + ); |
| 115 | + expect(htmlViolation.message).toBe( |
| 116 | + "This child component references an unanalyzable property 'descriptionValue' that’s not a public property." |
| 117 | + ); |
| 118 | + expect(htmlViolation.line).toBeGreaterThan(0); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should handle multiple HTML templates', () => { |
| 122 | + const jsContent = ` |
| 123 | +import { LightningElement } from 'lwc'; |
| 124 | +export default class MyComponent extends LightningElement { |
| 125 | + get value() { |
| 126 | + const x = 1; |
| 127 | + return x; |
| 128 | + } |
| 129 | +}`; |
| 130 | + |
| 131 | + const htmlContent1 = `<template><div>{value}</div></template>`; |
| 132 | + const htmlContent2 = `<template><div>Second template</div></template>`; |
| 133 | + |
| 134 | + // Set up bundle with multiple HTML templates |
| 135 | + bundleAnalyzer.setLwcBundleFromContent( |
| 136 | + 'myComponent', |
| 137 | + jsContent, |
| 138 | + htmlContent1, |
| 139 | + htmlContent2 |
| 140 | + ); |
| 141 | + |
| 142 | + const pluginPrefix = '@salesforce/lwc-graph-analyzer'; |
| 143 | + const config = { |
| 144 | + ...baseConfig, |
| 145 | + plugins: { [pluginPrefix]: lwcGraphAnalyzer }, |
| 146 | + rules: { |
| 147 | + [`${pluginPrefix}/no-getter-contains-more-than-return-statement`]: 'error' |
| 148 | + } |
| 149 | + }; |
| 150 | + |
| 151 | + const linter = new Linter(); |
| 152 | + |
| 153 | + // Lint JS file |
| 154 | + const jsErrors = linter.verify(jsContent, config, { |
| 155 | + filename: 'myComponent.js' |
| 156 | + }); |
| 157 | + |
| 158 | + // Should have violation because getter is referenced in HTML |
| 159 | + expect(jsErrors.length).toBeGreaterThan(0); |
| 160 | + const violation = jsErrors.find( |
| 161 | + (err) => |
| 162 | + err.ruleId === |
| 163 | + '@salesforce/lwc-graph-analyzer/no-getter-contains-more-than-return-statement' |
| 164 | + ); |
| 165 | + expect(violation).toBeDefined(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should return empty violations when no errors exist', () => { |
| 169 | + const jsContent = ` |
| 170 | +import { LightningElement } from 'lwc'; |
| 171 | +export default class MyComponent extends LightningElement { |
| 172 | + value = 'test'; |
| 173 | +}`; |
| 174 | + |
| 175 | + const htmlContent = `<template><div>{value}</div></template>`; |
| 176 | + |
| 177 | + bundleAnalyzer.setLwcBundleFromContent('myComponent', jsContent, htmlContent); |
| 178 | + |
| 179 | + const pluginPrefix = '@salesforce/lwc-graph-analyzer'; |
| 180 | + const config = { |
| 181 | + ...baseConfig, |
| 182 | + plugins: { [pluginPrefix]: lwcGraphAnalyzer }, |
| 183 | + rules: { |
| 184 | + [`${pluginPrefix}/no-getter-contains-more-than-return-statement`]: 'error' |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + const linter = new Linter(); |
| 189 | + |
| 190 | + const jsErrors = linter.verify(jsContent, config, { |
| 191 | + filename: 'myComponent.js' |
| 192 | + }); |
| 193 | + |
| 194 | + const htmlErrors = linter.verify(htmlContent, config, { |
| 195 | + filename: 'myComponent.html' |
| 196 | + }); |
| 197 | + |
| 198 | + // No violations expected since we're using a simple property, not a getter |
| 199 | + const jsViolations = jsErrors.filter( |
| 200 | + (err) => |
| 201 | + err.ruleId === |
| 202 | + '@salesforce/lwc-graph-analyzer/no-getter-contains-more-than-return-statement' |
| 203 | + ); |
| 204 | + const htmlViolations = htmlErrors.filter( |
| 205 | + (err) => |
| 206 | + err.ruleId === |
| 207 | + '@salesforce/lwc-graph-analyzer/no-getter-contains-more-than-return-statement' |
| 208 | + ); |
| 209 | + |
| 210 | + expect(jsViolations.length).toBe(0); |
| 211 | + expect(htmlViolations.length).toBe(0); |
| 212 | + }); |
| 213 | +}); |
0 commit comments