Skip to content

Commit

Permalink
Refactor to enable some xo rules
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 16, 2023
1 parent e1d929e commit 69d87e3
Show file tree
Hide file tree
Showing 29 changed files with 321 additions and 387 deletions.
4 changes: 1 addition & 3 deletions docs/_component/layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ export function Layout(props) {
? 'https://github.com/' + d.github
: d.twitter
? 'https://twitter.com/' + d.twitter
: d.url
? d.url
: undefined
: d.url || undefined
return (
<span key={d.name}>
{i ? ', ' : ''}
Expand Down
24 changes: 9 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
"atLeast": 100,
"detail": true,
"ignoreCatch": true,
"#": "needed `any`s",
"#": "needed `any`s; to do: can they be typed, liked `hast-util-to-jsx-runtime`?",
"ignoreFiles": [
"packages/mdx/lib/util/resolve-evaluate-options.d.ts",
"packages/mdx/lib/util/resolve-evaluate-options.js"
Expand All @@ -181,19 +181,6 @@
"envs": [
"shared-node-browser"
],
"prettier": true,
"rules": {
"n/file-extension-in-import": "off",
"react/jsx-no-bind": "off",
"react/prop-types": "off",
"unicorn/no-await-expression-member": "off",
"unicorn/prefer-at": "off",
"unicorn/prefer-logical-operator-over-ternary": "off",
"unicorn/prefer-node-protocol": "off",
"unicorn/prefer-string-replace-all": "off",
"unicorn/prefer-top-level-await": "off",
"complexity": "off"
},
"overrides": [
{
"files": [
Expand All @@ -214,6 +201,13 @@
"react/react-in-jsx-scope": "off"
}
}
]
],
"prettier": true,
"rules": {
"n/file-extension-in-import": "off",
"react/jsx-no-bind": "off",
"react/prop-types": "off",
"complexity": "off"
}
}
}
118 changes: 67 additions & 51 deletions packages/esbuild/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export function esbuild(options) {
if (cachedContents) {
contents = cachedContents
} else {
contents = await (await fetch(href)).text()
const response = await fetch(href)
contents = await response.text()
cache.set(href, contents)
}

Expand Down Expand Up @@ -136,6 +137,8 @@ export function esbuild(options) {
: await fs.readFile(data.path)
)

/** @type {State} */
const state = {doc, name, path: data.path}
let file = new VFile({value: doc, path: data.path})
/** @type {VFileValue | undefined} */
let value
Expand All @@ -157,56 +160,8 @@ export function esbuild(options) {
}

for (const message of messages) {
const place = 'place' in message ? message.place : undefined
const start = place
? 'start' in place
? place.start
: place
: undefined
const end = place && 'end' in place ? place.end : undefined
let length = 0
let lineStart = 0
let line = 0
let column = 0

if (start && start.offset !== undefined) {
line = start.line
column = start.column - 1
lineStart = start.offset - column
length = 1

if (end && end.offset !== undefined) {
length = end.offset - start.offset
}
}

eol.lastIndex = lineStart

const match = eol.exec(doc)
const lineEnd = match ? match.index : doc.length

;(!('fatal' in message) || message.fatal ? errors : warnings).push({
pluginName: name,
id: '',
text: String(
'reason' in message
? message.reason
: /* Extra fallback to make sure weird values are definitely strings */
/* c8 ignore next */
message.stack || message
),
notes: [],
location: {
namespace: 'file',
suggestion: '',
file: data.path,
line,
column,
length: Math.min(length, lineEnd),
lineText: doc.slice(lineStart, lineEnd)
},
detail: message
})
const list = !('fatal' in message) || message.fatal ? errors : warnings
list.push(vfileMessageToEsbuild(state, message))
}

// Safety check: the file has a path, so there has to be a `dirname`.
Expand All @@ -223,3 +178,64 @@ export function esbuild(options) {
}
}
}

/**
* @typedef State
* @property {string} doc
* @property {string} name
* @property {string} path
*/

