-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from salesforce/lintRuleUnsupportedDateFilter
Lint rule unsupported date filter
- Loading branch information
Showing
5 changed files
with
256 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
src/rules/graphql/no-fiscal-date-filtering-supported.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { Kind } from 'graphql'; | ||
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '@graphql-eslint/eslint-plugin'; | ||
import getDocUrl from '../../util/getDocUrl'; | ||
export const NO_FISCAL_DATE_FILTER_SUPPORTED_RULE_ID = | ||
'offline-graphql-no-fiscal-date-filter-supported'; | ||
|
||
type NodeWithParent = { | ||
kind: string; | ||
parent: NodeWithParent; | ||
}; | ||
|
||
export const rule: GraphQLESLintRule = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'fiscal date literals/ranges are not supported offline', | ||
category: 'Operations', | ||
url: getDocUrl(NO_FISCAL_DATE_FILTER_SUPPORTED_RULE_ID), | ||
recommended: true, | ||
examples: [ | ||
{ | ||
title: 'Correct', | ||
code: /* GraphQL */ ` | ||
{ | ||
uiapi { | ||
query { | ||
Account( | ||
where: { | ||
LastActivityDate: { | ||
eq: { literal: { THIS_YEAR } } | ||
} | ||
} | ||
) { | ||
edges { | ||
node { | ||
Id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
}, | ||
{ | ||
title: 'InCorrect', | ||
code: /* GraphQL */ ` | ||
{ | ||
uiapi { | ||
query { | ||
Account( | ||
where: { | ||
LastActivityDate: { eq: { literal: THIS_FISCAL_YEAR } } | ||
} | ||
) { | ||
edges { | ||
node { | ||
Id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
}, | ||
{ | ||
title: 'InCorrect', | ||
code: /* GraphQL */ ` | ||
{ | ||
uiapi { | ||
query { | ||
Account( | ||
where: { | ||
LastActivityDate: { | ||
eq: { range: { last_n_fiscal_years: 1 } } | ||
} | ||
} | ||
) { | ||
edges { | ||
node { | ||
Id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
} | ||
] | ||
}, | ||
messages: { | ||
[NO_FISCAL_DATE_FILTER_SUPPORTED_RULE_ID]: | ||
'Offline GraphQL: Date {{filterType}} "{{filterName}}" is not supported offline.' | ||
}, | ||
schema: [] | ||
}, | ||
create(context: GraphQLESLintRuleContext) { | ||
return { | ||
ObjectField(node) { | ||
if (node.kind == Kind.OBJECT_FIELD && node.name.kind === Kind.NAME) { | ||
// Handles literal. Example: where : {LastAcitivtyDate: {eq: {literal : LAST_FISCAL_YEAR }}}. | ||
if ( | ||
node.name.value === 'literal' && | ||
node.value.kind === Kind.ENUM && | ||
node.value.value.indexOf('_FISCAL_') > 0 && | ||
isInFilter(node as NodeWithParent) | ||
) { | ||
context.report({ | ||
messageId: NO_FISCAL_DATE_FILTER_SUPPORTED_RULE_ID, | ||
node: node.value, | ||
data: { | ||
filterType: 'literal', | ||
filterName: node.value.value | ||
} | ||
}); | ||
return; | ||
} | ||
//Handles ranges. Example: where : {LastAcitivtyDate: {eq: {range: {last_n_fiscal_years: 1 }}}}. | ||
if ( | ||
node.name.value === 'range' && | ||
node.value.kind === Kind.OBJECT && | ||
node.value.fields.length > 0 | ||
) { | ||
const rangeObjectField = node.value.fields[0]; | ||
// Checks if it is a fiscal date filter, for example 'last_n_fiscal_quarters', 'n_fiscal_years_ago'. | ||
if ( | ||
rangeObjectField.name.value.indexOf('_fiscal_') > 0 && | ||
isInFilter(rangeObjectField as NodeWithParent) | ||
) { | ||
context.report({ | ||
messageId: NO_FISCAL_DATE_FILTER_SUPPORTED_RULE_ID, | ||
node: rangeObjectField, | ||
data: { | ||
filterType: 'range', | ||
filterName: rangeObjectField.name.value | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
function isInFilter(node: NodeWithParent): boolean { | ||
if (node.kind === Kind.ARGUMENT) { | ||
return true; | ||
} | ||
return node.parent === null ? false : isInFilter(node.parent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
test/rules/graphql/no-fiscal-date-filtering-supported.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
rule, | ||
NO_FISCAL_DATE_FILTER_SUPPORTED_RULE_ID | ||
} from '../../../src/rules/graphql/no-fiscal-date-filtering-supported'; | ||
|
||
import { ruleTester } from '../../shared'; | ||
|
||
ruleTester.run('@salesforce/lwc-mobile/no-fiscal-date-filtering-supported', rule as any, { | ||
valid: [ | ||
{ | ||
code: /* GraphQL */ ` | ||
{ | ||
uiapi { | ||
query { | ||
Account(where: { LastActivityDate: { eq: { literal: THIS_YEAR } } }) { | ||
edges { | ||
node { | ||
Id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: /* GraphQL */ ` | ||
{ | ||
uiapi { | ||
query { | ||
Account( | ||
where: { | ||
LastActivityDate: { eq: { range: { last_n_fiscal_years: 1 } } } | ||
} | ||
) { | ||
edges { | ||
node { | ||
Id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: NO_FISCAL_DATE_FILTER_SUPPORTED_RULE_ID, | ||
data: { | ||
filterType: 'range', | ||
filterName: 'last_n_fiscal_years' | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
code: /* GraphQL */ ` | ||
{ | ||
uiapi { | ||
query { | ||
Account( | ||
where: { LastActivityDate: { eq: { literal: THIS_FISCAL_YEAR } } } | ||
) { | ||
edges { | ||
node { | ||
Id | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: NO_FISCAL_DATE_FILTER_SUPPORTED_RULE_ID, | ||
data: { | ||
filterType: 'literal', | ||
filterName: 'THIS_FISCAL_YEAR' | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}); |