forked from azat-io/eslint-plugin-perfectionist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
` |