diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index e567aba89..44e2f4cb1 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -40,18 +40,3 @@ jobs: - name: Verify Docs run: yarn docs:verify - - - name: Check no `console.log()` in .jison files - # ESLint can't parse .jison files directly - # In the future, it might be worth making a `eslint-plugin-jison`, so - # that this will be built into the `yarn lint` command. - run: | - shopt -s globstar - mkdir -p tmp/ - for jison_file in src/**/*.jison; do - outfile="tmp/$(basename -- "$jison_file" .jison)-jison.js" - echo "Converting $jison_file to $outfile" - # default module-type (CJS) always adds a console.log() - yarn jison "$jison_file" --outfile "$outfile" --module-type "amd" - done - yarn eslint --no-eslintrc --rule no-console:error --parser "@babel/eslint-parser" "./tmp/*-jison.js" diff --git a/.lintstagedrc.json b/.lintstagedrc.json index db16ea99a..c53b7f626 100644 --- a/.lintstagedrc.json +++ b/.lintstagedrc.json @@ -1,4 +1,5 @@ { "src/docs/**": ["yarn docs:build --git"], - "*.{ts,js,json,html,md}": ["eslint --fix", "prettier --write"] + "*.{ts,js,json,html,md}": ["eslint --fix", "prettier --write"], + "*.jison": ["yarn lint:jison"] } diff --git a/package.json b/package.json index e4ff645bc..91d000832 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,9 @@ "docs:verify": "ts-node-esm src/docs.mts --verify", "postbuild": "documentation build src/mermaidAPI.ts src/config.ts src/defaultConfig.ts --shallow -f md --markdown-toc false > src/docs/Setup.md && prettier --write src/docs/Setup.md && yarn docs:build", "release": "yarn build", - "lint": "eslint --cache --ignore-path .gitignore . && prettier --check .", + "lint": "eslint --cache --ignore-path .gitignore . && yarn lint:jison && prettier --check .", "lint:fix": "eslint --fix --ignore-path .gitignore . && prettier --write .", + "lint:jison": "ts-node-esm src/jison/lint.mts", "cypress": "cypress run", "cypress:open": "cypress open", "e2e": "start-server-and-test dev http://localhost:9000/ cypress", @@ -83,6 +84,7 @@ "@commitlint/config-conventional": "^17.0.0", "@types/d3": "^7.4.0", "@types/dompurify": "^2.3.4", + "@types/eslint": "^8.4.6", "@types/express": "^4.17.13", "@types/jest": "^28.1.7", "@types/lodash": "^4.14.184", diff --git a/src/jison/lint.mts b/src/jison/lint.mts new file mode 100644 index 000000000..6e17851f7 --- /dev/null +++ b/src/jison/lint.mts @@ -0,0 +1,31 @@ +/* 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 => { + const jisonCode = await readFile(file, 'utf8'); + // @ts-ignore no typings + const jsCode = new jison.Generator(jisonCode, { moduleType: 'amd' }).generate(); + const [result] = await linter.lintText(jsCode); + if (result.errorCount > 0) { + console.error(`Linting failed for ${file}`); + console.error(result.messages); + } + return result.errorCount === 0; +}; + +(async () => { + const jisonFiles = await globby(['./src/**/*.jison'], { dot: true }); + const lintResults = await Promise.all(jisonFiles.map(lint)); + if (lintResults.some((result) => result === false)) { + process.exit(1); + } +})(); diff --git a/yarn.lock b/yarn.lock index 8cfad7e72..61ea96333 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2452,6 +2452,19 @@ dependencies: "@types/trusted-types" "*" +"@types/eslint@^8.4.6": + version "8.4.6" + resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-8.4.6.tgz#7976f054c1bccfcf514bff0564c0c41df5c08207" + integrity sha512-/fqTbjxyFUaYNO7VcW5g+4npmqVACz1bB7RTHYuLj+PRjw9hrCwrUXVQFpChUS0JsyEFvMZ7U/PfmvWgxJhI9g== + dependencies: + "@types/estree" "*" + "@types/json-schema" "*" + +"@types/estree@*": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.0.tgz#5fb2e536c1ae9bf35366eed879e827fa59ca41c2" + integrity sha512-WulqXMDUTYAXCjZnk6JtIHPigp55cVtDgDrO2gHRwhyJto21+1zbVCtOYB2L1F9w4qCQ0rOGWBnBe0FNTiEJIQ== + "@types/express-serve-static-core@^4.17.18": version "4.17.27" resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.27.tgz#7a776191e47295d2a05962ecbb3a4ce97e38b401" @@ -2529,6 +2542,11 @@ resolved "https://registry.yarnpkg.com/@types/json-buffer/-/json-buffer-3.0.0.tgz#85c1ff0f0948fc159810d4b5be35bf8c20875f64" integrity sha512-3YP80IxxFJB4b5tYC2SUPwkg0XQLiu0nWvhRgEatgjf+29IcWO9X1k8xRv5DGssJ/lCrjYTjQPcobJr2yWIVuQ== +"@types/json-schema@*": + version "7.0.11" + resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.11.tgz#d421b6c527a3037f7c84433fd2c4229e016863d3" + integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ== + "@types/json-schema@^7.0.9": version "7.0.9" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d"