# Ported from alibaba/open-code-review (Apache-2.0):
#   internal/config/rules/rule_docs/ts_js_tsx_jsx.md
#   at commit 89ec55b14442c9f2601fb55b5f554fb6fabbe2c7.
# OCR tool names were replaced with this workflow's read-only
# exploration language. See the NOTICE.md and LICENSE files
# in rules/builtin/ for attribution and license details.
version: 1

rules:
  - id: language.javascript-typescript
    match:
      paths:
        - "**/*.{ts,js,tsx,jsx}"
    checks:
      - id: obvious-typos-or-spelling-errors
        category: conventions
        guidance: |
          - Spelling errors in variable names, function names, component names, or Props property names
          - Strings in log or error messages containing spelling errors that affect readability
      - id: dead-code
        category: simplification
        guidance: |
          - Code blocks that will never be executed (e.g., branches where the condition is always false, code after a return statement)
          - Variables that are declared but never read or referenced
          - Large blocks of commented-out code (with no apparent intent to retain)
      - id: code-quality-checks
        category: correctness
        guidance: |
          - **Duplicate Code**: Check for common logic that can be extracted
          - **Code Comments**: Complex business logic should have clear explanatory comments (avoid commenting obvious code)
          - **Hardcoding**: Business-related hardcoded strings are prohibited, especially URL paths and business numbers; simple UI text may be relaxed
          - **Variable Declarations**: Using `var` is strictly prohibited; use `let` or `const`
          - **Equality Comparisons**: Using `==` and `!=` is prohibited; use strict equality `===` and `!==`
          - **TypeScript Types**: Avoid using `any` type; if necessary, provide a comment explaining the reason
          - **Null Checks**: Perform null checks when accessing values or destructuring to avoid null pointer exceptions
          - **Ternary Expressions**: Nested ternary expressions are not allowed
      - id: react-best-practices
        category: conventions
        guidance: |
          - **Hooks Usage**: Verify compliance with Hooks rules (only call at the top level, only call in React functions)
          - **State Management**: Ensure state is placed at the appropriate level; avoid unnecessary state lifting
          - **Side Effect Handling**: Verify useEffect correctly handles dependencies and cleanup functions
          - **Performance Optimization**: Verify proper use of React.memo, useMemo, useCallback (based on performance analysis; avoid over-optimization)
          - **Render Side Effects**: Side effects in React component render methods are strictly prohibited (e.g., API calls, DOM manipulation)
          - **Inline Styles**: Avoid using inline `style` attributes, except for dynamic styles
          - **Inner Components**: Declaring new components inside a component is prohibited; use render methods instead (e.g., `renderItem`, not `<Item/>`)
      - id: async-handling-standards
        category: correctness
        guidance: |
          - **Error Handling**: Async functions must include proper error handling with user-friendly error messages
          - **Prefer async/await**: Prefer async/await over Promises; callback hell is prohibited
          - **Async in Loops**: Distinguish between independent async operations (use `Promise.all` for parallelism) and dependent async operations (use sequential execution); prefer `Promise.all` for performance
      - id: code-security-checks
        category: correctness
        guidance: |
          - **XSS Protection**: Verify that user input is properly escaped
          - **innerHTML Safety**: Using innerHTML to directly insert user input is prohibited; use textContent or apply XSS protection
          - **Code Injection Protection**: Using eval(), Function() constructor, and string argument forms of setTimeout/setInterval is strictly prohibited
          - **Dangerous Methods**: Using document.write() is prohibited as it causes page reflow and security issues
          - **Sensitive Information**: Check whether API keys or sensitive data are exposed
          - **Prototype Chain Safety**: Modifying native object prototypes (e.g., Array.prototype, Object.prototype) is prohibited
