/
check-rule-examples.js
150 lines (126 loc) · 5.01 KB
/
check-rule-examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const { parse } = require("espree");
const { readFile } = require("fs").promises;
const glob = require("glob");
const markdownIt = require("markdown-it");
const markdownItContainer = require("markdown-it-container");
const { promisify } = require("util");
const markdownItRuleExample = require("../docs/tools/markdown-it-rule-example");
//------------------------------------------------------------------------------
// Typedefs
//------------------------------------------------------------------------------
/** @typedef {import("../lib/shared/types").LintMessage} LintMessage */
/** @typedef {import("../lib/shared/types").LintResult} LintResult */
/** @typedef {import("../lib/shared/types").ParserOptions} ParserOptions */
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const STANDARD_LANGUAGE_TAGS = new Set(["javascript", "js", "jsx"]);
/**
* Tries to parse a specified JavaScript code with Playground presets.
* @param {string} code The JavaScript code to parse.
* @param {ParserOptions} parserOptions Explicitly specified parser options.
* @returns {SyntaxError | null} A `SyntaxError` object if the code cannot be parsed, or `null`.
*/
function tryParseForPlayground(code, parserOptions) {
try {
parse(code, { ecmaVersion: "latest", ...parserOptions });
} catch (error) {
return error;
}
return null;
}
/**
* Checks the example code blocks in a rule documentation file.
* @param {string} filename The file to be checked.
* @returns {Promise<LintMessage[]>} A promise of problems found. The promise will be rejected if an error occurs.
*/
async function findProblems(filename) {
const text = await readFile(filename, "UTF-8");
const problems = [];
const ruleExampleOptions = markdownItRuleExample({
open({ code, parserOptions, codeBlockToken }) {
const languageTag = codeBlockToken.info;
if (!STANDARD_LANGUAGE_TAGS.has(languageTag)) {
/*
* Missing language tags are also reported by Markdownlint rule MD040 for all code blocks,
* but the message we output here is more specific.
*/
const message = `${languageTag
? `Nonstandard language tag '${languageTag}'`
: "Missing language tag"}: use one of 'javascript', 'js' or 'jsx'`;
problems.push({
fatal: false,
severity: 2,
message,
line: codeBlockToken.map[0] + 1,
column: codeBlockToken.markup.length + 1
});
}
const error = tryParseForPlayground(code, parserOptions);
if (error) {
const message = `Syntax error: ${error.message}`;
const line = codeBlockToken.map[0] + 1 + error.lineNumber;
const { column } = error;
problems.push({
fatal: false,
severity: 2,
message,
line,
column
});
}
}
});
// Run `markdown-it` to check rule examples in the current file.
markdownIt({ html: true })
.use(markdownItContainer, "rule-example", ruleExampleOptions)
.render(text);
return problems;
}
/**
* Checks the example code blocks in a rule documentation file.
* @param {string} filename The file to be checked.
* @returns {Promise<LintResult>} The result of checking the file.
*/
async function checkFile(filename) {
let fatalErrorCount = 0,
problems;
try {
problems = await findProblems(filename);
} catch (error) {
fatalErrorCount = 1;
problems = [{
fatal: true,
severity: 2,
message: `Error checking file: ${error.message}`
}];
}
return {
filePath: filename,
errorCount: problems.length,
warningCount: 0,
fatalErrorCount,
messages: problems
};
}
//------------------------------------------------------------------------------
// Main
//------------------------------------------------------------------------------
const patterns = process.argv.slice(2);
(async function() {
const globAsync = promisify(glob);
// determine which files to check
const filenames = (await Promise.all(patterns.map(pattern => globAsync(pattern, { nonull: true })))).flat();
const results = await Promise.all(filenames.map(checkFile));
if (results.every(result => result.errorCount === 0)) {
return;
}
const formatter = require("../lib/cli-engine/formatters/stylish");
const output = formatter(results);
console.error(output);
process.exitCode = 1;
}());