From c8290d98d45699965148fbae9688db4017943096 Mon Sep 17 00:00:00 2001 From: Ben Zhang Date: Tue, 21 May 2024 17:08:23 -0700 Subject: [PATCH] reuse docUrl for all rules --- jest.config.ts | 2 +- src/rules/graphql/no-aggregate-query-supported.ts | 3 ++- src/rules/graphql/no-mutation-supported.ts | 7 +++++-- src/util/createRule.ts | 6 ++---- src/util/getDocUrl.ts | 12 ++++++++++++ src/{utils.ts => util/graphqlAstUtils.ts} | 0 test/util/getDocUrl.spec.ts | 14 ++++++++++++++ .../utils.spec.ts => util/graphqlAstUtils.spec.ts} | 2 +- 8 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 src/util/getDocUrl.ts rename src/{utils.ts => util/graphqlAstUtils.ts} (100%) create mode 100644 test/util/getDocUrl.spec.ts rename test/{rules/utils.spec.ts => util/graphqlAstUtils.spec.ts} (88%) diff --git a/jest.config.ts b/jest.config.ts index eef3027..895dd9f 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -11,7 +11,7 @@ const config: Config = { displayName: 'Unit Tests', setupFilesAfterEnv: ['jest-extended', 'jest-chain'], preset: 'ts-jest', - testMatch: ['/test/rules/**/*.ts'], + testMatch: ['/test/**/*.ts'], moduleFileExtensions: ['ts', 'js', 'json'], testResultsProcessor: 'jest-sonar-reporter', testPathIgnorePatterns: ['/node_modules/', '/lib/'], diff --git a/src/rules/graphql/no-aggregate-query-supported.ts b/src/rules/graphql/no-aggregate-query-supported.ts index 3988033..fcdeaf2 100644 --- a/src/rules/graphql/no-aggregate-query-supported.ts +++ b/src/rules/graphql/no-aggregate-query-supported.ts @@ -1,5 +1,6 @@ import { FieldNode } from 'graphql'; import { GraphQLESLintRule, GraphQLESLintRuleContext } from '@graphql-eslint/eslint-plugin'; +import getDocUrl from '../../util/getDocUrl'; export const NO_AGGREGATE_QUERY_SUPPORTED_RULE_ID = 'offline-graphql-no-aggregate-query-supported'; @@ -11,7 +12,7 @@ export const rule: GraphQLESLintRule = { category: 'Operations', description: 'aggregate operation in graphql query is not supported for mobile offline.', - // url: + url: getDocUrl(NO_AGGREGATE_QUERY_SUPPORTED_RULE_ID), examples: [ { title: 'Incorrect', diff --git a/src/rules/graphql/no-mutation-supported.ts b/src/rules/graphql/no-mutation-supported.ts index a8b3e70..3428b6a 100644 --- a/src/rules/graphql/no-mutation-supported.ts +++ b/src/rules/graphql/no-mutation-supported.ts @@ -6,7 +6,9 @@ */ import { GraphQLESLintRule, GraphQLESLintRuleContext } from '@graphql-eslint/eslint-plugin'; -import { getLocation } from '../../utils'; +import { getLocation } from '../../util/graphqlAstUtils'; +import getDocUrl from '../../util/getDocUrl'; + export const NO_MUTATION_SUPPORTED_RULE_ID = 'offline-graphql-no-mutation-supported'; export const rule: GraphQLESLintRule = { @@ -14,8 +16,9 @@ export const rule: GraphQLESLintRule = { type: 'problem', hasSuggestions: false, docs: { - description: 'mutation is not supported offline', category: 'Operations', + description: 'mutation is not supported offline', + url: getDocUrl(NO_MUTATION_SUPPORTED_RULE_ID), recommended: true, examples: [ { diff --git a/src/util/createRule.ts b/src/util/createRule.ts index 4175506..00254af 100644 --- a/src/util/createRule.ts +++ b/src/util/createRule.ts @@ -6,8 +6,6 @@ */ import { ESLintUtils } from '@typescript-eslint/utils'; -import { version, homepage } from '../../package.json'; +import getDocUrl from './getDocUrl'; -export default ESLintUtils.RuleCreator( - (name) => `${homepage}/blob/v${version}/lib/docs/${name}.md` -); +export default ESLintUtils.RuleCreator((name) => getDocUrl(name)); diff --git a/src/util/getDocUrl.ts b/src/util/getDocUrl.ts new file mode 100644 index 0000000..c6f0f42 --- /dev/null +++ b/src/util/getDocUrl.ts @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2024, salesforce.com, inc. + * All rights reserved. + * SPDX-License-Identifier: MIT + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT + */ + +import { version, homepage } from '../../package.json'; + +export default function getDocUrl(ruleName: string): string { + return `${homepage}/blob/v${version}/lib/docs/${ruleName}.md`; +} diff --git a/src/utils.ts b/src/util/graphqlAstUtils.ts similarity index 100% rename from src/utils.ts rename to src/util/graphqlAstUtils.ts diff --git a/test/util/getDocUrl.spec.ts b/test/util/getDocUrl.spec.ts new file mode 100644 index 0000000..e31f63e --- /dev/null +++ b/test/util/getDocUrl.spec.ts @@ -0,0 +1,14 @@ +import { describe } from 'node:test'; + +import { expect } from '@jest/globals'; +import getDocUrl from '../../src/util/getDocUrl'; + +describe('getDocUrl', () => { + it('doc url returned should be right value', () => { + const docUrl = getDocUrl('ruleNameXYZ'); + expect( + docUrl.startsWith('https://github.com/salesforce/eslint-plugin-lwc-mobile/blob/v') + ).toBe(true); + expect(docUrl.endsWith('/lib/docs/ruleNameXYZ.md')).toBe(true); + }); +}); diff --git a/test/rules/utils.spec.ts b/test/util/graphqlAstUtils.spec.ts similarity index 88% rename from test/rules/utils.spec.ts rename to test/util/graphqlAstUtils.spec.ts index b74a05b..8aeb4f1 100644 --- a/test/rules/utils.spec.ts +++ b/test/util/graphqlAstUtils.spec.ts @@ -1,5 +1,5 @@ import { describe } from 'node:test'; -import { getLocation } from '../../src/utils'; +import { getLocation } from '../../src/util/graphqlAstUtils'; import { Position } from 'estree'; import { expect } from '@jest/globals';