包详细信息

typescript-docs-verifier

bbc18.7kApache-2.02.5.3

Verifies that typescript examples in markdown files actually compile.

block, blocks, build, check

自述文件

typescript-docs-verifier

Verifies that typescript examples in markdown files actually compile.

TypeScript JavaScript Style Guide Apache 2.0 TypeScript docs verifier

Why?

Ever copied a TypeScript code example from a README and found that it didn't even compile? This tool can help by verifying that all of your code examples compile correctly. And yes, the TypeScript code samples in this README are checked using this tool.

demo

Inspired the by the tut documentation compilation tool for scala.

How it works

The selected markdown files are searched for TypeScript code blocks marked like this:

```typescript
// Some TypeScript code here
const write = 'some code';
```

These code blocks are extracted and any imports from the current project are replaced with an import of the main or exports from package.json (e.g. import { compileSnippets } from 'typescript-docs-verifier' would be replaced with import { compileSnippets } from './dist/index' for this project).

Each code snippet is compiled (but not run) and any compilation errors are reported. Code snippets must compile independently from any other code snippets in the file.

The library can also be used to type check .tsx files:

```tsx
import React from 'react'

const SomeComponent = () => (
  <div>
    This is a TSX component!
  </div>
)
```

Ignoring code blocks

Individual code blocks can be ignored by preceding them with a <!-- ts-docs-verifier:ignore --> comment:

<!-- ts-docs-verifier:ignore -->
```typescript
// This block won't be compiled by typescript-docs-verifier
```

Script usage

node_modules/.bin/typescript-docs-verifier [--input-files <markdown-files-to-test>] [--project <path-to-tsconfig-file>]
  • --input-files is optional and defaults to README.md.
  • --project is optional and defaults to the tsconfig.json file in the package root.
  • Any compilation errors will be reported on the console.
  • The exit code is 1 if there are any compilation errors and 0 otherwise.

Library usage

TypeScript

import { compileSnippets, SnippetCompilationResult } from 'typescript-docs-verifier'
import * as http from 'http'

const markdownFiles = ['README', 'examples.md'] // defaults to 'README.md' if not provided
const tsconfigPath = 'docs-tsconfig.json' // defaults to the 'tsconfig.json' file in the package root
compileSnippets({ markdownFiles, project: tsconfigPath })
  .then((results: SnippetCompilationResult[]) => {
    results.forEach((result: SnippetCompilationResult) => {
      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)
      }
    })
  })
  .catch((error: unknown) => {
    console.error('Error compiling TypeScript snippets', error)
  })

JavaScript

const { compileSnippets } = require('typescript-docs-verifier')

const markdownFiles = ['README.md', 'examples.md'] // defaults to 'README.md' if not provided
const tsconfigPath = 'docs-tsconfig.json' // defaults to the 'tsconfig.json' file in the package root
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)
      }
    })
  })
  .catch((error) => {
    console.error('Error compiling TypeScript snippets', error)
  })

Development

Run the tests:

npm install
npm test

Contributing

See these notes for information for contributors.

License

typescript-docs-verifier is available to all via the Apache-2.0 license.

Copyright © 2017 BBC

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

[2.5.3]

Changed

  • Use a separate ts-node compiler per-snippet to ensure that compilation of snippets is independent

[2.5.2]

Removed

  • Obsolete Travis CI build badge from README

[2.5.1]

Added

  • Override any project-specific ts-node transpileOnly to force type checking when compiling code snippets

[2.5.0]

Added

  • Support for tsx snippets

[2.4.1]

Changed

  • Various fixes for Windows environments

[2.4.0]

Added

  • A new --project option that overrides the tsconfig.json file to be used when compiling snippets

[2.3.1]

Changed

  • Update dependencies

[2.3.0]

Added

  • Support for exports in package.json including wildcard subpaths

Changed

  • Compile documentation snippets in the project folder so that dependent packages can be resolved reliably even in nested projects

[2.2.2]

Changed

  • Unpinned ts-node dependency to fix issues with the most recent TypeScript versions (required the extraction of the line numbers of compilation errors to be changed)

[2.2.1]

Added

  • Allow code blocks to be ignored by preceding them with a <!-- ts-docs-verifier:ignore --> comment

[2.2.0]

Changed

  • Link project node_modules to snippet compilation directory so you can import from the current project's dependencies in snippets

Added

  • Support for importing sub-paths within packages
  • Support for scoped package names

[2.1.0]

Changed

  • No longer wrap TypeScript code blocks in functions before compilation
  • Write temporary files to the OS temporary directory

Added

  • An 1-indexed index property to the CodeBlock type to indicate where in the file the code block was found
  • Add a linesWithErrors property to the compilation result to indicate which lines contained errors

[2.0.1] - 2021-10-08

Changed

  • Updated dependencies (including dropping tslint in favour of eslint)
    • Pin to version 5.x.x of ora to avoid issues with ESM

[2.0.0-rc.1] - 2021-09-27

Added

  • Support for TypeScript code blocked marked with ```ts as well as ```typescript
  • This changelog 🎉

Changed

  • [BREAKING] TypeScript is now a peerDependency and must be installed by the client.
  • Tagging of the source is now done using a release branch.
  • Migrated code to async / await.

Removed

  • [BREAKING] Support for NodeJS versions prior to version 12.
  • Bluebird dependency.