Skip to content

Commit

Permalink
Add strict to tsconfig.json
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 12, 2021
1 parent a33caec commit a54de55
Show file tree
Hide file tree
Showing 29 changed files with 508 additions and 536 deletions.
48 changes: 29 additions & 19 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,39 @@ export function createProcessor(options = {}) {
...rest
} = options

// @ts-ignore runtime.
// @ts-expect-error runtime.
if (format === 'detect') {
throw new Error(
"Incorrect `format: 'detect'`: `createProcessor` can support either `md` or `mdx`; it does not support detecting the format"
)
}

return (
unified()
.use(remarkParse)
.use(format === 'md' ? undefined : remarkMdx)
.use(remarkMarkAndUnravel)
.use(remarkPlugins)
.use(remarkRehype, {allowDangerousHtml: true, passThrough: nodeTypes})
.use(rehypePlugins)
.use(format === 'md' ? rehypeRemoveRaw : undefined)
// @ts-ignore this switches from rehype to recma
.use(rehypeRecma)
.use(recmaDocument, {...rest, outputFormat})
.use(recmaJsxRewrite, {providerImportSource, outputFormat})
.use(jsx ? undefined : recmaJsxBuild, {outputFormat})
// @ts-ignore recma compiler is seen as a transformer
.use(recmaStringify, {SourceMapGenerator})
.use(recmaPlugins)
)
const pipeline = unified().use(remarkParse)

if (format !== 'md') {
pipeline.use(remarkMdx)
}

pipeline
.use(remarkMarkAndUnravel)
.use(remarkPlugins || [])
.use(remarkRehype, {allowDangerousHtml: true, passThrough: nodeTypes})
.use(rehypePlugins || [])

if (format === 'md') {
pipeline.use(rehypeRemoveRaw)
}

pipeline
.use(rehypeRecma)
.use(recmaDocument, {...rest, outputFormat})
.use(recmaJsxRewrite, {providerImportSource, outputFormat})

if (!jsx) {
pipeline.use(recmaJsxBuild, {outputFormat})
}

pipeline.use(recmaStringify, {SourceMapGenerator}).use(recmaPlugins || [])

return pipeline
}
51 changes: 21 additions & 30 deletions lib/integration/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,32 +120,14 @@ export function esbuild(options = {}) {
)

let file = new VFile({value: doc, path: data.path})
/** @type {VFileValue|undefined} */
let value
/** @type {VFileMessage[]} */
let messages = []
/** @type {Message[]} */
const errors = []
/** @type {Message[]} */
const warnings = []
/** @type {VFileValue} */
let value
/** @type {VFileMessage} */
let message
/** @type {Point} */
let start
/** @type {Point} */
let end
/** @type {number} */
let length
/** @type {number} */
let lineStart
/** @type {number} */
let lineEnd
/** @type {RegExpExecArray} */
let match
/** @type {number} */
let line
/** @type {number} */
let column

