Skip to content

Commit

Permalink
Setup eslint for polyfills package.
Browse files Browse the repository at this point in the history
  • Loading branch information
CXuesong committed Jul 24, 2024
1 parent 1f0832b commit d2f6f2a
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 87 deletions.
6 changes: 6 additions & 0 deletions packages/@jscorlib-repo/linters/src/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as path from "node:path";
import * as url from "node:url";

// Make ESLint running in VSCode happy.
// VSCode is on Node 20 right now. import.meta.dirname is not available yet.
export const repoRootDir = path.resolve(url.fileURLToPath(import.meta.url), "../../../../..");
82 changes: 82 additions & 0 deletions packages/@jscorlib-repo/linters/src/eslint-rules/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// @ts-check
import eslint from "@eslint/js";
import stylistic from "@stylistic/eslint-plugin";
import jsdoc from "eslint-plugin-jsdoc";
import tseslint from "typescript-eslint";
import { repoRootDir } from "../environment.js";

export const baseConfig = tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs.stylisticTypeChecked,
{
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: repoRootDir,
},
},
rules: {
"@typescript-eslint/array-type": ["error", {
default: "array-simple",
readonly: "array-simple",
}],
"@typescript-eslint/no-unused-vars": [
"error",
{
"args": "all",
"argsIgnorePattern": "^_",
"caughtErrors": "all",
"caughtErrorsIgnorePattern": "^_",
"destructuredArrayIgnorePattern": "^_",
// https://github.com/tc39/proposal-discard-binding
"varsIgnorePattern": "^_void",
"ignoreRestSiblings": true,
},
],
"@typescript-eslint/explicit-member-accessibility": "error",
"@typescript-eslint/prefer-nullish-coalescing": [
"error", {
ignorePrimitives: {
string: true,
},
},
],
"@typescript-eslint/no-inferrable-types": [
"error", {
ignoreParameters: true,
ignoreProperties: true,
},
],
// This rule does not handle `unknown` well
"@typescript-eslint/restrict-template-expressions": "warn",
"@typescript-eslint/unbound-method": ["error", { ignoreStatic: true }],
},
},
{
plugins: {
"@stylistic": stylistic,
},
rules: {
"@stylistic/indent": ["error", 2],
"@stylistic/quotes": ["error", "double", {
"avoidEscape": true,
"allowTemplateLiterals": true,
}],
"@stylistic/semi": "error",
"@stylistic/comma-dangle": ["error", "always-multiline"],
"@stylistic/eol-last": ["error", "always"],
},
},
{
plugins: {
jsdoc,
},
rules: {
"jsdoc/no-undefined-types": ["warn", {
markVariablesAsUsed: true,
disableReporting: true,
}],
},
},
);
1 change: 1 addition & 0 deletions packages/@jscorlib-repo/linters/src/eslint-rules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./base.js";
7 changes: 7 additions & 0 deletions packages/@jscorlib-repo/linters/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
import tseslint from "typescript-eslint";

/** Absolute path of repository root folder. */
export const repoRootDir: string;

export namespace ESLintRules {
// See https://github.com/typescript-eslint/typescript-eslint/issues/8571
export const baseConfig: ReturnType<typeof tseslint.config>;
}
7 changes: 2 additions & 5 deletions packages/@jscorlib-repo/linters/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
import * as path from "node:path";
import * as url from "node:url";

// VSCode is on Node 20 right now. import.meta.dirname is not available.
export const repoRootDir = path.resolve(url.fileURLToPath(import.meta.url), "../../../../..");
export * from "./environment.js";
export * as ESLintRules from "./eslint-rules/index.js";
7 changes: 7 additions & 0 deletions packages/@jscorlib/polyfills/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @ts-check
import * as JscorlibLinters from "@jscorlib-repo/linters";
import tseslint from "typescript-eslint";

export default tseslint.config(
...JscorlibLinters.ESLintRules.baseConfig,
);
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ export class DisposableStack implements globalThis.DisposableStack {
[Symbol.dispose]: () => {
onDispose();
},
})
});
}
public move(): DisposableStack {
const inst = new DisposableStack();
inst._stack = this._stack;
this._stack = undefined;
return inst;
}
public [Symbol.dispose] = this.dispose;
public readonly [Symbol.toStringTag] = "jscorlib::DisposableStack"
public [Symbol.dispose](): void {
this.dispose();
}
public readonly [Symbol.toStringTag] = "jscorlib::DisposableStack";
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*
* @module
*/
export * from "./disposableStack"
export * from "./installer"
export * from "./disposableStack";
export * from "./installer";
1 change: 1 addition & 0 deletions packages/@jscorlib/polyfills/src/promises/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { withResolvers } from "./withResolvers";

export function installPolyfill(options: InstallPolyfillOptions): void {
const { globalThis: global = globalThis } = options;
// eslint-disable-next-line @typescript-eslint/unbound-method
global.Promise.withResolvers ??= withResolvers;
}
4 changes: 2 additions & 2 deletions packages/@jscorlib/polyfills/src/promises/withResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Polyfill of {@link Promise.withResolvers}.
* @see [ES2023: ES Promise.withResolvers](https://github.com/tc39/proposal-promise-with-resolvers)
*/
export function withResolvers<T>(): PromiseWithResolvers<T> {
export function withResolvers<T>(this: void): PromiseWithResolvers<T> {
const result = {} as PromiseWithResolvers<T>;
result.promise = new Promise((res, rej) => {
result.resolve = res;
result.reject = rej;
})
});
return result;
}
76 changes: 1 addition & 75 deletions packages/jscorlib/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,7 @@
// @ts-check
import eslint from "@eslint/js";
import * as JscorlibLinters from "@jscorlib-repo/linters";
import stylistic from "@stylistic/eslint-plugin";
import jsdoc from "eslint-plugin-jsdoc";
import tseslint from "typescript-eslint";

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
...tseslint.configs.stylisticTypeChecked,
{
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: JscorlibLinters.repoRootDir,
},
},
rules: {
"@typescript-eslint/array-type": ["error", {
default: "array-simple",
readonly: "array-simple",
}],
"@typescript-eslint/no-unused-vars": [
"error",
{
"args": "all",
"argsIgnorePattern": "^_",
"caughtErrors": "all",
"caughtErrorsIgnorePattern": "^_",
"destructuredArrayIgnorePattern": "^_",
// https://github.com/tc39/proposal-discard-binding
"varsIgnorePattern": "^_void",
"ignoreRestSiblings": true,
},
],
"@typescript-eslint/explicit-member-accessibility": "error",
"@typescript-eslint/prefer-nullish-coalescing": [
"error", {
ignorePrimitives: {
string: true,
},
},
],
"@typescript-eslint/no-inferrable-types": [
"error", {
ignoreParameters: true,
ignoreProperties: true,
},
],
// This rule does not handle `unknown` well
"@typescript-eslint/restrict-template-expressions": "warn",
},
},
{
plugins: {
"@stylistic": stylistic,
},
rules: {
"@stylistic/indent": ["error", 2],
"@stylistic/quotes": ["error", "double", {
"avoidEscape": true,
"allowTemplateLiterals": true,
}],
"@stylistic/semi": "error",
"@stylistic/comma-dangle": ["error", "always-multiline"],
"@stylistic/eol-last": ["error", "always"],
},
},
{
plugins: {
jsdoc,
},
rules: {
"jsdoc/no-undefined-types": ["warn", {
markVariablesAsUsed: true,
disableReporting: true,
}],
},
},
...JscorlibLinters.ESLintRules.baseConfig,
);

0 comments on commit d2f6f2a

Please sign in to comment.