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

remove truthy helper function #2797

Merged
merged 12 commits into from
Dec 3, 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
5 changes: 5 additions & 0 deletions .changeset/thick-candles-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

remove `truthy` helper function
3 changes: 0 additions & 3 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ module.exports = {
},
{
files: ['**/*.{spec,test}.ts'],
env: {
jest: true,
},
extends: ['plugin:eslint-plugin/tests-recommended'],
rules: {
'eslint-plugin/test-case-shorthand-strings': 'error',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"tsup": "^8.3.5",
"tsx": "4.19.2",
"turbo": "2.3.3",
"typescript": "5.6.3",
"typescript": "5.7.2",
"vitest": "2.1.8"
},
"pnpm": {
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin/__tests__/examples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ function parseESLintOutput({
/\(node:\d+\) ESLintRCWarning: You are using an eslintrc configuration file, which is deprecated and support will be removed in v10.0.0. Please migrate to an eslint.config.js file. See https:\/\/eslint.org\/docs\/latest\/use\/configure\/migration-guide for details./,
'',
)
.replace(
'An eslintrc configuration file is used because you have the ESLINT_USE_FLAT_CONFIG environment variable set to false. If you want to use an eslint.config.js file, remove the environment variable. If you want to find the location of the eslintrc configuration file, use the --debug flag.',
'',
)
.replace('(Use `node --trace-deprecation ...` to show where the warning was created)', '')
.replace('(Use `node --trace-warnings ...` to show where the warning was created)', '')
.trimEnd();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
query {
{
hello
}
5 changes: 1 addition & 4 deletions packages/plugin/__tests__/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ describe('schema', async () => {

it('should load the graphql-config rc file relative to the linted file', () => {
const gqlConfig = loadGraphQLConfig({
graphQLConfig: {
schema: path.resolve(__dirname, 'mocks/using-config/schema-in-config.graphql'),
},
filePath: path.resolve(__dirname, 'mocks/using-config/test.graphql'),
filePath: path.resolve(__dirname, 'mocks/using-config/nested/test.graphql'),
});

const graphQLSchema = getSchema(gqlConfig.getDefault()) as GraphQLSchema;
Expand Down
15 changes: 5 additions & 10 deletions packages/plugin/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { asArray } from '@graphql-tools/utils';
import { loadOnDiskGraphQLConfig } from './graphql-config.js';
import { version } from './meta.js';
import { CWD, REPORT_ON_FIRST_CHARACTER, truthy } from './utils.js';
import { CWD, REPORT_ON_FIRST_CHARACTER } from './utils.js';

export type Block = Linter.ProcessorFile & {
lineOffset: number;
Expand Down Expand Up @@ -50,15 +50,10 @@ export const processor = {
gqlMagicComment = 'GraphQL',
} = pluckConfig;

keywords = [
...new Set(
[
...modules.map(({ identifier }) => identifier),
...asArray(globalGqlIdentifierName),
gqlMagicComment,
].filter(truthy),
),
];
const mods = modules.map(({ identifier }) => identifier).filter((v): v is string => !!v);

const result = [...mods, ...asArray(globalGqlIdentifierName), gqlMagicComment];
keywords = [...new Set(result)];
}

if (keywords.every(keyword => !code.includes(keyword))) {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/rules/alphabetize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { FromSchema } from 'json-schema-to-ts';
import lowerCase from 'lodash.lowercase';
import { GraphQLESTreeNode } from '../../estree-converter/index.js';
import { GraphQLESLintRule, GraphQLESLintRuleListener } from '../../types.js';
import { ARRAY_DEFAULT_OPTIONS, displayNodeName, truthy } from '../../utils.js';
import { ARRAY_DEFAULT_OPTIONS, displayNodeName } from '../../utils.js';

const RULE_ID = 'alphabetize';

Expand Down Expand Up @@ -348,7 +348,7 @@ export const rule: GraphQLESLintRule<RuleOptions> = {
Kind.INPUT_OBJECT_TYPE_EXTENSION,
],
]
.filter(truthy)
.filter(v => !!v)
.flat();

const fieldsSelector = kinds.join(',');
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin/src/rules/naming-convention/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
convertCase,
displayNodeName,
englishJoinWords,
truthy,
TYPES_KINDS,
} from '../../utils.js';

Expand Down Expand Up @@ -511,7 +510,7 @@ export const rule: GraphQLESLintRule<RuleOptions> = {
}

const selectors = new Set(
[types && TYPES_KINDS, Object.keys(restOptions)].flat().filter(truthy),
[types && TYPES_KINDS, Object.keys(restOptions)].filter(v => !!v).flat(),
);

for (const selector of selectors) {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/rules/no-root-type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NameNode } from 'graphql';
import { FromSchema } from 'json-schema-to-ts';
import { GraphQLESTreeNode } from '../../estree-converter/index.js';
import { GraphQLESLintRule } from '../../types.js';
import { ARRAY_DEFAULT_OPTIONS, requireGraphQLSchema, truthy } from '../../utils.js';
import { ARRAY_DEFAULT_OPTIONS, requireGraphQLSchema } from '../../utils.js';

const schema = {
type: 'array',
Expand Down Expand Up @@ -66,7 +66,7 @@ export const rule: GraphQLESLintRule<RuleOptions> = {
disallow.has('mutation') && schema.getMutationType(),
disallow.has('subscription') && schema.getSubscriptionType(),
]
.filter(truthy)
.filter(v => !!v)
.map(type => type.name)
.join('|');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Kind, ObjectTypeDefinitionNode } from 'graphql';
import { GraphQLESTreeNode } from '../../estree-converter/index.js';
import { GraphQLESLintRule } from '../../types.js';
import { getNodeName, requireGraphQLSchema, truthy } from '../../utils.js';
import { getNodeName, requireGraphQLSchema } from '../../utils.js';

const RULE_ID = 'require-nullable-result-in-root';

Expand Down Expand Up @@ -43,7 +43,7 @@ export const rule: GraphQLESLintRule = {
create(context) {
const schema = requireGraphQLSchema(RULE_ID, context);
const rootTypeNames = new Set(
[schema.getQueryType(), schema.getMutationType()].filter(truthy).map(type => type.name),
[schema.getQueryType(), schema.getMutationType()].filter(v => !!v).map(type => type.name),
);
const sourceCode = context.getSourceCode();

Expand Down
3 changes: 1 addition & 2 deletions packages/plugin/src/rules/strict-id-in-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
displayNodeName,
englishJoinWords,
requireGraphQLSchema,
truthy,
} from '../../utils.js';

const RULE_ID = 'strict-id-in-types';
Expand Down Expand Up @@ -137,7 +136,7 @@ export const rule: GraphQLESLintRule<RuleOptions> = {
schema.getMutationType(),
schema.getSubscriptionType(),
]
.filter(truthy)
.filter(v => !!v)
.map(type => type.name);
const selector = `ObjectTypeDefinition[name.value!=/^(${rootTypeNames.join('|')})$/]`;

Expand Down
6 changes: 0 additions & 6 deletions packages/plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,6 @@ export const ARRAY_DEFAULT_OPTIONS = {
export const englishJoinWords = (words: string[]): string =>
new Intl.ListFormat('en-US', { type: 'disjunction' }).format(words);

type Truthy<T> = T extends '' | 0 | false | null | undefined ? never : T; // from lodash

export function truthy<T>(value: T): value is Truthy<T> {
return !!value;
}

const DisplayNodeNameMap: Record<Kind, string> = {
[Kind.ARGUMENT]: 'argument',
[Kind.BOOLEAN]: 'boolean',
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"strict": true,
"lib": ["ESNext", "dom"],
"types": ["vitest/globals"],
"strictNullChecks": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"paths": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs/promises';
import path from 'node:path';
import { defineConfig, Options } from 'tsup';
import { CWD } from '@/utils.js';
import packageJson from './package.json';
import packageJson from './package.json' with { type: 'json' };

const opts: Options = {
entry: ['src/**/*.ts', '!src/index.browser.ts', '!src/**/*.test.ts'],
Expand Down
Loading
Loading