Skip to content

forcedotcom/sf-skills / plugins/builder/experience-lwc/skills/experience-lwc-generate

experience-lwc-generate

Unverified

Lightning Web Components with PICKLES methodology and 165-point scoring. Use this skill when the user creates or edits LWC components, builds wire service patterns, or writes Jest tests for LWC. TRIGGER when: user creates/edits LWC components, touches lwc/**/*.js, .html, .css, .js-meta.xml files, or asks about wire service, SLDS, or Jest LWC tests. DO NOT TRIGGER when: Apex classes (use platform-apex-generate), Aura components, or Visualforce.

FileKindSize
README.mdmarkdown4.1 KB
SKILL.mdskill md9.6 KB
assets/apex-controller/LwcController.clsother12.0 KB
assets/basic-component/basicComponent.cssother1.5 KB
assets/basic-component/basicComponent.htmlother4.5 KB
assets/basic-component/basicComponent.jsscript7.3 KB
assets/basic-component/basicComponent.js-meta.xmlconfig4.5 KB
assets/datatable-component/datatableComponent.htmlother4.1 KB
assets/datatable-component/datatableComponent.jsscript13.1 KB
assets/flow-screen-component/flowScreenComponent.cssother1.7 KB
assets/flow-screen-component/flowScreenComponent.htmlother7.1 KB
assets/flow-screen-component/flowScreenComponent.jsscript13.9 KB
assets/flow-screen-component/flowScreenComponent.js-meta.xmlconfig3.9 KB
assets/form-component/formComponent.htmlother6.4 KB
assets/form-component/formComponent.jsscript11.3 KB
assets/graphql-component/graphqlComponent.htmlother4.8 KB
assets/graphql-component/graphqlComponent.jsscript10.7 KB
assets/jest-test/componentName.test.js.exampleother15.6 KB
assets/message-channel/RecordSelected.messageChannel-meta.xmlconfig2.4 KB
assets/message-channel/lmsPublisher.jsscript3.8 KB
assets/message-channel/lmsSubscriber.jsscript7.0 KB
assets/modal-component/modalComponent.htmlother3.2 KB
assets/modal-component/modalComponent.jsscript5.5 KB
assets/record-picker/recordPicker.htmlother2.0 KB
assets/record-picker/recordPicker.jsscript5.4 KB
assets/state-store/store.jsscript9.1 KB
assets/typescript-component/typescriptComponent.cssother1.3 KB
assets/typescript-component/typescriptComponent.htmlother4.3 KB
assets/typescript-component/typescriptComponent.js-meta.xmlconfig2.4 KB
assets/typescript-component/typescriptComponent.test.ts.exampleother13.6 KB
assets/typescript-component/typescriptComponent.tsscript11.7 KB
assets/workspace-api/workspaceComponent.htmlother3.4 KB
assets/workspace-api/workspaceComponent.jsscript8.0 KB
references/accessibility-guide.mdmarkdown22.3 KB
references/advanced-features.mdmarkdown2.8 KB
references/async-notification-patterns.mdmarkdown19.7 KB
references/cli-commands.mdmarkdown12.6 KB
references/component-patterns.mdmarkdown44.5 KB
references/flow-integration-guide.mdmarkdown19.0 KB
references/jest-testing.mdmarkdown25.3 KB
references/lms-guide.mdmarkdown21.9 KB
references/lwc-best-practices.mdmarkdown36.3 KB
references/performance-guide.mdmarkdown20.3 KB
references/scoring-and-testing.mdmarkdown3.8 KB
references/slds-blueprints.jsonconfig781.3 KB
references/slds-design-guide.mdmarkdown10.7 KB
references/state-management.mdmarkdown19.9 KB
references/template-anti-patterns.mdmarkdown20.1 KB
references/triangle-pattern.mdmarkdown12.5 KB
scripts/local-dev-preview.shscript1.1 KB
scripts/lwc-lsp-validate.pyscript8.6 KB
scripts/post-tool-validate.pyscript14.7 KB
scripts/slds_data/deprecated_patterns.jsonconfig3.4 KB
scripts/slds_data/styling_hooks.jsonconfig2.9 KB
scripts/slds_data/valid_slds_classes.jsonconfig7.4 KB
scripts/slds_linter_wrapper.pyscript8.9 KB
scripts/slds_rules/__init__.pyscript593 B
scripts/template_validator.pyscript13.5 KB
scripts/validate_slds.pyscript26.5 KB

scripts/template_validator.py

