no-drama-rule-disable.test.js 1.44 KB
import { Linter } from 'eslint'
import { describe, it, expect } from 'vitest'
import { dramaPlugin } from '../index.js'

// 用 Linter + 完整 plugin 装配(而非 RuleTester 单规则):这样 disable 指令点名的
// drama/* 规则都已注册,不会因「规则未定义」产生额外报错,能干净断言本规则是否触发。
const linter = new Linter({ configType: 'flat' })
const config = {
  plugins: { drama: dramaPlugin },
  rules: { 'drama/no-drama-rule-disable': 'error' },
}

/** @param {string} code @returns {boolean} 是否报了 no-drama-rule-disable。 */
function hitsDisable(code) {
  const messages = linter.verify(code, config, { filename: 'a.js' })
  return messages.some((m) => m.ruleId === 'drama/no-drama-rule-disable')
}

describe('no-drama-rule-disable (禁 disable 绕红线)', () => {
  it('点名 drama/* 的 disable 被拦', () => {
    expect(hitsDisable('/* eslint-disable drama/no-conn-outside-root */\nexport const a = 1')).toBe(true)
    expect(hitsDisable('export const b = 2 // eslint-disable-line drama/no-app-import')).toBe(true)
    expect(hitsDisable('/* eslint-disable no-console, drama/no-deep-import */\nexport const c = 3')).toBe(true)
  })

  it('点名非 drama 规则 / 普通注释不被拦', () => {
    expect(hitsDisable('// eslint-disable-next-line no-console\nexport const x = 1')).toBe(false)
    expect(hitsDisable('// drama/no-conn-outside-root 只是说明文字\nexport const z = 3')).toBe(false)
  })
})