Skip to content

Commit

Permalink
small touchup
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-zhang-at-salesforce committed May 28, 2024
1 parent c2a39fc commit 4400982
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
Binary file removed salesforce-eslint-plugin-lwc-mobile-0.0.1.tgz
Binary file not shown.
20 changes: 13 additions & 7 deletions src/rules/graphql/EntityStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const MAX_ROOT_ENTITY_COUNT_COUNT = 3;
const MAX_CHILD_ENTITY_TYPE_COUNT = 3;

type EntityName = string;
type Parent = EntityName | undefined;
type ParentEntityName = EntityName | undefined;
type OperationId = number;

export enum ViolationType {
Expand All @@ -40,12 +40,12 @@ interface Violation {
export type ViolationListener = (violation: Violation) => void;

/*
*
* A data structure to track query entity, page size and child entities.
*/
export class EntityStat {
name: EntityName; // entity/object name
pageSize: number; // the value of 'first' argument from graph entity query
node: GraphQLESTreeNode<FieldNode>; //
node: GraphQLESTreeNode<FieldNode>; // the entity estree node
childrenEntities: Array<EntityStat>; // children entity query

constructor(name: string, pageSize: number, node: GraphQLESTreeNode<FieldNode>) {
Expand Down Expand Up @@ -85,13 +85,17 @@ export class DocumentStat {
}
}

addEntityStat(entityStat: EntityStat, operationId: OperationId, parent: Parent = undefined) {
addEntityStat(
entityStat: EntityStat,
operationId: OperationId,
parentEntityName: ParentEntityName = undefined
) {
if (this.entityStats[operationId] === undefined) {
this.entityStats[operationId] = [];
}
const operationEntityStats = this.entityStats[operationId];

if (parent === undefined) {
if (parentEntityName === undefined) {
// add new root entity and check if trigger violation
operationEntityStats.push(entityStat);
if (operationEntityStats.length > MAX_ROOT_ENTITY_COUNT_COUNT) {
Expand All @@ -104,11 +108,13 @@ export class DocumentStat {
} else {
const parentEntityStat = operationEntityStats
.filter((entityStat) => {
return entityStat.name === parent;
return entityStat.name === parentEntityName;
})
.pop();
if (parentEntityStat === undefined) {
throw new Error(`the parent entity stat is not found for specified ${parent}`);
throw new Error(
`the parent entity stat is not found for specified ${parentEntityName}`
);
}

// raise violation if parent entity pagesize is over the max allowed
Expand Down
2 changes: 1 addition & 1 deletion src/rules/graphql/no-more-than-1-parent-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const rule: GraphQLESLintRule = {
if (!reportedViolations.includes(key)) {
reportedViolations.push(key);
context.report({
node: violation.violator.node,
node: violation.violator.node.name,
messageId: NO_MORE_THAN_1_PARENT_RECORD_RULE_ID,
data: {
pageSize: violation.violator.pageSize.toString()
Expand Down
2 changes: 1 addition & 1 deletion src/rules/graphql/no-more-than-3-child-entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const rule: GraphQLESLintRule = {
const documentStat = new DocumentStat((violation) => {
if (violation.type === ViolationType.MAX_CHILD_ENTITY_COUNT) {
context.report({
node: violation.violator.node,
node: violation.violator.node.name,
messageId: NO_MORE_THAN_3_CHILD_ENTITIES_RULE_ID
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/graphql/no-more-than-3-root-entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const rule: GraphQLESLintRule = {
const documentStat = new DocumentStat((violation) => {
if (violation.type === ViolationType.MAX_ROOT_ENTITY_COUNT) {
context.report({
node: violation.violator.node,
node: violation.violator.node.name,
messageId: NO_MORE_THAN_3_ROOT_ENTITIES_RULE_ID
});
}
Expand Down

0 comments on commit 4400982

Please sign in to comment.