# 代码实践规范 Gate 规则
# 来源: openJiuwen-JavaScript-coding-style-guide.md 代码规范 (3.1-3.17)

rules:
  - id: practice-no-var
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.1"
    message: "禁止使用 var 声明变量,应使用 const 或 let"
    pattern: "\\bvar\\s+"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-no-function-declaration-in-block
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.2"
    message: "函数块内须使用函数表达式声明函数, 禁止块内 function 声明"
    pattern: "(?:if|else|for|while)\\s*\\([^)]*\\)\\s*\\{[^}]*function\\s+\\w+"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-no-primitive-wrapper
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.3"
    message: "禁止封装基本类型 (new Boolean/Number/String/Object/Array)"
    pattern: "\\bnew\\s+(Boolean|Number|String|Object|Array)\\s*\\("
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-no-with
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.4"
    message: "禁止使用 with 语句"
    pattern: "\\bwith\\s*\\("
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  # 3.5 this 仅限构造函数/方法/闭包 — 不可 regex 化
  - id: practice-this-only-in-constructor-method-closure
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.5"
    message: "this 仅可在构造函数、方法、闭包中使用 (不可自动化: 需AST+控制流)"
    pattern: "(?!)"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-no-ie-conditional-comment
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.6"
    message: "禁止使用 IE 条件注释 (@cc_on / @if / @set)"
    pattern: "/\\*@cc_on|/@if\\s|/@set\\s"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-no-prototype-modification
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.7"
    message: "禁止修改内置对象的原型 (Object/Array/String.prototype.xxx =)"
    pattern: "\\b(Object|Array|String|Number|Boolean|Function)\\.prototype\\.\\w+\\s*="
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-no-object-prototype-builtin
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.8"
    message: "禁止直接调用 Object.prototype 内置属性, 应使用 Object.prototype.hasOwnProperty.call()"
    pattern: "\\b\\w+\\.(hasOwnProperty|isPrototypeOf|propertyIsEnumerable)\\s*\\("
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-no-dunder-proto
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.9"
    message: "禁止使用 __proto__, 应使用 Object.getPrototypeOf()"
    pattern: "\\.__proto__"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-no-new-function
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.10"
    message: "禁止使用函数构造器创建函数 (new Function)"
    pattern: "\\bnew\\s+Function\\s*\\("
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  # 3.11 继承用库方法 — 不可 regex 化
  - id: practice-use-lib-for-inheritance
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.11"
    message: "使用原型实现继承时尽量使用稳定的库方法 (不可自动化: 需语义理解)"
    pattern: "(?!)"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  # 3.12 方法定义在原型 — 不可 regex 化
  - id: practice-define-methods-on-prototype
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.12"
    message: "定义类时应在原型下定义方法, 构造方法内定义属性 (不可自动化: 需语义理解)"
    pattern: "(?!)"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  # 3.13 闭包避免循环引用 — 不可 regex 化
  - id: practice-avoid-closure-circular-ref
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.13"
    message: "使用闭包时避免构成循环引用导致内存泄露 (不可自动化: 需运行时检测)"
    pattern: "(?!)"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  # 3.14 警惕浮点数 — 不可 regex 化
  - id: practice-beware-floating-point
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.14"
    message: "警惕 JavaScript 浮点数精度问题, 禁止直接用 === 比较浮点数 (不可自动化: 需语义理解)"
    pattern: "(?!)"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-prefer-template-literal
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.16"
    message: "构建字符串优先使用模板字符串而非字符串链接"
    pattern: "(?:['\"]\\s*\\+\\s*|\\+\\s*['\"])"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-for-of-over-for-in-array
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.17"
    message: "数组遍历应使用 for...of, 对象遍历使用 for...in"
    pattern: "for\\s*\\(\\s*(?:let|const|var)\\s+\\w+\\s+in\\s+\\w+(?:Array|List|Items|Collection|s)\\b"
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"

  - id: practice-no-eval
    type: regex-check
    blocking: false
    severity: warning
    scope: incremental
    source: "JS-coding-style#3.10-关联"
    message: "禁止使用 eval() — 类似 new Function 的安全风险"
    pattern: "\\beval\\s*\\("
    match_mode: absent
    includes:
      - "**/*.ts"
      - "**/*.js"
    excludes:
      - "**/*.d.ts"
      - "**/*.test.*"