try {
file = await process(file)
Expand All @@ -156,19 +138,21 @@ export function esbuild(options = {}) {
messages.push(error)
}

for (message of messages) {
for (const message of messages) {
/** @type {{start?: Point, end?: Point}} */
// Non-message errors stored on `vfile.messages`.
/* c8 ignore next */
const location = message.position || {}
start = location.start
end = location.end
length = 0
lineStart = 0
line = undefined
column = undefined

const start = location.start
const end = location.end
let length = 0
let lineStart = 0
let line = 0
let column = 0

if (
start &&
start.line != null &&
start.column != null &&
start.offset != null
Expand All @@ -178,14 +162,21 @@ export function esbuild(options = {}) {
lineStart = start.offset - column
length = 1

if (end.line != null && end.column != null && end.offset != null) {
if (
end &&
end.line != null &&
end.column != null &&
end.offset != null
) {
length = end.offset - start.offset
}
}

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

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

;(message.fatal ? errors : warnings).push({
pluginName: name,
text: message.reason,
Expand Down
2 changes: 1 addition & 1 deletion lib/integration/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function rollup(options = {}) {

if (filter(file.path) && extnames.includes(file.extname)) {
const compiled = await process(file)
// @ts-ignore `map` is added if a `SourceMapGenerator` is passed in.
// @ts-expect-error `map` is added if a `SourceMapGenerator` is passed in.
return {code: String(compiled.value), map: compiled.map}
// V8 on Erbium.
/* c8 ignore next 2 */
Expand Down
12 changes: 7 additions & 5 deletions lib/integration/webpack.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @typedef {import('webpack').LoaderContext<unknown>} LoaderContext
*/

import {getOptions} from 'loader-utils'
import {compile} from '../compile.js'

Expand All @@ -7,19 +11,17 @@ import {compile} from '../compile.js'
* be CommonJS.
* `file.map` is defined when a `SourceMapGenerator` is passed in options.
*
* @this {LoaderContext}
* @param {string} value
* @param {(error: Error|null|undefined, content?: string|Buffer, map?: Object) => void} callback
*/
export function loader(value, callback) {
compile(
// TS through JSDoc can’t handle `this`
// type-coverage:ignore-next-line
{value, path: this.resourcePath},
// TS through JSDoc can’t handle `this`
// type-coverage:ignore-next-line
// @ts-expect-error: types for webpack/loader-utils are out of sync.
{...getOptions(this)}
).then((file) => {
// @ts-ignore conflict between UInt8Array and Buffer is expected, and a tradeoff made in vfile typings to avoid `@types/node` being required
// @ts-expect-error conflict between UInt8Array and Buffer is expected, and a tradeoff made in vfile typings to avoid `@types/node` being required
callback(null, file.value, file.map)
return file
}, callback)
Expand Down
95 changes: 51 additions & 44 deletions lib/plugin/recma-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
* @typedef {import('estree-jsx').SimpleLiteral} SimpleLiteral
* @typedef {import('estree-jsx').Statement} Statement
* @typedef {import('estree-jsx').VariableDeclarator} VariableDeclarator
*
* @typedef {import('unified').Transformer} Transformer
*
* @typedef {import('vfile').VFile} VFile
* @typedef {import('estree-jsx').SpreadElement} SpreadElement
* @typedef {import('estree-jsx').Property} Property
*
* @typedef RecmaDocumentOptions
* @property {'program'|'function-body'} [outputFormat='program'] Whether to use either `import` and `export` statements to get the runtime (and optionally provider) and export the content, or get values from `arguments` and return things
Expand All @@ -41,8 +39,7 @@ import {isDeclaration} from '../util/estree-util-is-declaration.js'
/**
* A plugin to wrap the estree in `MDXContent`.
*
* @param {RecmaDocumentOptions} [options]
* @returns {Transformer}
* @type {import('unified').Plugin<[RecmaDocumentOptions]|[], Program>}
*/
export function recmaDocument(options = {}) {
const {
Expand All @@ -56,29 +53,25 @@ export function recmaDocument(options = {}) {
jsxRuntime = 'automatic'
} = options

// @ts-ignore root of an estree is a `Program`.
return transform

/**
* @param {Program} tree
* @param {VFile} file
* @returns {void}
*/
function transform(tree, file) {
return (tree, file) => {
/** @type {Array.<string|[string, string]>} */
const exportedIdentifiers = []
/** @type {Array.<Directive|Statement|ModuleDeclaration>} */
const replacement = []
/** @type {Array.<string>} */
const pragmas = []
let exportAllCount = 0
/** @type {ExportDefaultDeclaration|ExportSpecifier} */
/** @type {ExportDefaultDeclaration|ExportSpecifier|undefined} */
let layout
/** @type {boolean} */
/** @type {boolean|undefined} */
let content
/** @type {Node} */
let child

// Patch missing comments, which types say could occur.
/* c8 ignore next */
if (!tree.comments) tree.comments = []

if (jsxRuntime) {
pragmas.push('@jsxRuntime ' + jsxRuntime)
}
Expand Down Expand Up @@ -157,7 +150,7 @@ export function recmaDocument(options = {}) {
// ```
else if (child.type === 'ExportNamedDeclaration' && child.source) {
/** @type {SimpleLiteral} */
// @ts-ignore `ExportNamedDeclaration.source` can only be a string literal.
// @ts-expect-error `ExportNamedDeclaration.source` can only be a string literal.
const source = child.source

// Remove `default` or `as default`, but not `default as`, specifier.
Expand Down Expand Up @@ -220,10 +213,10 @@ export function recmaDocument(options = {}) {
handleEsm(child)
} else if (
child.type === 'ExpressionStatement' &&
// @ts-ignore types are wrong: `JSXElement`/`JSXFragment` are
// @ts-expect-error types are wrong: `JSXElement`/`JSXFragment` are
// `Expression`s.
(child.expression.type === 'JSXFragment' ||
// @ts-ignore "
// @ts-expect-error "
child.expression.type === 'JSXElement')
) {
content = true
Expand All @@ -250,28 +243,39 @@ export function recmaDocument(options = {}) {
type: 'ReturnStatement',
argument: {
type: 'ObjectExpression',
properties: [].concat(
Array.from({length: exportAllCount}).map(
properties: [
...Array.from({length: exportAllCount}).map(
/**
* @param {undefined} _
* @param {number} index
* @returns {SpreadElement}
*/
(_, index) => ({
type: 'SpreadElement',
argument: {type: 'Identifier', name: '_exportAll' + (index + 1)}
})
),
exportedIdentifiers.map((d) => ({
type: 'Property',
kind: 'init',
shorthand: typeof d === 'string',
key: {type: 'Identifier', name: typeof d === 'string' ? d : d[1]},
value: {
type: 'Identifier',
name: typeof d === 'string' ? d : d[0]
...exportedIdentifiers.map((d) => {
/** @type {Property} */
const prop = {
type: 'Property',
kind: 'init',
method: false,
computed: false,
shorthand: typeof d === 'string',
key: {
type: 'Identifier',
name: typeof d === 'string' ? d : d[1]
},
value: {
type: 'Identifier',
name: typeof d === 'string' ? d : d[0]
}
}
}))
)

return prop
})
]
}
})
} else {
Expand Down Expand Up @@ -317,19 +321,10 @@ export function recmaDocument(options = {}) {
* @returns {void}
*/
function handleEsm(node) {
/** @type {string} */
let value
/** @type {Statement|ModuleDeclaration} */
let replace
/** @type {Expression} */
let init
/** @type {Array.<VariableDeclarator>} */
let declarators

// Rewrite the source of the `import` / `export … from`.
// See: <https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier>
if (baseUrl && node.source) {
value = String(node.source.value)
let value = String(node.source.value)

try {
// A full valid URL.
Expand All @@ -349,6 +344,11 @@ export function recmaDocument(options = {}) {
node.source = create(node.source, {type: 'Literal', value})
}

/** @type {Statement|ModuleDeclaration|undefined} */
let replace
/** @type {Expression} */
let init

if (outputFormat === 'function-body') {
if (
// Always have a source:
Expand All @@ -365,6 +365,12 @@ export function recmaDocument(options = {}) {
)
}

// Just for types.
/* c8 ignore next 3 */
if (!node.source) {
throw new Error('Expected `node.source` to be defined')
}

// ```
// import 'a'
// //=> await import('a')
Expand Down Expand Up @@ -412,7 +418,8 @@ export function recmaDocument(options = {}) {
} else if (node.declaration) {
replace = node.declaration
} else {
declarators = node.specifiers
/** @type {Array.<VariableDeclarator>} */
const declarators = node.specifiers
.filter(
(specifier) => specifier.local.name !== specifier.exported.name
)
Expand Down Expand Up @@ -471,7 +478,7 @@ export function recmaDocument(options = {}) {
]
}
/** @type {Expression} */
// @ts-ignore types are wrong: `JSXElement` is an `Expression`.
// @ts-expect-error types are wrong: `JSXElement` is an `Expression`.
const consequent = element

return {
Expand Down
Loading

0 comments on commit a54de55

Please sign in to comment.