View on GitHub
#!/usr/bin/env python3
"""
LWC Template Anti-Pattern Validator.

Detects common mistakes that LLMs make when generating LWC templates:
1. Inline JavaScript expressions ({a + b})
2. Ternary operators in templates ({x ? a : b})
3. Object/array literals in attributes
4. Method calls in templates ({items.length})
5. Comparison operators in if:true
6. Event handlers with inline arguments

This validator is ADVISORY - it provides warnings but does not block operations.

Source: https://salesforcediaries.com/2026/01/16/llm-mistakes-in-apex-lwc-salesforce-code-generation-rules/
"""

import re
import os
from typing import Dict, List, Set


class LWCTemplateValidator:
    """Detects LLM-specific anti-patterns in LWC HTML templates."""

    # Patterns for inline expressions (arithmetic, concatenation)
    INLINE_EXPRESSION_PATTERNS = [
        # Arithmetic operations
        (r'\{[^}]*\s*[\+\-\*\/]\s*[^}]*\}', 'Arithmetic expression', 'Use a getter in your JS file'),
        # Ternary operators
        (r'\{[^}]*\s*\?\s*[^}]+\s*:\s*[^}]*\}', 'Ternary operator', 'Use a getter or if:true/if:false'),
        # String concatenation with +
        (r"\{[^}]*['\"]\s*\+\s*[^}]*\}", 'String concatenation', 'Use a getter with template literals'),
        # Template literals (backticks)
        (r'\{`[^`]*`\}', 'Template literal', 'Template literals not supported; use getter'),
    ]

    # Patterns for method calls in templates
    METHOD_CALL_PATTERNS = [
        # .method() call
        (r'\{[^}]*\.\w+\s*\(\s*[^)]*\)\s*[^}]*\}', 'Method call', 'Use a getter instead of calling methods'),
        # .length, .size, etc. (common array/string properties that need getters)
        (r'\{[^}]*\.length[^}]*\}', 'Array/string .length', 'Use a getter: get count() { return this.items.length; }'),
        (r'\{[^}]*\.size[^}]*\}', 'Collection .size', 'Use a getter'),
        # Common method patterns
        (r'\{[^}]*\.toUpperCase\s*\(\)', 'toUpperCase()', 'Use a getter'),
        (r'\{[^}]*\.toLowerCase\s*\(\)', 'toLowerCase()', 'Use a getter'),
        (r'\{[^}]*\.trim\s*\(\)', 'trim()', 'Use a getter'),
        (r'\{[^}]*\.toString\s*\(\)', 'toString()', 'Use a getter'),
        (r'\{[^}]*\.join\s*\(', 'join()', 'Use a getter'),
        (r'\{[^}]*\.slice\s*\(', 'slice()', 'Use a getter'),
        (r'\{[^}]*\.split\s*\(', 'split()', 'Use a getter'),
        (r'\{[^}]*\.filter\s*\(', 'filter()', 'Use a getter'),
        (r'\{[^}]*\.map\s*\(', 'map()', 'Use a getter'),
        (r'\{[^}]*\.find\s*\(', 'find()', 'Use a getter'),
        (r'\{[^}]*\.includes\s*\(', 'includes()', 'Use a getter'),
        (r'\{[^}]*JSON\.stringify\s*\(', 'JSON.stringify()', 'Use a getter'),
        (r'\{[^}]*JSON\.parse\s*\(', 'JSON.parse()', 'Use a getter'),
    ]

    # Patterns for comparisons in if:true/if:false
    COMPARISON_PATTERNS = [
        # Comparison operators in if:true
        (r'if:true=\{[^}]*\s*[><=!]+\s*[^}]*\}', 'Comparison in if:true', 'Use a getter: get isGreater() { return x > 5; }'),
        (r'if:false=\{[^}]*\s*[><=!]+\s*[^}]*\}', 'Comparison in if:false', 'Use a getter'),
        # Logical operators
        (r'if:true=\{[^}]*\s*&&\s*[^}]*\}', 'Logical AND in if:true', 'Use a getter: get bothTrue() { return a && b; }'),
        (r'if:false=\{[^}]*\s*&&\s*[^}]*\}', 'Logical AND in if:false', 'Use a getter'),
        (r'if:true=\{[^}]*\s*\|\|\s*[^}]*\}', 'Logical OR in if:true', 'Use a getter'),
        (r'if:false=\{[^}]*\s*\|\|\s*[^}]*\}', 'Logical OR in if:false', 'Use a getter'),
        # Negation
        (r'if:true=\{!\w', 'Negation in if:true', 'Use if:false instead, or use a getter'),
    ]

    # Patterns for object/array literals
    LITERAL_PATTERNS = [
        # Object literal in attribute
        (r'=\{\s*\{[^}]+\}\s*\}', 'Inline object literal', 'Define objects in your JS file as properties'),
        # Array literal in attribute
        (r"=\{\s*\[[^\]]+\]\s*\}", 'Inline array literal', 'Define arrays in your JS file as properties'),
    ]

    # Patterns for incorrect event handler syntax
    EVENT_HANDLER_PATTERNS = [
        # Event handler with parentheses/arguments
        (r'on\w+=\{[\w.]+\s*\([^)]+\)\s*\}', 'Event handler with arguments', 'Use data-* attributes instead'),
        # Arrow function in handler
        (r'on\w+=\{\s*\([^)]*\)\s*=>', 'Arrow function in handler', 'Define handler method in JS, use data-* for context'),
        # .bind() in handler
        (r'on\w+=\{[\w.]+\.bind\s*\(', '.bind() in handler', 'Use data-* attributes for context instead'),
    ]

    # Patterns for common Vue/React/Angular syntax mistakes
    FRAMEWORK_SYNTAX_PATTERNS = [
        # Vue v-model / v-bind / v-on
        (r'\bv-model\s*=', 'Vue v-model syntax', 'LWC uses value={prop} with onchange handler'),
        (r'\bv-bind:', 'Vue v-bind syntax', 'LWC uses {property} binding'),
        (r'\bv-on:', 'Vue v-on syntax', 'LWC uses on* handlers (onclick, onchange)'),
        (r'\bv-if\s*=', 'Vue v-if syntax', 'LWC uses if:true={condition}'),
        (r'\bv-for\s*=', 'Vue v-for syntax', 'LWC uses for:each={array} for:item="item"'),
        (r'\bv-show\s*=', 'Vue v-show syntax', 'LWC uses if:true/if:false or CSS classes'),
        # React patterns
        (r'\bclassName\s*=', 'React className', 'LWC uses class={classString}'),
        (r'\bhtmlFor\s*=', 'React htmlFor', 'LWC uses for attribute or lightning-input label'),
        (r'\bonClick\s*=\s*\{[^}]*\(\s*\)', 'React onClick with call', 'LWC uses onclick={handler} without parentheses'),
        # Angular patterns
        (r'\[\(ngModel\)\]', 'Angular two-way binding', 'LWC uses value={prop} with onchange'),
        (r'\*ngIf\s*=', 'Angular *ngIf', 'LWC uses if:true={condition}'),
        (r'\*ngFor\s*=', 'Angular *ngFor', 'LWC uses for:each={array} for:item="item"'),
        (r'\(click\)\s*=', 'Angular event binding', 'LWC uses onclick={handler}'),
    ]

    # Missing key in iteration
    ITERATION_PATTERNS = [
        (r'for:each=\{[^}]+\}\s+for:item="[^"]+"\s*>', 'for:each without key', 'Add key={item.id} to the first child element'),
    ]

    def __init__(self, file_path: str):
        """
        Initialize the validator with an LWC HTML file.

        Args:
            file_path: Path to .html file
        """
        self.file_path = file_path
        self.content = ""
        self.lines = []
        self.issues = []

        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                self.content = f.read()
                self.lines = self.content.split('\n')
        except Exception as e:
            self.issues.append({
                'severity': 'ERROR',
                'category': 'file',
                'message': f'Cannot read file: {e}',
                'line': 0
            })

    def validate(self) -> Dict:
        """
        Run all LWC template validations.

        Returns:
            Dictionary with validation results
        """
        if not self.content:
            return {
                'file': os.path.basename(self.file_path),
                'issues': self.issues,
                'issue_count': len(self.issues)
            }

        # Run all checks
        self._check_patterns(self.INLINE_EXPRESSION_PATTERNS, 'inline_expression', 'CRITICAL')
        self._check_patterns(self.METHOD_CALL_PATTERNS, 'method_call', 'CRITICAL')
        self._check_patterns(self.COMPARISON_PATTERNS, 'comparison', 'CRITICAL')
        self._check_patterns(self.LITERAL_PATTERNS, 'literal', 'CRITICAL')
        self._check_patterns(self.EVENT_HANDLER_PATTERNS, 'event_handler', 'WARNING')
        self._check_patterns(self.FRAMEWORK_SYNTAX_PATTERNS, 'framework_syntax', 'CRITICAL')
        self._check_iteration_keys()

        return {
            'file': os.path.basename(self.file_path),
            'issues': self.issues,
            'issue_count': len(self.issues)
        }

    def _check_patterns(self, patterns: List, category: str, severity: str):
        """Check for pattern matches in the template."""
        for i, line in enumerate(self.lines, 1):
            # Skip HTML comments
            if '<!--' in line and '-->' in line:
                continue

            for pattern, name, fix in patterns:
                matches = re.finditer(pattern, line)
                for match in matches:
                    # Avoid duplicate issues for same line/category
                    existing = any(
                        issue['line'] == i and
                        issue['category'] == category and
                        name in issue['message']
                        for issue in self.issues
                    )
                    if not existing:
                        self.issues.append({
                            'severity': severity,
                            'category': category,
                            'message': f'{name} not supported in LWC templates',
                            'line': i,
                            'fix': fix,
                            'source': 'template-validator'
                        })

    def _check_iteration_keys(self):
        """Check for missing key attribute in for:each iterations."""
        in_foreach = False
        foreach_line = 0
        foreach_item = ""

        for i, line in enumerate(self.lines, 1):
            # Detect for:each start
            foreach_match = re.search(r'for:each=\{([^}]+)\}\s+for:item="([^"]+)"', line)
            if foreach_match:
                in_foreach = True
                foreach_line = i
                foreach_item = foreach_match.group(2)
                continue

            if in_foreach:
                # Look for key attribute in the next few lines
                if 'key=' in line or 'key =' in line:
                    in_foreach = False
                    continue

                # If we hit a closing tag or another element without key
                if '>' in line and not line.strip().startswith('<!--'):
                    # Check if it's a template tag (doesn't need key directly)
                    if '<template' not in line:
                        self.issues.append({
                            'severity': 'WARNING',
                            'category': 'iteration',
                            'message': f'for:each iteration (line {foreach_line}) may be missing key attribute',
                            'line': i,
                            'fix': f'Add key={{{foreach_item}.id}} to identify each item uniquely',
                            'source': 'template-validator'
                        })
                    in_foreach = False


