Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

chore: Add oxlint plugin to warn about TS-private class members (#4035)

Jan Buchar80d7010f0628
diff --git a/oxlint.config.ts b/oxlint.config.ts
index 75ec13ccd..34dca9633 100644
--- a/oxlint.config.ts
+++ b/oxlint.config.ts
@@ -13,6 +13,7 @@ export default defineConfig({
'packages/templates/scripts/**',
'test/e2e/**',
],
+ jsPlugins: ['./scripts/oxlint-plugin.ts'],
globals: {
vi: 'readonly',
vitest: 'readonly',
@@ -56,6 +57,9 @@ export default defineConfig({
],
},
],
+ // Counterpart to `no-underscore-dangle` above: private members should be native `#` fields,
+ // not the TypeScript `private` modifier. `warn` until the existing usages are migrated.
+ 'crawlee/prefer-private-fields': 'warn',
'typescript/consistent-type-imports': ['error', { disallowTypeAnnotations: false }],
'typescript/consistent-type-definitions': ['error', 'interface'],
diff --git a/package.json b/package.json
index bbedd0099..3194c8a99 100644
--- a/package.json
+++ b/package.json
@@ -75,6 +75,7 @@
"@crawlee/types": "workspace:*",
"@crawlee/utils": "workspace:*",
"@microsoft/api-extractor": "^7.58.9",
+ "@oxlint/plugins": "^1.62.0",
"@playwright/browser-chromium": "1.61.1",
"@playwright/browser-firefox": "1.61.1",
"@playwright/browser-webkit": "1.61.1",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index bfcfa8978..191157074 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -85,6 +85,9 @@ importers:
'@microsoft/api-extractor':
specifier: ^7.58.9
version: 7.58.9(@types/[email protected])
+ '@oxlint/plugins':
+ specifier: ^1.62.0
+ version: 1.62.0
'@playwright/browser-chromium':
specifier: 1.61.1
version: 1.61.1
@@ -3752,6 +3755,10 @@ packages:
cpu: [x64]
os: [win32]
+ '@oxlint/[email protected]':
+ resolution: {integrity: sha512-BgKRw631mImmxuGuRoWaRT8AvNSJCfJvR7lDzP1jR3sBAa7q8P/Ii+lcEOEFZsfcfpkXLyUKGEQLYXWqb60wvw==}
+ engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
+
'@peculiar/[email protected]':
resolution: {integrity: sha512-vdG4fBF6Lkirkcl53q6eOdn3XYKt+kJTG59edgRZORlg/3atWWEReRCx5rYE1ZzTTX6vLK5zDMjHh7vbrcXGtw==}
@@ -16223,6 +16230,8 @@ snapshots:
'@oxlint/[email protected]':
optional: true
+ '@oxlint/[email protected]': {}
+
'@peculiar/[email protected]':
dependencies:
'@peculiar/asn1-schema': 2.6.0
diff --git a/scripts/oxlint-plugin.test.ts b/scripts/oxlint-plugin.test.ts
new file mode 100644
index 000000000..d4e06cf1c
--- /dev/null
+++ b/scripts/oxlint-plugin.test.ts
@@ -0,0 +1,40 @@
+import { RuleTester } from 'oxlint/plugins-dev';
+import { describe, it } from 'vitest';
+
+import { preferPrivateFields } from './oxlint-plugin.js';
+
+RuleTester.describe = describe;
+RuleTester.it = it;
+
+const ruleTester = new RuleTester({ languageOptions: { parserOptions: { lang: 'ts' } } });
+
+ruleTester.run('prefer-private-fields', preferPrivateFields, {
+ valid: [
+ 'class A { #a = 1; }',
+ 'class A { static #a = 1; }',
+ 'class A { readonly #a = 1; }',
+ 'class A { #a() {} }',
+ 'class A { a = 1; }',
+ 'class A { protected a = 1; }',
+ 'class A { public a = 1; }',
+ 'class A { constructor(readonly a: number) {} }',
+ 'abstract class A { protected abstract a(): void; }',
+ ],
+ invalid: [
+ { name: 'field', code: 'class A { private a = 1; }', errors: [{ messageId: 'preferHash' }] },
+ { name: 'static field', code: 'class A { private static a = 1; }', errors: [{ messageId: 'preferHash' }] },
+ { name: 'readonly field', code: 'class A { private readonly a = 1; }', errors: [{ messageId: 'preferHash' }] },
+ { name: 'method', code: 'class A { private a() {} }', errors: [{ messageId: 'preferHash' }] },
+ { name: 'getter', code: 'class A { private get a() { return 1; } }', errors: [{ messageId: 'preferHash' }] },
+ {
+ name: 'parameter property',
+ code: 'class A { constructor(private a: number) {} }',
+ errors: [{ messageId: 'preferHash' }],
+ },
+ {
+ name: 'abstract member',
+ code: 'abstract class A { private abstract a(): void; }',
+ errors: [{ messageId: 'preferHash' }],
+ },
+ ],
+});
diff --git a/scripts/oxlint-plugin.ts b/scripts/oxlint-plugin.ts
new file mode 100644
index 000000000..e5c3a0f5a
--- /dev/null
+++ b/scripts/oxlint-plugin.ts
@@ -0,0 +1,33 @@
+/**
+ * Local oxlint JS plugin. Loaded via `jsPlugins` in `oxlint.config.ts`.
+ */
+
+import type { ESTree, Plugin, Rule } from '@oxlint/plugins';
+
+// `PropertyDefinition`/`MethodDefinition` also cover their `TSAbstract*` variants — oxlint models
+// those as the same node shape with a different `type`, which is why the selector lists both.
+type PrivateMember = ESTree.PropertyDefinition | ESTree.MethodDefinition | ESTree.TSParameterProperty;
+
+export const preferPrivateFields = {
+ meta: {
+ messages: {
+ preferHash: 'Use a native `#private` field instead of the TypeScript `private` modifier.',
+ },
+ },
+ // `createOnce` is oxlint's faster alternative to ESLint's `create`; safe here because the rule
+ // keeps no per-file state.
+ createOnce(context) {
+ return {
+ 'PropertyDefinition[accessibility="private"], MethodDefinition[accessibility="private"], TSAbstractPropertyDefinition[accessibility="private"], TSAbstractMethodDefinition[accessibility="private"], TSParameterProperty[accessibility="private"]'(
+ node: PrivateMember,
+ ) {
+ context.report({ node: 'key' in node ? node.key : node, messageId: 'preferHash' });
+ },
+ };
+ },
+} satisfies Rule;
+
+export default {
+ meta: { name: 'crawlee' },
+ rules: { 'prefer-private-fields': preferPrivateFields },
+} satisfies Plugin;