Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESLint and lint codebase #2

Merged
merged 9 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore → .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__tests__/*/results*.json5
dist
116 changes: 116 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
module.exports = {
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/errors",
"plugin:import/typescript",
"prettier",
],
plugins: ["tsdoc", "simple-import-sort", "import"],
env: {
node: true,
es6: true,
},
rules: {
// We need this for our `GraphileEngine` namespace
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/consistent-type-imports": "error",
"no-confusing-arrow": "off",
"no-else-return": "off",
"no-underscore-dangle": "off",
"no-restricted-syntax": "off",
"no-await-in-loop": "off",
"tsdoc/syntax": "error",
"@typescript-eslint/no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
args: "after-used",
ignoreRestSiblings: true,
},
],

/*
* simple-import-sort seems to be the most stable import sorting currently,
* disable others
*/
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error",
"sort-imports": "off",
"import/order": "off",

"import/extensions": ["error", "ignorePackages"],
"import/no-deprecated": "warn",

// Apply has been more optimised than spread, use whatever feels right.
"prefer-spread": "off",

// note you must disable the base rule as it can report incorrect errors
"no-duplicate-imports": "off",
"import/no-duplicates": "error",
},
overrides: [
// Rules for interfaces.ts files
{
files: ["**/interfaces.ts"],
rules: {
"no-restricted-syntax": [
"error",
{
selector: "TSModuleDeclaration[kind='global']",
message:
"No `declare global` allowed in `interface.ts` files since these type-only files may not be imported by dependents, recommend adding to `index.ts` instead.",
},
],
},
},

// Rules for TypeScript only
{
files: ["*.ts", "*.tsx"],
parser: "@typescript-eslint/parser",
rules: {
"no-dupe-class-members": "off",
"no-undef": "off",
// This rule doesn't understand import of './js'
"import/no-unresolved": "off",
},
},

// Rules for JavaScript only
{
files: ["*.js", "*.jsx", "*.mjs", "*.cjs"],
rules: {
"tsdoc/syntax": "off",
"import/extensions": "off",
},
},

// Stricter rules for source code
{
files: ["*/*/src/**/*.ts", "*/*/src/**/*.tsx"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: true,
},
rules: {},
},

// Rules for tests only
{
files: ["**/__tests__/**/*.{ts,js,mts,mjs}"],
rules: {
// Disable these to enable faster test writing
"prefer-const": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/explicit-function-return-type": "off",

// We don't normally care about race conditions in tests
"require-atomic-updates": "off",
},
},
],
};
44 changes: 29 additions & 15 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
name: Tests
name: CI

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:
build:
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: 20.x
cache: "npm"
- run: yarn
# We need to build for linting due to `import` checks in `__tests__` files
- run: yarn build
- run: yarn lint

tests:
runs-on: ubuntu-latest

strategy:
Expand All @@ -17,14 +31,14 @@ jobs:
graphql-version: [15.x, 16.x, ^17.0.0-alpha.3]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: yarn
- run: yarn build
# We must build BEFORE we replace the GraphQL version (because otherwise the types don't match)
- run: yarn add graphql@${{ matrix.graphql-version }}
- run: yarn test
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: yarn
- run: yarn build
# We must build BEFORE we replace the GraphQL version (because otherwise the types don't match)
- run: yarn add graphql@${{ matrix.graphql-version }}
- run: yarn test
3 changes: 2 additions & 1 deletion __tests__/common.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// @ts-check

import { diff } from "jest-diff";
import assert from "assert";
import { readdir, readFile, writeFile } from "fs/promises";
import { diff } from "jest-diff";
import JSON5 from "json5";

import { printResults } from "../dist/index.js";

/** @import { SourceLike, CheckOperationsResult } from '../dist/index.js' */
Expand Down
3 changes: 2 additions & 1 deletion __tests__/depth-limit-basics/tests.test.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @ts-check

import { it } from "node:test";

import {
checkOperations,
filterBaseline,
generateBaseline,
} from "../../dist/index.js";
import { it } from "node:test";
import { getDirHelpers } from "../common.mjs";

const __dirname = new URL(".", import.meta.url).pathname;
Expand Down
3 changes: 2 additions & 1 deletion __tests__/depth-limit-large-schema/tests.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @ts-check

import { checkOperations } from "../../dist/index.js";
import { it } from "node:test";

import { checkOperations } from "../../dist/index.js";
import { getDirHelpers } from "../common.mjs";

