From 640bc9de24830060f9fef4d0b719f08567e9a517 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 2 Jun 2023 11:45:51 +0100 Subject: [PATCH] Revert 09c36781 & tweak linting (#1501) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This diff reverts 09c36781 and tweaks how some of our linting was working. I'm not actually sure what caused the regression that 09c36781 was fixing - it was something to do with typescript being used to transpile eslintrc.js, but that being excluded from the tsconfig for those projects. I fixed that by removing `rootDir` from those, but that revealed some other issues with files not getting ignored correctly. I fixed the ignoring issue with a change I've wanted to make to these scripts for a while: only running them on files that are actually tracked by git, instead of on everything with a relevant extension. A side effect of that is that we have to re-implement .eslintignore support ourselves, but that's very straight forward: the `ignore` package that eslint uses is very easy to include. ### Change Type - [x] `internal` — Any other changes that don't affect the published package (will not publish a new version) ### Test Plan - ### Release Notes [internal-only] --- .prettierignore | 2 - apps/vscode/editor/package.json | 2 +- apps/vscode/editor/tsconfig.json | 3 +- apps/vscode/extension/package.json | 2 +- apps/vscode/extension/tsconfig.json | 3 +- public-yarn.lock | 3 +- scripts/check-scripts.ts | 8 ---- scripts/lint.ts | 71 +++++++++++++++++++++-------- scripts/package.json | 3 ++ 9 files changed, 62 insertions(+), 35 deletions(-) diff --git a/.prettierignore b/.prettierignore index 63b3db0f4..40e18103a 100644 --- a/.prettierignore +++ b/.prettierignore @@ -10,8 +10,6 @@ *.mdx **/_archive/* apps/docs/content.json -apps/vscode/extension/editor/index.js -apps/vscode/extension/editor/tldraw-assets.json apps/webdriver/www/index.js apps/vscode/extension/editor/* apps/examples/www \ No newline at end of file diff --git a/apps/vscode/editor/package.json b/apps/vscode/editor/package.json index e267760e0..6935727e6 100644 --- a/apps/vscode/editor/package.json +++ b/apps/vscode/editor/package.json @@ -30,7 +30,7 @@ "build": "yarn run -T tsx scripts/build.ts", "dev": "yarn run -T tsx scripts/dev.ts", "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist", - "lint": "yarn run -T eslint --report-unused-disable-directives --ignore-path ../../../.eslintignore" + "lint": "yarn run -T tsx ../../../scripts/lint.ts" }, "devDependencies": { "@tldraw/assets": "workspace:*", diff --git a/apps/vscode/editor/tsconfig.json b/apps/vscode/editor/tsconfig.json index cb90a5bb6..f056aeded 100644 --- a/apps/vscode/editor/tsconfig.json +++ b/apps/vscode/editor/tsconfig.json @@ -21,8 +21,7 @@ "composite": true, "importHelpers": false, "skipDefaultLibCheck": true, - "experimentalDecorators": true, - "rootDir": ".." + "experimentalDecorators": true }, "include": ["src", "../messages", "scripts", "../vscode-script-utils"], "references": [ diff --git a/apps/vscode/extension/package.json b/apps/vscode/extension/package.json index 9d86f2fbd..734b3820f 100644 --- a/apps/vscode/extension/package.json +++ b/apps/vscode/extension/package.json @@ -126,7 +126,7 @@ "build": "cd ../editor && yarn build && cd ../extension && tsx scripts/build.ts", "package": "yarn build && tsx scripts/package.ts", "publish": "vsce publish", - "lint": "yarn run -T eslint --report-unused-disable-directives --ignore-path ../../../.eslintignore", + "lint": "yarn run -T tsx ../../../scripts/lint.ts", "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf editor && rm -rf temp & yarn" }, "devDependencies": { diff --git a/apps/vscode/extension/tsconfig.json b/apps/vscode/extension/tsconfig.json index c1cee92fd..f5e2f8a0e 100644 --- a/apps/vscode/extension/tsconfig.json +++ b/apps/vscode/extension/tsconfig.json @@ -21,8 +21,7 @@ "composite": true, "importHelpers": false, "skipDefaultLibCheck": true, - "experimentalDecorators": true, - "rootDir": ".." + "experimentalDecorators": true }, "include": ["src", "../messages", "scripts", "../vscode-script-utils"], "references": [{ "path": "../../../packages/file-format" }] diff --git a/public-yarn.lock b/public-yarn.lock index e1dc26153..3b37f1387 100644 --- a/public-yarn.lock +++ b/public-yarn.lock @@ -4457,6 +4457,7 @@ __metadata: cross-fetch: ^3.1.5 esbuild: ^0.16.7 glob: ^8.0.3 + ignore: ^5.2.4 is-ci: ^3.0.1 kleur: ^4.1.5 lazyrepo: 0.0.0-alpha.26 @@ -10578,7 +10579,7 @@ __metadata: languageName: node linkType: hard -"ignore@npm:^5.1.1, ignore@npm:^5.2.0": +"ignore@npm:^5.1.1, ignore@npm:^5.2.0, ignore@npm:^5.2.4": version: 5.2.4 resolution: "ignore@npm:5.2.4" checksum: 3d4c309c6006e2621659311783eaea7ebcd41fe4ca1d78c91c473157ad6666a57a2df790fe0d07a12300d9aac2888204d7be8d59f9aaf665b1c7fcdb432517ef diff --git a/scripts/check-scripts.ts b/scripts/check-scripts.ts index 144cd1a2c..24bcf14af 100644 --- a/scripts/check-scripts.ts +++ b/scripts/check-scripts.ts @@ -63,14 +63,6 @@ const perPackageExceptions: Record string | undefin prepack: () => undefined, postpack: () => undefined, }, - 'tldraw-vscode': { - lint: () => - 'yarn run -T eslint --report-unused-disable-directives --ignore-path ../../../.eslintignore', - }, - '@tldraw/vscode-editor': { - lint: () => - 'yarn run -T eslint --report-unused-disable-directives --ignore-path ../../../.eslintignore', - }, } async function main({ fix }: { fix?: boolean }) { diff --git a/scripts/lint.ts b/scripts/lint.ts index 24c8fbb8b..5f42b17aa 100755 --- a/scripts/lint.ts +++ b/scripts/lint.ts @@ -1,33 +1,68 @@ -import { join, relative } from 'path' - +import ignore from 'ignore' +import * as path from 'path' import { exec } from './lib/exec' -import { REPO_ROOT } from './lib/file' +import { REPO_ROOT, readFileIfExists } from './lib/file' + +const ESLINT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] +const PRETTIER_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx', 'json'] async function main() { const shouldFix = process.argv.includes('--fix') + const lsFiles = await exec('git', ['ls-files', '.'], { + processStdoutLine: () => { + // don't print anything + }, + }) + + const filesByExtension = new Map() + for (const file of lsFiles.trim().split('\n')) { + const ext = file.split('.').pop() + if (!ext) continue + let files = filesByExtension.get(ext) + if (!files) { + files = [] + filesByExtension.set(ext.toLowerCase(), files) + } + files.push(file) + } + + let prettierFiles = PRETTIER_EXTENSIONS.flatMap((ext) => filesByExtension.get(ext) ?? []) + let eslintFiles = ESLINT_EXTENSIONS.flatMap((ext) => filesByExtension.get(ext) ?? []) + + const relativeCwd = path.relative(REPO_ROOT, process.cwd()) + + const prettierIgnoreFile = await readFileIfExists(path.join(REPO_ROOT, '.prettierignore')) + if (prettierIgnoreFile) { + prettierFiles = prettierFiles + .map((f) => path.join(relativeCwd, f)) + .filter(ignore().add(prettierIgnoreFile).createFilter()) + .map((f) => path.relative(relativeCwd, f)) + } + + const eslintIgnoreFile = await readFileIfExists(path.join(REPO_ROOT, '.eslintignore')) + if (eslintIgnoreFile) { + eslintFiles = eslintFiles + .map((f) => path.join(relativeCwd, f)) + .filter(ignore().add(eslintIgnoreFile).createFilter()) + .map((f) => path.relative(relativeCwd, f)) + } + try { - await exec( - 'yarn', - [ - 'run', - '-T', - 'prettier', - shouldFix ? '--write' : '--check', - // we have to run prettier from root so it picks up the ignore file - join(relative(REPO_ROOT, process.cwd()), '**', '*.{ts,tsx,js,jsx,json}'), - ], - { pwd: REPO_ROOT } - ) + await exec('yarn', [ + 'run', + '-T', + 'prettier', + shouldFix ? '--write' : '--check', + ...prettierFiles, + ]) await exec('yarn', [ 'run', '-T', 'eslint', '--report-unused-disable-directives', - '--ignore-path', - join(REPO_ROOT, '.eslintignore'), shouldFix ? '--fix' : null, - '.', + ...eslintFiles, ]) } catch (error) { process.exit(1) diff --git a/scripts/package.json b/scripts/package.json index 4e3f7174a..fbff4885d 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -47,5 +47,8 @@ }, "scripts": { "lint": "yarn run -T tsx lint.ts" + }, + "dependencies": { + "ignore": "^5.2.4" } }