mermaid/scripts/jison/lint.mts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-09-13 09:16:14 +02:00
/* 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({
overrideConfig: { rules: { 'no-console': 'error' }, parser: '@typescript-eslint/parser' },
useEslintrc: false,
});
const lint = async (file: string): Promise<boolean> => {
2022-11-14 10:21:23 +01:00
console.log(`Linting ${file}`);
2022-09-13 09:16:14 +02:00
const jisonCode = await readFile(file, 'utf8');
// @ts-ignore no typings
2023-04-23 20:45:51 +02:00
const generator = new jison.Generator(jisonCode, { moduleType: 'amd' });
const jsCode = generator.generate();
2022-09-13 09:16:14 +02:00
const [result] = await linter.lintText(jsCode);
if (result.errorCount > 0) {
console.error(`Linting failed for ${file}`);
console.error(result.messages);
}
2023-04-23 20:45:51 +02:00
if (generator.conflicts > 0) {
console.error(`Linting failed for ${file}. Conflicts found in grammar`);
return false;
}
2022-09-13 09:16:14 +02:00
return result.errorCount === 0;
};
const main = async () => {
2022-11-14 10:21:23 +01:00
const jisonFiles = await globby(['./packages/**/*.jison', '!./**/node_modules/**'], {
dot: true,
});
2022-09-13 09:16:14 +02:00
const lintResults = await Promise.all(jisonFiles.map(lint));
2022-11-22 20:35:08 +01:00
if (lintResults.includes(false)) {
2022-09-13 09:16:14 +02:00
process.exit(1);
}
};
void main();