From 9cf3168e66fdd53a51f5a1fb2c62b5db607b6291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Mon, 23 Oct 2023 23:11:41 +0200 Subject: [PATCH] chore: better Prettier config for easier reviews --- .github/ISSUE_TEMPLATE/BUG.md | 8 ++--- .github/ISSUE_TEMPLATE/DOCUMENTATION.md | 8 ++--- .github/ISSUE_TEMPLATE/FEATURE_REQUEST.md | 8 ++--- .github/ISSUE_TEMPLATE/IMPROVEMENT.md | 8 ++--- .github/ISSUE_TEMPLATE/QUESTION.md | 8 ++--- .github/workflows/lint.yml | 26 +++++++------- .github/workflows/release.yml | 34 +++++++++--------- .github/workflows/test.yml | 22 ++++++------ .prettierrc.json | 5 +-- src/index.js | 38 ++++++++++---------- src/utils.js | 22 ++++++------ test/basic.test.js | 44 +++++++++++------------ test/utils.test.js | 34 +++++++++--------- 13 files changed, 131 insertions(+), 134 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/BUG.md b/.github/ISSUE_TEMPLATE/BUG.md index e1fc7e7..d47b126 100644 --- a/.github/ISSUE_TEMPLATE/BUG.md +++ b/.github/ISSUE_TEMPLATE/BUG.md @@ -1,8 +1,8 @@ --- -name: '🐛 Bug Report' -about: 'Report an unexpected problem or unintended behavior.' -title: '[Bug]' -labels: 'bug' +name: "🐛 Bug Report" +about: "Report an unexpected problem or unintended behavior." +title: "[Bug]" +labels: "bug" --- diff --git a/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md index 3307418..dc9a49c 100644 --- a/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md +++ b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md @@ -1,8 +1,8 @@ --- -name: '✨ Feature Request' -about: 'Suggest a new feature idea.' -title: '[Feature]' -labels: 'feature request' +name: "✨ Feature Request" +about: "Suggest a new feature idea." +title: "[Feature]" +labels: "feature request" --- diff --git a/.github/ISSUE_TEMPLATE/IMPROVEMENT.md b/.github/ISSUE_TEMPLATE/IMPROVEMENT.md index b5bdbb7..e50e289 100644 --- a/.github/ISSUE_TEMPLATE/IMPROVEMENT.md +++ b/.github/ISSUE_TEMPLATE/IMPROVEMENT.md @@ -1,8 +1,8 @@ --- -name: '🔧 Improvement' -about: 'Improve structure/format/performance/refactor/tests of the code.' -title: '[Improvement]' -labels: 'improvement' +name: "🔧 Improvement" +about: "Improve structure/format/performance/refactor/tests of the code." +title: "[Improvement]" +labels: "improvement" --- diff --git a/.github/ISSUE_TEMPLATE/QUESTION.md b/.github/ISSUE_TEMPLATE/QUESTION.md index c18eaa3..df6acdc 100644 --- a/.github/ISSUE_TEMPLATE/QUESTION.md +++ b/.github/ISSUE_TEMPLATE/QUESTION.md @@ -1,8 +1,8 @@ --- -name: '🙋 Question' -about: 'Further information is requested.' -title: '[Question]' -labels: 'question' +name: "🙋 Question" +about: "Further information is requested." +title: "[Question]" +labels: "question" --- ### Question diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index e894a4c..18c9436 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,4 +1,4 @@ -name: 'Lint' +name: "Lint" on: push: @@ -8,21 +8,21 @@ on: jobs: lint: - runs-on: 'ubuntu-latest' + runs-on: "ubuntu-latest" steps: - - uses: 'actions/checkout@v3.5.2' + - uses: "actions/checkout@v3.5.2" - - name: 'Setup Node.js' - uses: 'actions/setup-node@v3.6.0' + - name: "Setup Node.js" + uses: "actions/setup-node@v3.6.0" with: - node-version: 'lts/*' - cache: 'npm' + node-version: "lts/*" + cache: "npm" - - name: 'Install dependencies' - run: 'npm clean-install' + - name: "Install dependencies" + run: "npm clean-install" - run: 'npm run lint:commit -- --to "${{ github.sha }}"' - - run: 'npm run lint:editorconfig' - - run: 'npm run lint:markdown' - - run: 'npm run lint:eslint' - - run: 'npm run lint:prettier' + - run: "npm run lint:editorconfig" + - run: "npm run lint:markdown" + - run: "npm run lint:eslint" + - run: "npm run lint:prettier" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 615ba7b..d129b43 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: 'Release' +name: "Release" on: push: @@ -6,32 +6,32 @@ on: jobs: release: - runs-on: 'ubuntu-latest' + runs-on: "ubuntu-latest" permissions: - contents: 'write' - issues: 'write' - pull-requests: 'write' - id-token: 'write' + contents: "write" + issues: "write" + pull-requests: "write" + id-token: "write" steps: - - uses: 'actions/checkout@v3.5.2' + - uses: "actions/checkout@v3.5.2" with: fetch-depth: 0 persist-credentials: false - - name: 'Setup Node.js' - uses: 'actions/setup-node@v3.6.0' + - name: "Setup Node.js" + uses: "actions/setup-node@v3.6.0" with: - node-version: 'lts/*' - cache: 'npm' + node-version: "lts/*" + cache: "npm" - - name: 'Install dependencies' - run: 'npm clean-install' + - name: "Install dependencies" + run: "npm clean-install" - - name: 'Verify the integrity of provenance attestations and registry signatures for installed dependencies' - run: 'npm audit signatures' + - name: "Verify the integrity of provenance attestations and registry signatures for installed dependencies" + run: "npm audit signatures" - - name: 'Release' - run: 'npm run release' + - name: "Release" + run: "npm run release" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d4dde34..2b9f3d6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: 'Test' +name: "Test" on: push: @@ -8,18 +8,18 @@ on: jobs: test: - runs-on: 'ubuntu-latest' + runs-on: "ubuntu-latest" steps: - - uses: 'actions/checkout@v3.5.2' + - uses: "actions/checkout@v3.5.2" - - name: 'Setup Node.js' - uses: 'actions/setup-node@v3.6.0' + - name: "Setup Node.js" + uses: "actions/setup-node@v3.6.0" with: - node-version: 'lts/*' - cache: 'npm' + node-version: "lts/*" + cache: "npm" - - name: 'Install dependencies' - run: 'npm clean-install' + - name: "Install dependencies" + run: "npm clean-install" - - name: 'Test' - run: 'npm run test' + - name: "Test" + run: "npm run test" diff --git a/.prettierrc.json b/.prettierrc.json index 83ef1ce..cce9d3c 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -1,6 +1,3 @@ { - "singleQuote": true, - "jsxSingleQuote": true, - "semi": false, - "trailingComma": "none" + "semi": false } diff --git a/src/index.js b/src/index.js index bb963ad..9e8012d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,39 +1,39 @@ -'use strict' +"use strict" -const { pathToFileURL } = require('node:url') -const fs = require('node:fs') +const { pathToFileURL } = require("node:url") +const fs = require("node:fs") const { filterTokens, addError, convertHeadingToHTMLFragment, - getMarkdownHeadings -} = require('./utils.js') + getMarkdownHeadings, +} = require("./utils.js") const customRule = { - names: ['relative-links'], - description: 'Relative links should be valid', - tags: ['links'], + names: ["relative-links"], + description: "Relative links should be valid", + tags: ["links"], function: (params, onError) => { - filterTokens(params, 'inline', (token) => { + filterTokens(params, "inline", (token) => { for (const child of token.children) { const { lineNumber, type, attrs } = child /** @type {string | null} */ let hrefSrc = null - if (type === 'link_open') { + if (type === "link_open") { for (const attr of attrs) { - if (attr[0] === 'href') { + if (attr[0] === "href") { hrefSrc = attr[1] break } } } - if (type === 'image') { + if (type === "image") { for (const attr of attrs) { - if (attr[0] === 'src') { + if (attr[0] === "src") { hrefSrc = attr[1] break } @@ -43,7 +43,7 @@ const customRule = { if (hrefSrc != null) { const url = new URL(hrefSrc, pathToFileURL(params.name)) const isRelative = - url.protocol === 'file:' && !hrefSrc.startsWith('/') + url.protocol === "file:" && !hrefSrc.startsWith("/") if (isRelative) { const detail = `Link "${hrefSrc}"` @@ -51,13 +51,13 @@ const customRule = { addError( onError, lineNumber, - `${detail} should exist in the file system` + `${detail} should exist in the file system`, ) continue } - if (type === 'link_open' && url.hash !== '') { - const fileContent = fs.readFileSync(url, { encoding: 'utf8' }) + if (type === "link_open" && url.hash !== "") { + const fileContent = fs.readFileSync(url, { encoding: "utf8" }) const headings = getMarkdownHeadings(fileContent) /** @type {Map} */ @@ -77,7 +77,7 @@ const customRule = { addError( onError, lineNumber, - `${detail} should have a valid fragment` + `${detail} should have a valid fragment`, ) } } @@ -85,7 +85,7 @@ const customRule = { } } }) - } + }, } module.exports = customRule diff --git a/src/utils.js b/src/utils.js index b0c2586..de73cd1 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,4 @@ -const MarkdownIt = require('markdown-it') +const MarkdownIt = require("markdown-it") /** * Calls the provided function for each matching token. @@ -33,7 +33,7 @@ const addError = (onError, lineNumber, detail, context, range, fixInfo) => { detail, context, range, - fixInfo + fixInfo, }) } @@ -47,7 +47,7 @@ const addError = (onError, lineNumber, detail, context, range, fixInfo) => { */ const convertHeadingToHTMLFragment = (inlineText) => { return ( - '#' + + "#" + encodeURIComponent( inlineText .toLowerCase() @@ -56,15 +56,15 @@ const convertHeadingToHTMLFragment = (inlineText) => { // https://ruby-doc.org/core-3.0.2/Regexp.html .replace( /[^\p{Letter}\p{Mark}\p{Number}\p{Connector_Punctuation}\- ]/gu, - '' + "", ) - .replace(/ /gu, '-') + .replace(/ /gu, "-"), ) ) } -const headingTags = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']) -const ignoredTokens = new Set(['heading_open', 'heading_close']) +const headingTags = new Set(["h1", "h2", "h3", "h4", "h5", "h6"]) +const ignoredTokens = new Set(["heading_open", "heading_close"]) /** * Gets the headings from a Markdown string. @@ -83,9 +83,9 @@ const getMarkdownHeadings = (content) => { for (const token of tokens) { if (headingTags.has(token.tag)) { - if (token.type === 'heading_open') { + if (token.type === "heading_open") { headingToken = token.markup - } else if (token.type === 'heading_close') { + } else if (token.type === "heading_close") { headingToken = null } } @@ -103,7 +103,7 @@ const getMarkdownHeadings = (content) => { .map((token) => { return token.content }) - .join('')}` + .join("")}`, ) } @@ -114,5 +114,5 @@ module.exports = { filterTokens, addError, convertHeadingToHTMLFragment, - getMarkdownHeadings + getMarkdownHeadings, } diff --git a/test/basic.test.js b/test/basic.test.js index 5d7965b..aac73d4 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -1,46 +1,46 @@ -const test = require('node:test') -const assert = require('node:assert/strict') +const test = require("node:test") +const assert = require("node:assert/strict") -const { markdownlint } = require('markdownlint').promises +const { markdownlint } = require("markdownlint").promises -const relativeLinks = require('../src/index.js') +const relativeLinks = require("../src/index.js") -test('ensure the rule validate correctly', async () => { +test("ensure the rule validate correctly", async () => { const lintResults = await markdownlint({ - files: ['test/fixtures/Valid.md', 'test/fixtures/Invalid.md'], + files: ["test/fixtures/Valid.md", "test/fixtures/Invalid.md"], config: { default: false, - 'relative-links': true + "relative-links": true, }, - customRules: [relativeLinks] + customRules: [relativeLinks], }) - assert.equal(lintResults['test/fixtures/Valid.md'].length, 0) - assert.equal(lintResults['test/fixtures/Invalid.md'].length, 3) + assert.equal(lintResults["test/fixtures/Valid.md"].length, 0) + assert.equal(lintResults["test/fixtures/Invalid.md"].length, 3) assert.equal( - lintResults['test/fixtures/Invalid.md'][0]?.ruleDescription, - 'Relative links should be valid' + lintResults["test/fixtures/Invalid.md"][0]?.ruleDescription, + "Relative links should be valid", ) assert.equal( - lintResults['test/fixtures/Invalid.md'][0]?.errorDetail, - 'Link "./basic.test.js" should exist in the file system' + lintResults["test/fixtures/Invalid.md"][0]?.errorDetail, + 'Link "./basic.test.js" should exist in the file system', ) assert.equal( - lintResults['test/fixtures/Invalid.md'][1]?.ruleDescription, - 'Relative links should be valid' + lintResults["test/fixtures/Invalid.md"][1]?.ruleDescription, + "Relative links should be valid", ) assert.equal( - lintResults['test/fixtures/Invalid.md'][1]?.errorDetail, - 'Link "../image.png" should exist in the file system' + lintResults["test/fixtures/Invalid.md"][1]?.errorDetail, + 'Link "../image.png" should exist in the file system', ) assert.equal( - lintResults['test/fixtures/Invalid.md'][2]?.ruleDescription, - 'Relative links should be valid' + lintResults["test/fixtures/Invalid.md"][2]?.ruleDescription, + "Relative links should be valid", ) assert.equal( - lintResults['test/fixtures/Invalid.md'][2]?.errorDetail, - 'Link "./Valid.md#not-existing-heading" should have a valid fragment' + lintResults["test/fixtures/Invalid.md"][2]?.errorDetail, + 'Link "./Valid.md#not-existing-heading" should have a valid fragment', ) }) diff --git a/test/utils.test.js b/test/utils.test.js index 92f781f..71a4521 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -1,37 +1,37 @@ -const test = require('node:test') -const assert = require('node:assert/strict') +const test = require("node:test") +const assert = require("node:assert/strict") const { convertHeadingToHTMLFragment, - getMarkdownHeadings -} = require('../src/utils.js') + getMarkdownHeadings, +} = require("../src/utils.js") -test('utils', async (t) => { - await t.test('convertHeadingToHTMLFragment', async () => { +test("utils", async (t) => { + await t.test("convertHeadingToHTMLFragment", async () => { assert.strictEqual( - convertHeadingToHTMLFragment('Valid Fragments'), - '#valid-fragments' + convertHeadingToHTMLFragment("Valid Fragments"), + "#valid-fragments", ) assert.strictEqual( - convertHeadingToHTMLFragment('Valid Heading With Underscores _'), - '#valid-heading-with-underscores-_' + convertHeadingToHTMLFragment("Valid Heading With Underscores _"), + "#valid-heading-with-underscores-_", ) assert.strictEqual( convertHeadingToHTMLFragment( - `Valid Heading With Quotes ' And Double Quotes "` + `Valid Heading With Quotes ' And Double Quotes "`, ), - '#valid-heading-with-quotes--and-double-quotes-' + "#valid-heading-with-quotes--and-double-quotes-", ) assert.strictEqual( - convertHeadingToHTMLFragment('🚀 Valid Heading With Emoji'), - '#-valid-heading-with-emoji' + convertHeadingToHTMLFragment("🚀 Valid Heading With Emoji"), + "#-valid-heading-with-emoji", ) }) - await t.test('getMarkdownHeadings', async () => { + await t.test("getMarkdownHeadings", async () => { assert.deepStrictEqual( - getMarkdownHeadings('# Hello\n\n## World\n\n## Hello, world!\n'), - ['Hello', 'World', 'Hello, world!'] + getMarkdownHeadings("# Hello\n\n## World\n\n## Hello, world!\n"), + ["Hello", "World", "Hello, world!"], ) }) })