def validate_lwc_template(file_path: str) -> Dict:
    """
    Validate an LWC HTML template for anti-patterns.

    Args:
        file_path: Path to .html file

    Returns:
        Dictionary with validation results
    """
    validator = LWCTemplateValidator(file_path)
    return validator.validate()


def format_output(results: Dict) -> str:
    """Format validation results for display."""
    issues = results.get('issues', [])

    if not issues:
        return ""

    output_parts = []
    output_parts.append("")
    output_parts.append(f"🔍 LWC Template Check: {results['file']}")
    output_parts.append("─" * 50)

    # Group by severity
    critical = [i for i in issues if i['severity'] == 'CRITICAL']
    warnings = [i for i in issues if i['severity'] == 'WARNING']

    if critical:
        output_parts.append(f"🔴 Critical ({len(critical)}):")
        for issue in critical[:10]:
            output_parts.append(f"   L{issue['line']}: {issue['message']}")
            if issue.get('fix'):
                fix = issue['fix'][:120] + '...' if len(issue['fix']) > 120 else issue['fix']
                output_parts.append(f"      💡 {fix}")

    if warnings:
        output_parts.append(f"🟡 Warnings ({len(warnings)}):")
        for issue in warnings[:5]:
            output_parts.append(f"   L{issue['line']}: {issue['message']}")
            if issue.get('fix'):
                fix = issue['fix'][:120] + '...' if len(issue['fix']) > 120 else issue['fix']
                output_parts.append(f"      💡 {fix}")

    remaining = len(issues) - len(critical[:10]) - len(warnings[:5])
    if remaining > 0:
        output_parts.append(f"   ... and {remaining} more issues")

    output_parts.append("─" * 50)
    output_parts.append("📚 See: experience-lwc-generate/references/template-anti-patterns.md")

    return "\n".join(output_parts)


if __name__ == "__main__":
    import sys
    import json

    file_path = None

    # Mode 1: Hook mode - read from stdin JSON
    if not sys.stdin.isatty():
        try:
            hook_input = json.load(sys.stdin)
            tool_input = hook_input.get("tool_input", {})
            file_path = tool_input.get("file_path", "")
        except (json.JSONDecodeError, EOFError):
            pass

    # Mode 2: CLI mode - read from command-line argument
    if not file_path and len(sys.argv) >= 2:
        file_path = sys.argv[1]

    # No file path from either mode
    if not file_path:
        print("Usage: python template_validator.py <component.html>")
        print("   Or: echo '{\"tool_input\": {\"file_path\": \"...\"}}' | python template_validator.py")
        sys.exit(0)

    # Only validate .html files in lwc folders
    if not file_path.endswith('.html') or '/lwc/' not in file_path:
        sys.exit(0)

    if not os.path.exists(file_path):
        print(f"Error: File not found: {file_path}")
        sys.exit(1)

    results = validate_lwc_template(file_path)

    # Print formatted output
    output = format_output(results)
    if output:
        print(output)
    else:
        print(f"✅ No template anti-patterns detected in {results['file']}")

    sys.exit(0)  # Advisory only - don't block
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall