Skip to content

Commit

Permalink
chore: move doc's footer strings to doc-renderer & remove unused co…
Browse files Browse the repository at this point in the history
…de (#448)

- Move the logic of processing/rendering markdown parts to the `doc-renderer.ts` tool
- Remove unused code:
     -   `footer.replace(/\$/g, "$$$$")`
     -   `computed.replace(/\$/g, "$$$$")`
  • Loading branch information
Foxeye-Rinx authored Nov 11, 2024
1 parent 37a77ef commit b16eef6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
38 changes: 37 additions & 1 deletion tools/lib/doc-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { RuleModule } from "src/types"
// eslint-disable-next-line @eslint-community/eslint-comments/disable-enable-pair -- ignore
/* eslint-disable func-style -- Arrow functions are better when returning string */
import type { RuleModule, RuleMetaData } from "src/types"

type RuleDocHeader = {
ruleId: string
Expand Down Expand Up @@ -95,3 +97,37 @@ export function buildNotesFromRule(rule: RuleModule, isNew: boolean): string[] {
}
return notes
}

export const renderVerion = (since: string): string => `## 🚀 Version
This rule was introduced in eslint-plugin-astro ${since}`

export const renderImplementation = (
ruleName: string,
): string => `## 🔍 Implementation
- [Rule source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/src/rules/${ruleName}.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/tests/src/rules/${ruleName}.ts)
- [Test fixture sources](https://github.com/ota-meshi/eslint-plugin-astro/tree/main/tests/fixtures/rules/${ruleName})
`

const buildExtensionNoteString = (plugin: string, url: string): string => `
<sup>Taken with ❤️ [from ${plugin}](${url})</sup>
`

/**
* Render extension note
*/
export function renderExtensionNote(
extensionRule: RuleMetaData["docs"]["extensionRule"],
): string {
if (!extensionRule) {
return ""
}
const isEslintExtension = typeof extensionRule === "string"
const plugin = isEslintExtension ? "ESLint core" : extensionRule.plugin
const url = isEslintExtension
? `https://eslint.org/docs/rules/${extensionRule}`
: extensionRule.url
return buildExtensionNoteString(plugin, url)
}
59 changes: 18 additions & 41 deletions tools/update-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { rules } from "../src/rules"
import type { RuleModule } from "../src/types"
import { getNewVersion } from "./lib/changesets-util"
import { formatAndSave } from "./lib/utils"
import { buildNotesFromRule, renderRuleHeader } from "./lib/doc-renderer"
import {
buildNotesFromRule,
renderExtensionNote,
renderImplementation,
renderRuleHeader,
renderVerion,
} from "./lib/doc-renderer"

//eslint-disable-next-line jsdoc/require-jsdoc -- tools
function yamlValue(val: unknown) {
Expand Down Expand Up @@ -76,38 +82,14 @@ class DocFile {
public async updateFooter() {
const { ruleName, extensionRule } = this.rule.meta.docs
const footerPattern = /## (?:(?:🔍)? ?Implementation|🚀 Version).+$/s
const footer = `${
this.since
? `## 🚀 Version
This rule was introduced in eslint-plugin-astro ${await this.since}
`
: ""
}## 🔍 Implementation
- [Rule source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/src/rules/${ruleName}.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-astro/blob/main/tests/src/rules/${ruleName}.ts)
- [Test fixture sources](https://github.com/ota-meshi/eslint-plugin-astro/tree/main/tests/fixtures/rules/${ruleName})
${
extensionRule
? typeof extensionRule === "string"
? `
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/${extensionRule})</sup>
`
: `
<sup>Taken with ❤️ [from ${extensionRule.plugin}](${extensionRule.url})</sup>
`
: ""
}`
if (footerPattern.test(this.content)) {
this.content = this.content.replace(
footerPattern,
footer.replace(/\$/g, "$$$$"),
)
} else {
this.content = `${this.content.trim()}\n\n${footer}`
}
const since = await this.since
const version = since ? renderVerion(since) : ""
const implementation = renderImplementation(ruleName)
const extensionNote = renderExtensionNote(extensionRule)
const footer = `${version}\n\n${implementation}${extensionNote}`
this.content = footerPattern.test(this.content)
? this.content.replace(footerPattern, footer)
: `${this.content.trim()}\n\n${footer}`

return this
}
Expand Down Expand Up @@ -160,14 +142,9 @@ ${

const fileIntroPattern = /^---\n(?:.*\n)+?---\n*/gu

if (fileIntroPattern.test(this.content)) {
this.content = this.content.replace(
fileIntroPattern,
computed.replace(/\$/g, "$$$$"),
)
} else {
this.content = `${computed}${this.content.trim()}\n`
}
this.content = fileIntroPattern.test(this.content)
? this.content.replace(fileIntroPattern, computed)
: `${computed}${this.content.trim()}\n`

return this
}
Expand Down

0 comments on commit b16eef6

Please sign in to comment.