Skip to content

Commit

Permalink
feat: rule no-woof
Browse files Browse the repository at this point in the history
  • Loading branch information
lzear committed Nov 5, 2023
1 parent b6897d7 commit 48b89ee
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
46 changes: 46 additions & 0 deletions docs/rules/no-woof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: no-woof
description: No woof!
---

<script setup lang="ts">
import CodeEditor from '../../.vitepress/theme/components/code-editor.vue';
import {ruleName, presetConfigs, initialText} from '../../src/sample-code/no-woof.js';
</script>

> "Woof" — Randall T.
# Disallow woof! (`dont/no-woof`)

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

## 📖 Rule details

No woof!

## 💡 Examples

```js
// ❌ Incorrect
// woof Woof! WOOF!
const woof = 'woof'

// ✅ Correct
const foo = 'bar'
```

## 🔧 Config

```js
{ rules: { 'dont/no-woof': 2 } }
```

## 🔗 See also

- [private joke](https://en.wikipedia.org/wiki/Private_joke)

## 🧑‍💻 Demo

<CodeEditor :rule="ruleName" :text="initialText" :presetConfigs="presetConfigs" />
52 changes: 52 additions & 0 deletions rules/no-woof.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { RuleContext, RuleListener } from '../utils/eslint-types/Rule.js'

import { createEslintRule } from '../utils/create-eslint-rule.js'

type MESSAGE_ID = 'match'

type Options = [unknown]

export const RULE_NAME = 'no-woof'
type Context = RuleContext<MESSAGE_ID, Options>

export default createEslintRule<Options, MESSAGE_ID>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'disallow woof!',
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {},
},
],
messages: {
match: 'No woof! Found "{{ match }}".',
},
},
defaultOptions: [{}],
create: (context: Context): RuleListener => {
const { sourceCode } = context
const regex = /w[0o]{2}f/gi
return {
Program: () => {
for (const token of sourceCode.tokensAndComments) {
const text = sourceCode.getText(token)
if (regex.test(text)) {
const match = text.match(regex)
const replacement = text.replaceAll(regex, '')
context.report({
node: token,
messageId: 'match',
data: { match: match?.[0] },
fix: fixer => fixer.replaceText(token, replacement),
})
}
}
},
}
},
})
13 changes: 13 additions & 0 deletions src/sample-code/no-woof.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { PresetConfig } from './presets.js'

export const ruleName = 'no-woof'

export const presetConfigs = [] satisfies PresetConfig[]

export const initialText = `// woof
const WOOF = 'woof';
function woof() {
return 'w00f';
}
`

0 comments on commit 48b89ee

Please sign in to comment.