mermaid/scripts/jison/lint.mts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-09-13 12:46:14 +05:30
/* eslint-disable no-console */
import { readFile } from 'fs/promises';
import { globby } from 'globby';
import { ESLint } from 'eslint';
// @ts-ignore no typings
import jison from 'jison';
const linter = new ESLint({
2024-06-29 16:31:19 +05:30
// @ts-expect-error ESLint types are incorrect
overrideConfigFile: true,
overrideConfig: { rules: { 'no-console': 'error' } },
2022-09-13 12:46:14 +05:30
});
const lint = async (file: string): Promise<boolean> => {
2022-11-14 14:51:23 +05:30
console.log(`Linting ${file}`);
2022-09-13 12:46:14 +05:30
const jisonCode = await readFile(file, 'utf8');
// @ts-ignore no typings
2023-04-24 00:15:51 +05:30
const generator = new jison.Generator(jisonCode, { moduleType: 'amd' });
const jsCode = generator.generate();
2022-09-13 12:46:14 +05:30
const [result] = await linter.lintText(jsCode);
if (result.errorCount > 0) {
console.error(`Linting failed for ${file}`);
console.error(result.messages);
}
2023-04-24 00:15:51 +05:30
if (generator.conflicts > 0) {
console.error(`Linting failed for ${file}. Conflicts found in grammar`);
return false;
}
2022-09-13 12:46:14 +05:30
return result.errorCount === 0;
};
const main = async () => {
2022-11-14 14:51:23 +05:30
const jisonFiles = await globby(['./packages/**/*.jison', '!./**/node_modules/**'], {
dot: true,
});
2022-09-13 12:46:14 +05:30
const lintResults = await Promise.all(jisonFiles.map(lint));
2022-11-23 01:05:08 +05:30
if (lintResults.includes(false)) {
2022-09-13 12:46:14 +05:30
process.exit(1);
}
};
void main();