Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 24, 2024
1 parent 68d24e1 commit 1c8905d
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 283 deletions.
4 changes: 1 addition & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/ban-types, @typescript-eslint/consistent-type-definitions */

import type {Options as DeadOrAliveOptions} from 'dead-or-alive'

export {default} from './lib/index.js'
Expand All @@ -14,7 +12,7 @@ export interface Options {
* `deadOrAliveOptions.findUrls` is always off as further URLs are not used
* by `remark-lint-no-dead-urls`.
*/
deadOrAliveOptions?: DeadOrAliveOptions | null | undefined
deadOrAliveOptions?: Readonly<DeadOrAliveOptions> | null | undefined
/**
* Check relative values relative to this URL
* (optional, example: `'https://example.com/from'`).
Expand Down
5 changes: 1 addition & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
/**
* @typedef {import('./lib/index.js').Options} Options
*/

// Note: types exposed from `index.d.ts`.
export {default} from './lib/index.js'
30 changes: 16 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
/**
* @typedef {import('mdast').Nodes} Nodes
* @typedef {import('mdast').Resource} Resource
* @typedef {import('mdast').Root} Root
*
* @typedef {import('remark-lint-no-dead-urls').Options} Options
*
* @typedef {import('vfile').VFile} VFile
* @import {Nodes, Resource, Root} from 'mdast'
* @import {Options} from 'remark-lint-no-dead-urls'
* @import {VFile} from 'vfile'
*/

/**
* @typedef {Extract<Nodes, Resource>} Resources
* Resource nodes.
*/

import {ok as assert} from 'devlop'
import {deadOrAlive} from 'dead-or-alive'
import {ok as assert} from 'devlop'
import isOnline from 'is-online'
import {lintRule} from 'unified-lint-rule'
import {visit} from 'unist-util-visit'

/** @type {Readonly<Options>} */
const emptyOptions = {}
const defaultSkipUrlPatterns = [/^(?!https?)/i]
const remarkLintNoDeadUrls = lintRule(
{
origin: 'remark-lint:no-dead-urls',
url: 'https://github.com/remarkjs/remark-lint-no-dead-urls'
},
rule
)
const defaultSkipUrlPatterns = [/^(?!https?)/i]

export default remarkLintNoDeadUrls

Expand Down Expand Up @@ -59,11 +57,11 @@ async function rule(tree, file, options) {
/** @type {Map<string, Array<Resources>>} */
const nodesByUrl = new Map()
const online = await isOnline()
const settings = options || {}
const settings = options || emptyOptions
const skipUrlPatterns = settings.skipUrlPatterns
? settings.skipUrlPatterns.map((d) =>
typeof d === 'string' ? new RegExp(d) : d
)
? settings.skipUrlPatterns.map(function (d) {
return typeof d === 'string' ? new RegExp(d) : d
})
: [...defaultSkipUrlPatterns]

if (settings.skipLocalhost) {
Expand Down Expand Up @@ -125,7 +123,11 @@ async function rule(tree, file, options) {

const url = new URL(value, from).href

if (skipUrlPatterns.some((skipPattern) => skipPattern.test(url))) {
if (
skipUrlPatterns.some(function (skipPattern) {
return skipPattern.test(url)
})
) {
return
}

Expand Down
35 changes: 30 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "remark-lint-no-dead-urls",
"version": "1.1.0",
"description": "Ensure that URLs in your Markdown are alive",
"description": "remark-lint rule to warn when URLs are dead",
"license": "MIT",
"keywords": [
"lint",
"markdown",
"remark",
"remark-lint",
"remark-lint-rule",
"remark-lint",
"remark",
"rule"
],
"repository": "remarkjs/remark-lint-no-dead-urls",
Expand Down Expand Up @@ -58,9 +58,9 @@
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . --frail --output --quiet && prettier . --log-level warn --write && xo --fix",
"prepack": "npm run build && npm run format",
"test": "npm run build && npm run format && npm run test-coverage",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --100 --reporter lcov npm run test-api"
"test-coverage": "c8 --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"bracketSpacing": false,
Expand All @@ -82,6 +82,31 @@
"strict": true
},
"xo": {
"overrides": [
{
"files": [
"**/*.d.ts"
],
"rules": {
"@typescript-eslint/array-type": [
"error",
{
"default": "generic"
}
],
"@typescript-eslint/ban-types": [
"error",
{
"extendDefaults": true
}
],
"@typescript-eslint/consistent-type-definitions": [
"error",
"interface"
]
}
}
],
"prettier": true
}
}
Loading

0 comments on commit 1c8905d

Please sign in to comment.