Daw*_*n B 5 javascript parsing command-line-interface
在编写代码时,Linting 是一项非常宝贵的技术。然而,我发现自己想要更多地了解 linting 的过程。为此,我正在使用 node.js 构建一个基本的静态代码分析工具。
linter 应该做的是执行正则表达式检查,如果正则表达式匹配,则抛出错误(或警告,取决于用户的配置)。
我知道 linters 传统上解析代码,有些甚至对 AST 执行检查,但我想完全避免这种情况。我也明白我的方法通过完全避免解析语法来绕过几乎所有重要的 linting 部分。
目标是能够编写一些非常简单的检查,并将其作为快速原型设计的辅助 linter。(例如:放入^$\n^$
我的 linter 配置中,两个连续的空行会抛出错误)
该过程中似乎没有记录的部分是命令行的预期输出类型。以下是xo 的示例输出:
/Users/dawsonbotsford/code/regexLinter/cli.js
42:9 error Expected indentation of 6 space characters but found 8 indent
43:9 error Expected indentation of 6 space characters but found 8 indent
43:32 error Missing semicolon semi
Run Code Online (Sandbox Code Playgroud)
和 eslint 示例输出:
/Users/dawsonbotsford/code/regexLinter/cli.js
3:1 error Parsing error: The keyword 'const' is reserved
Run Code Online (Sandbox Code Playgroud)
我将如何使用正确类型的 shell 错误/警告来模拟此输出,以便它可以插入到 sublime-contrib 插件、CI 服务器等中?
如果您希望它是可插入的,那么您可以做的最简单的事情就是为ESLint编写一个插件(如果您需要捕获格式问题)。如果你想转换代码,请使用Putout,这是我正在使用的工具,它还有一个ESLint插件,因此你将要显示的消息可以在终端、IDE、CI 等中访问。
\n\n\n该过程中似乎没有记录的部分是命令行期望的输出类型。
\n
问题是命令行不关心任何 CLI 工具生成的输出,重要的是用户尽快理解输出的能力。为此,ESLint有很多格式化程序。由于可插入性和文档格式,任何人都可以为他们需要的情况编写格式化程序。例如,json
格式化程序有助于通过任何其他工具解析ESLint的输出,并以您需要的方式显示它。
Putout有类似的使用formatter的方式,默认情况下它显示进度条,但在 CI 上你不需要它,所以它切换到dump
formatter,它只显示最终结果。
因此,如果您需要ESLint支持,或者想要使用现有的格式化程序来显示错误,则需要支持其 API,例如,@putout/formatter-eslint
让Putout能够使用任何现有的ESLint格式化程序,因为它将其输出格式转换为一种ESLint使用的。
\n\n我如何使用正确类型的 shell 错误/警告来模拟此输出
\n
模仿 ESLint 默认输出的方法之一是使用库,我在@putout/formatter-dumptable
中这样做了:
for (const {name, places} of json.errors) {\n const line = buildLine(places);\n \n output.push([\n underline(name),\n table(line, {\n border: getBorderCharacters(\'void\'),\n drawHorizontalLine: () => false,\n }),\n ].join(\'\\n\'));\n}\n \noutput.push(bold(redBright(`\xe2\x9c\x96 ${errorsCount} errors in ${filesCount} files`)));\noutput.push(bold(redBright(\' fixable with the `--fix` option\')));\n
Run Code Online (Sandbox Code Playgroud)\n它构建具有不可见边框的表格以及其中的所有数据。
\n 归档时间: |
|
查看次数: |
381 次 |
最近记录: |