Skip to content

Commit

Permalink
feat: rule no-rush
Browse files Browse the repository at this point in the history
  • Loading branch information
lzear committed Nov 4, 2023
1 parent 2566d2d commit df03379
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
38 changes: 38 additions & 0 deletions docs/rules/no-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: no-rush
description: No rush
---

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

> "Sometimes, the most productive thing you can do is wait." - Unknown
# Enforce a delay (`dont/no-rush`)

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

## 📖 Rule details

Slowing down can sometimes lead to more thoughtful and considered decisions, ensuring you're not rushing through
critical processes or checks.

## ⚙️ Options

### delay

Time to wait, in seconds.

## 🔧 Config

```js
// wait 10 seconds
{ rules: {'dont/no-rush': [2, { delay: 10 }] } }
```


## 🧑‍💻 Demo

<CodeEditor :rule="ruleName" :text="initialText" :presetConfigs="presetConfigs" />
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"date-fns": "^2.30.0",
"has-emoji": "^2.0.0",
"minimatch": "^9.0.3",
"natural-compare-lite": "^1.4.0"
"natural-compare-lite": "^1.4.0",
"uuid": "^9.0.1"
},
"devDependencies": {
"@azat-io/stylelint-config": "^0.1.0",
Expand Down
51 changes: 51 additions & 0 deletions rules/no-rush.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { RuleListener } from '../utils/eslint-types/Rule.js'

import { createEslintRule } from '../utils/create-eslint-rule.js'
import { sleepSync } from '../utils/sleep-sync.js'
import { complete } from '../utils/complete.js'
import { RuleContext } from '../utils/eslint-types/Rule.js'

type MESSAGE_ID = 'no-rush'

type Options = [Partial<{ delay: number }>]

export const RULE_NAME = 'no-rush'

type Context = RuleContext<MESSAGE_ID, Options>

export default createEslintRule<Options, MESSAGE_ID>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'enforce a delay',
},
schema: [
{
type: 'object',
properties: {
delay: {
type: 'number',
default: 10,
},
},
},
],
messages: {
'no-rush': 'no-rush',
},
},
defaultOptions: [
{
delay: 10,
},
],
create: (context: Context): RuleListener => {
const options = complete(context.options.at(0), {
delay: 10,
})
return {
Program: () => sleepSync(options.delay * 1000),
}
},
})
23 changes: 23 additions & 0 deletions src/sample-code/no-rush.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { PresetConfig } from './presets.js'

export const ruleName = 'no-rush'

export const presetConfigs = [] satisfies PresetConfig[]

export const initialText = `console.log('Hello')
`

const sleep = (delayMs: number) =>
new Promise(resolve => setTimeout(resolve, delayMs))

export const fakeLint = async (code: string, { delay }: { delay?: number }) => {
delay && delay > 0 && (await sleep(delay / 1000))
return {
fix: {
fixed: false,
messages: [],
output: code,
},
verify: [],
}
}
12 changes: 12 additions & 0 deletions utils/sleep-sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'node:fs'
import * as uuid from 'uuid'

/**
* Tie up execution for at-least the given number of millis. This is not efficient.
* @param millis Min number of millis to wait
*/
export const sleepSync = (millis: number) => {
if (millis <= 0) return
const proceedAt = Date.now() + millis
while (Date.now() < proceedAt) fs.existsSync(uuid.v4())
}

0 comments on commit df03379

Please sign in to comment.