@bartekbp/typescript-checkstyle

中文文档
5.0.0发布于 2021/07/17MITTypeScriptCommonJS近 30 天下载 1.9万

Library reporting tsc output in checkstyle format

原始使用文档
文档目录8 节
  1. Prerequisites
  2. Installation
  3. Usage
  4. Command line
  5. Pull request decoration with typescript errors
  6. Javascript
  7. License
  8. Acknowledgements

Typescript-checkstyle

Typescript-checkstyle is a tool for reporting typescript compiler output in checkstyle format. It was created to improve pull request decoration with compiler errors.

Prerequisites

Project requires:

  • nodejs 10
  • npm

Installation

All arifacts are published to npm. To use the latest version, you can install it using:

npm i --save-dev @bartekbp/typescript-checkstyle

Usage

Typescript-checkstyle can be used from command line and javascript.

Command line

Library reads tsc output from standard input. You may want to run both of them together. The following command typechecks your project and shows compilation errors in checkstyle format:

> npx tsc --noEmit | npx @bartekbp/typescript-checkstyle

If you already have npm scripts configured and want to use output from one of them to typescript-checkstyle, you need to invoke npm with --silent to suppress npm prologue:

> npm --silent run lint:tsc | npx @bartekbp/typescript-checkstyle

Pull request decoration with typescript errors

If you want to automate adding comments to your pull request based on complier output, typescript-checkstyle will help you achieve it. To configure it, I will use jenkins, but the solution applies to other continuous integration systems as well.

Jenkins pipleline

You need to setup a jenkins pipeline that will be triggered with bitbucket branch source plugin or github branch source plugin. In the pipeline you need to run typescript-checkstyle with tsc and redirect output to a file:

stage('tsc') {
  steps {
    sh  'npx tsc --noEmit | npx @bartekbp/typescript-checkstyle > tsc-checkstyle.xml'
  }
}

Depending on your code review system, tomasbjerre created different plugins for pull request decoration. You can find the full list of them at the bottom of violations-lib readme. I'll focus on the configuration for bitbucket cloud.

You need to update pipeline with additional stage for pr decoration:

stage('Decorate pr') {
  steps {
    withCredentials([usernamePassword(credentialsId: 'jenkins-pr-writes', passwordVariable: 'JENKINS_VIOLATION_PASSWORD', usernameVariable: 'JENKINS_VIOLATION_USER')]) {
      sh 'npx violation-comments-to-bitbucket-cloud-command-line -u "$JENKINS_VIOLATION_USER" -p "$JENKINS_VIOLATION_PASSWORD" -ws workspace -rs repoName -prid "$CHANGE_ID" -v "CHECKSTYLE" "." ".*tsc-checkstyle.xml$" "tsc" || true'
    }
  }
}

The bold parts in the stage needs to updated to reflect your project:

  • jenkins-pr-writes with credentials to bitbucket user
  • workspace with bitbucket workspace
  • repoName with repository that has an opened pull request

Javascript

Typescript-checkstyle exposes one function format:

/**
 * Converts tsc errors to checkstyle format
 *
 * @param {string} input - errors from tsc
 * @return {string} errors in checkstyle format
 function format(input) {}
 */

You can use it as follows:

import format from '@bartekbp/typescript-checkstyle';

const input = `src/test.component.ts(3, 12): error TS2564: Property 'name' has no initializer`;
const errorsInCheckstyleFormat = format(input);
console.log(errorsInCheckstyleFormat);

This code prints out errors from input in checkstyle format.

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements

Great thanks to:

  • tomasbjerre for his tools enabling pull request decoration
  • aivenkimmob for tsc output parser
  • jimf for tool to convert json to xml checkstyle format

版本与发布记录

@bartekbp/typescript-checkstyle 共发布 5 个版本,首次发布于 2021/01/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.0latest2021/07/175 年前
4.0.02021/07/175 年前
3.0.02021/01/245 年前
2.0.02021/01/235 年前
1.0.02021/01/235 年前

关于 @bartekbp/typescript-checkstyle 的常见问题

@bartekbp/typescript-checkstyle 是什么?

Library reporting tsc output in checkstyle format

如何安装 @bartekbp/typescript-checkstyle?

在项目目录运行 npm install @bartekbp/typescript-checkstyle,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 typescript-checkstyle,可以用 npx typescript-checkstyle 直接运行。

@bartekbp/typescript-checkstyle 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bartekbp/typescript-checkstyle 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bartekbp/typescript-checkstyle 可以商用吗?

@bartekbp/typescript-checkstyle 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bartekbp/typescript-checkstyle 的下载量有多少?

根据 npm 官方下载统计,@bartekbp/typescript-checkstyle 在 2026/10/08 – 2026/11/06 这 30 天内被下载 19,279 次,日均约 643 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bartekbp/typescript-checkstyle 还在维护吗?

最新版本 5.0.0 发布于 2021/07/17(5 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/01/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。