Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Updated doc-check to generate config + format errors #1255

Merged
merged 11 commits into from
May 26, 2023
Merged
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
"chai-parentheses": "^0.0.2",
"chai-string": "^1.5.0",
"chai-subset": "^1.6.0",
"chalk": "^5.2.0",
"conventional-changelog-conventionalcommits": "^5.0.0",
"cors": "^2.8.5",
"depcheck": "^1.4.3",
Expand Down
2 changes: 1 addition & 1 deletion src/config/tsconfig.aegir.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"stripInternal": true,
"resolveJsonModule": true
"resolveJsonModule": true,
},
"include": [
"src",
Expand Down
58 changes: 41 additions & 17 deletions src/document-check.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* eslint-disable no-console */

import chalk from 'chalk'
import fs from 'fs-extra'
import { globby } from 'globby'
import Listr from 'listr'
import merge from 'merge-options'
import { compileSnippets } from 'typescript-docs-verifier'
import { hasTsconfig } from './utils.js'
import { formatCode, formatError, fromRoot, hasTsconfig, isTypescript, readJson } from './utils.js'
/**
* @typedef {import("./types").GlobalOptions} GlobalOptions
* @typedef {import("./types").DocsVerifierOptions} DocsVerifierOptions
Expand All @@ -17,37 +20,58 @@ const tasks = new Listr(
/**
* @param {GlobalOptions & DocsVerifierOptions} ctx
*/
enabled: ctx => hasTsconfig,
enabled: ctx => hasTsconfig && !isTypescript,
/**
* @param {GlobalOptions & DocsVerifierOptions} ctx
* @param {Task} task
*/
task: async (ctx, task) => {
let tsconfigPath = 'tsconfig.json'
let configPath = './tsconfig-doc-check.aegir.json'
let markdownFiles = ['README.md']

if (ctx.tsConfigPath) {
tsconfigPath = `${ctx.tsConfigPath}/tsconfig.json`
if (ctx.tsConfigPath && ctx.tsConfigPath !== '.') {
configPath = `${ctx.tsConfigPath}/tsconfig.json`
}

if (ctx.inputFiles) {
markdownFiles = await globby(ctx.inputFiles)
}

compileSnippets({ markdownFiles, project: tsconfigPath })
.then((results) => {
results.forEach((result) => {
if (result.error) {
console.log(`Error compiling example code block ${result.index} in file ${result.file}`)
console.log(result.error.message)
console.log('Original code:')
console.log(result.snippet)
const userTSConfig = readJson(fromRoot('tsconfig.json'))

try {
fs.writeJsonSync(
configPath,
merge.apply({ concatArrays: true }, [
userTSConfig,
{
compilerOptions: {
target: 'esnext',
module: 'esnext',
noImplicitAny: true,
noEmit: true
}
}
})
})
.catch((error) => {
console.error('Error compiling TypeScript snippets', error)
])
)

const results = await compileSnippets({ markdownFiles, project: configPath })

results.forEach((result) => {
if (result.error) {
process.exitCode = 1
console.log(chalk.red.bold(`Error compiling example code block ${result.index} in file ${result.file}:`))
console.log(formatError(result.error))
console.log(chalk.blue.bold('Original code:'))
console.log(formatCode(result.snippet, result.linesWithErrors))
}
})
} catch (error) {
console.error('Error complining Typescript snippets ', error)
} finally {
fs.removeSync(configPath)
fs.removeSync(fromRoot('dist', 'tsconfig-doc-check.aegir.tsbuildinfo'))
}
}

}
Expand Down
26 changes: 26 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import readline from 'readline'
import { fileURLToPath } from 'url'
import { constants, createBrotliCompress, createGzip } from 'zlib'
import { download } from '@electron/get'
import chalk from 'chalk'
import envPaths from 'env-paths'
import { execa } from 'execa'
import extract from 'extract-zip'
Expand Down Expand Up @@ -513,3 +514,28 @@ async function * _glob (base, dir, pattern, options) {
}
}
}

/**
*
* @param {Error} error
* @returns
*/
export const formatError = (error) => ' ' + error.message.split('\n').join('\n ')

/**
*
* @param {string} code
* @param {number[]} errorLines
* @returns
*/
export const formatCode = (code, errorLines) => {
const lines = code.split('\n').map((line, index) => {
const lineNumber = index + 1
if (errorLines.includes(lineNumber)) {
return chalk.bold.red(`${String(lineNumber).padStart(2)}| ${line}`)
} else {
return `${String(lineNumber).padStart(2)}| ${line}`
}
})
return ' ' + lines.join('\n ')
}