/**
* @param {State} state
* @param {Error | VFileMessage} message
* @returns {Message}
*/
function vfileMessageToEsbuild(state, message) {
const place = 'place' in message ? message.place : undefined
const start = place ? ('start' in place ? place.start : place) : undefined
const end = place && 'end' in place ? place.end : undefined
let length = 0
let lineStart = 0
let line = 0
let column = 0

if (start && start.offset !== undefined) {
line = start.line
column = start.column - 1
lineStart = start.offset - column
length = 1

if (end && end.offset !== undefined) {
length = end.offset - start.offset
}
}

eol.lastIndex = lineStart

const match = eol.exec(state.doc)
const lineEnd = match ? match.index : state.doc.length

return {
pluginName: state.name,
id: '',
text: String(
'reason' in message
? message.reason
: /* Extra fallback to make sure weird values are definitely strings */
/* c8 ignore next */
message.stack || message
),
notes: [],
location: {
namespace: 'file',
suggestion: '',
file: state.path,
line,
column,
length: Math.min(length, lineEnd),
lineText: state.doc.slice(lineStart, lineEnd)
},
detail: message
}
}
5 changes: 1 addition & 4 deletions packages/esbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@
"xo": {
"prettier": true,
"rules": {
"complexity": "off",
"n/file-extension-in-import": "off",
"unicorn/no-await-expression-member": "off",
"unicorn/prefer-node-protocol": "off"
"n/file-extension-in-import": "off"
}
}
}
72 changes: 37 additions & 35 deletions packages/esbuild/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
* @typedef {import('esbuild').BuildFailure} BuildFailure
* @typedef {import('hast').Root} Root
* @typedef {import('vfile').VFile} VFile
* @typedef {import('mdx/types.js').MDXModule} MDXModule
* @typedef {import('mdx/types.js').MDXContent} MDXContent
*
* @typedef {import('remark-mdx')} DoNotTouchIncludeMathInTree
* Augment node types:
* @typedef {import('remark-mdx')}
*/

import assert from 'node:assert/strict'
import {promises as fs} from 'fs'
import {promises as fs} from 'node:fs'
import {test} from 'node:test'
import {fileURLToPath} from 'url'
import {fileURLToPath} from 'node:url'
import esbuild from 'esbuild'
import React from 'react'
import {renderToStaticMarkup} from 'react-dom/server'
Expand All @@ -32,10 +34,10 @@ test('@mdx-js/esbuild', async (t) => {
plugins: [esbuildMdx()]
})

/** @type {MDXContent} */
const Content =
/* @ts-expect-error file is dynamically generated */
(await import('./esbuild.js')).default // type-coverage:ignore-line
/** @type {MDXModule} */
// @ts-expect-error: dynamically generated file.
const mod = await import('./esbuild.js')
const Content = mod.default

assert.equal(
renderToStaticMarkup(React.createElement(Content)),
Expand Down Expand Up @@ -71,10 +73,10 @@ test('@mdx-js/esbuild', async (t) => {
format: 'esm',
plugins: [esbuildMdx()]
})
/** @type {MDXContent} */
const Content =
/* @ts-expect-error file is dynamically generated */
(await import('./esbuild-resolve.js')).default // type-coverage:ignore-line
/** @type {MDXModule} */
// @ts-expect-error: dynamically generated file.
const mod = await import('./esbuild-resolve.js')
const Content = mod.default

assert.equal(renderToStaticMarkup(React.createElement(Content)), '0.1')
})
Expand All @@ -98,10 +100,10 @@ test('@mdx-js/esbuild', async (t) => {
plugins: [esbuildMdx()]
})

/** @type {MDXContent} */
const Content =
/* @ts-expect-error file is dynamically generated */
(await import('./esbuild-md.js')).default // type-coverage:ignore-line
/** @type {MDXModule} */
// @ts-expect-error: dynamically generated file.
const mod = await import('./esbuild-md.js')
const Content = mod.default