const __dirname = new URL(".", import.meta.url).pathname;
Expand Down
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@
"scripts": {
"build": "tsc -p tsconfig.build.json",
"watch": "tsc -p tsconfig.build.json -w",
"lint": "yarn run eslint . && yarn prettier:check",
"lint:fix": "yarn run eslint --fix . && yarn prettier:fix",
"test": "node --test",
"prepack": "rm -Rf dist && yarn build",
"version": "node scripts/postversion.mjs && git add src/version.ts",
"test": "node --test"
"-----": "-----",
"eslint": "eslint --ext .js,.jsx,.ts,.tsx,.mjs,.cjs",
"prettier": "prettier --cache --ignore-path ./.eslintignore",
"prettier:all": "yarn prettier '**/*.{json,md,mdx,html,js,jsx,ts,tsx,mjs,cjs,mts,cts,yml}'",
"prettier:fix": "yarn prettier:all --write",
"prettier:check": "yarn prettier:all --list-different"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -44,10 +52,11 @@
"@types/node": "^20.14.2",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"eslint": "^9.5.0",
"eslint": "8.x",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-simple-import-sort": "^12.1.0",
"eslint-plugin-tsdoc": "^0.3.0",
"eslint_d": "^13.1.2",
"jest-diff": "^29.7.0",
"prettier": "^3.3.2",
Expand Down
2 changes: 1 addition & 1 deletion scripts/postversion.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFile, readFile } from "node:fs/promises";
import { readFile, writeFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";

const __dirname = fileURLToPath(new URL(".", import.meta.url)).replace(
Expand Down
24 changes: 14 additions & 10 deletions src/DepthVisitor.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import * as assert from "node:assert";
import {

import type {
ASTVisitor,
FragmentDefinitionNode,
GraphQLOutputType,
OperationDefinitionNode,
} from "graphql";
import {
GraphQLList,
GraphQLNonNull,
GraphQLOutputType,
isCompositeType,
isNamedType,
Kind,
OperationDefinitionNode,
} from "graphql";
import { RulesContext } from "./rulesContext";
import { RuleError } from "./ruleError";

import { RuleError } from "./ruleError.js";
import type { RulesContext } from "./rulesContext.js";

interface DepthInfo {
current: number;
Expand Down Expand Up @@ -287,13 +291,13 @@ export function DepthVisitor(context: RulesContext): ASTVisitor {

return {
Document: {
enter(node) {
enter(_node) {
if (!state.complete) {
console.warn("Previous DepthVisitor didn't complete cleanly");
}
state = newState();
},
leave(node) {
leave(_node) {
// Finalize depths by applying all the fragment depths to the operations
const resolvedOperations = resolveRoots(state);
const resolvedPreset = context.getResolvedPreset();
Expand Down Expand Up @@ -437,7 +441,7 @@ export function DepthVisitor(context: RulesContext): ASTVisitor {
currentRoot = newRoot(node);
state.roots.push(currentRoot);
},
leave(node) {
leave(_node) {
currentRoot = null;
},
},
Expand All @@ -451,7 +455,7 @@ export function DepthVisitor(context: RulesContext): ASTVisitor {
currentRoot = newRoot(node);
state.roots.push(currentRoot);
},
leave(node) {
leave(_node) {
currentRoot = null;
},
},
Expand Down Expand Up @@ -490,7 +494,7 @@ export function DepthVisitor(context: RulesContext): ASTVisitor {
// Visiting same fragment again; ignore
}
},
leave(node) {
leave(_node) {
// No specific action required
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/baseline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
Baseline,
CheckDocumentOutput,
SourceResultsBySourceName,
Expand All @@ -11,7 +11,7 @@ export function generateBaseline(
version: 1,
operations: {},
};
for (const [sourceName, { output }] of Object.entries(resultsBySourceName)) {
for (const [_sourceName, { output }] of Object.entries(resultsBySourceName)) {
const { errors } = output;
for (const error of errors) {
if ("infraction" in error) {
Expand Down
15 changes: 9 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/usr/bin/env node
import { open, readdir, readFile, stat, writeFile } from "node:fs/promises";
import type { ParseArgsConfig } from "node:util";
import { parseArgs } from "node:util";

import JSON5 from "json5";
import { parseArgs, ParseArgsConfig } from "node:util";
import { checkOperations } from "./main";
import { open, readdir, stat, readFile, writeFile } from "node:fs/promises";
import { kjsonlLines } from "kjsonl";
import { SourceLike } from "./interfaces";

import { filterBaseline, generateBaseline } from "./baseline.js";
import type { SourceLike } from "./interfaces.js";
import { checkOperations } from "./main.js";
import { printResults } from "./print.js";
import { filterBaseline, generateBaseline } from "./baseline";
import { version } from "./version";
import { version } from "./version.js";

const parseArgsConfig = {
options: {
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export { filterBaseline, generateBaseline } from "./baseline.js";
export {
Baseline,
CheckOperationsResult,
SourceLike,
SourceResultsBySourceName as SourceResultBySourceName,
CheckOperationsResult,
Baseline,
} from "./interfaces.js";
export { checkOperations } from "./main.js";
export { printResults } from "./print.js";
export { filterBaseline, generateBaseline } from "./baseline.js";

declare global {
namespace GraphileConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLFormattedError } from "graphql";
import type { GraphQLFormattedError } from "graphql";

export interface WorkerData {
configPath: string | null | undefined;
Expand Down
Loading
Loading