assert.equal(
renderToStaticMarkup(React.createElement(Content)),
Expand All @@ -124,10 +126,10 @@ test('@mdx-js/esbuild', async (t) => {
plugins: [esbuildMdx({mdExtensions: [], mdxExtensions: ['.md']})]
})

/** @type {MDXContent} */
const Content =
/* @ts-expect-error file is dynamically generated */
(await import('./esbuild-md-as-mdx.js')).default // type-coverage:ignore-line
/** @type {MDXModule} */
// @ts-expect-error: dynamically generated file.
const mod = await import('./esbuild-md-as-mdx.js')
const Content = mod.default

assert.equal(renderToStaticMarkup(React.createElement(Content)), '<p>a</p>')
})
Expand Down Expand Up @@ -464,10 +466,10 @@ test('@mdx-js/esbuild', async (t) => {
bundle: true
})

/** @type {MDXContent} */
const Content =
/** @ts-expect-error file is dynamically generated */
(await import('./esbuild-compile-from-memory.js')).default // type-coverage:ignore-line
/** @type {MDXModule} */
// @ts-expect-error: dynamically generated file.
const mod = await import('./esbuild-compile-from-memory.js')
const Content = mod.default

assert.equal(
renderToStaticMarkup(React.createElement(Content)),
Expand All @@ -493,10 +495,10 @@ test('@mdx-js/esbuild', async (t) => {
bundle: true
})

/** @type {MDXContent} */
const Content =
/** @ts-expect-error file is dynamically generated */
(await import('./esbuild-compile-from-memory-empty.js')).default // type-coverage:ignore-line
/** @type {MDXModule} */
// @ts-expect-error: dynamically generated file.
const mod = await import('./esbuild-compile-from-memory-empty.js')
const Content = mod.default

assert.equal(renderToStaticMarkup(React.createElement(Content)), '')
}
Expand Down Expand Up @@ -528,10 +530,10 @@ test('@mdx-js/esbuild', async (t) => {
plugins: [esbuildMdx({allowDangerousRemoteMdx: true})]
})

/** @type {MDXContent} */
const Content =
/* @ts-expect-error file is dynamically generated */
(await import('./esbuild-with-remote-md.js')).default // type-coverage:ignore-line
/** @type {MDXModule} */
// @ts-expect-error: dynamically generated file.
const mod = await import('./esbuild-with-remote-md.js')
const Content = mod.default

assert.equal(
renderToStaticMarkup(React.createElement(Content)),
Expand Down Expand Up @@ -568,10 +570,10 @@ test('@mdx-js/esbuild', async (t) => {
plugins: [esbuildMdx({allowDangerousRemoteMdx: true})]
})

/** @type {MDXContent} */
const Content =
/* @ts-expect-error file is dynamically generated */
(await import('./esbuild-with-remote-mdx.js')).default // type-coverage:ignore-line
/** @type {MDXModule} */
// @ts-expect-error: dynamically generated file.
const mod = await import('./esbuild-with-remote-mdx.js')
const Content = mod.default

assert.equal(
renderToStaticMarkup(React.createElement(Content)),
Expand Down
4 changes: 1 addition & 3 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@
"xo": {
"prettier": true,
"rules": {
"n/file-extension-in-import": "off",
"unicorn/prefer-node-protocol": "off",
"unicorn/prefer-string-replace-all": "off"
"n/file-extension-in-import": "off"
}
}
}
6 changes: 3 additions & 3 deletions packages/loader/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*/

import assert from 'node:assert/strict'
import {promises as fs} from 'fs'
import {promises as fs} from 'node:fs'
import {test} from 'node:test'
import {promisify} from 'util'
import {fileURLToPath} from 'url'
import {promisify} from 'node:util'
import {fileURLToPath} from 'node:url'
import webpack from 'webpack'
import React from 'react'
import {renderToStaticMarkup} from 'react-dom/server'
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/lib/condition.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import process from 'process'
import process from 'node:process'

export const development = process.env.NODE_ENV === 'development'
4 changes: 1 addition & 3 deletions packages/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@
"complexity": "off",
"max-depth": "off",
"n/file-extension-in-import": "off",
"unicorn/no-await-expression-member": "off",
"unicorn/prefer-at": "off",
"unicorn/prefer-code-point": "off",
"unicorn/prefer-node-protocol": "off"
"unicorn/prefer-code-point": "off"
}
}
}
Loading

0 comments on commit 69d87e3

Please sign in to comment.