Skip to content

Commit

Permalink
Unicity error messages (#235)
Browse files Browse the repository at this point in the history
* added better error information for unicity verifiers
  • Loading branch information
lfportal authored May 15, 2019
1 parent ebaa534 commit da7b16c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/src/verifiers/unicity/endpoints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("unique endpoint names verifier", () => {
};
expect(verifyUniqueEndpointNames(contractNode)).toMatchObject([
{
message: "endpoints must have unique names"
message: "endpoints must have unique names: EndpointOne"
}
]);
});
Expand Down
25 changes: 17 additions & 8 deletions lib/src/verifiers/unicity/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { uniq } from "lodash";
import { groupBy, keys, pickBy } from "lodash";
import { ContractNode } from "../../models/nodes";
import { VerificationError } from "../verification-error";

export function verifyUniqueEndpointNames(
contract: ContractNode
): VerificationError[] {
const errors: VerificationError[] = [];
const endpointNames = contract.endpoints.map(

const endpointsByName = groupBy(
contract.endpoints,
endpoint => endpoint.value.name.value
);
if (uniq(endpointNames).length !== endpointNames.length) {

const duplicatedEndpoints = pickBy(
endpointsByName,
endpointCollection => endpointCollection.length > 1
);

keys(duplicatedEndpoints).forEach(endpointName => {
const firstEndpointWithName = duplicatedEndpoints[endpointName][0];
errors.push({
message: "endpoints must have unique names",
// TODO: use a duplicated endpoint location
location: contract.api.location,
line: contract.api.line
message: `endpoints must have unique names: ${endpointName}`,
location: firstEndpointWithName.value.name.location,
line: firstEndpointWithName.value.name.line
});
}
});

return errors;
}
2 changes: 1 addition & 1 deletion lib/src/verifiers/unicity/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("unique type names verifier", () => {
};
expect(verifyUniqueTypeNames(contractNode)).toMatchObject([
{
message: "types must have unique names"
message: "types must have unique names: TypeOne"
}
]);
});
Expand Down
17 changes: 12 additions & 5 deletions lib/src/verifiers/unicity/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { uniq } from "lodash";
import { groupBy, keys, pickBy } from "lodash";
import { ContractNode } from "../../models/nodes";
import { VerificationError } from "../verification-error";

export function verifyUniqueTypeNames(
contract: ContractNode
): VerificationError[] {
const errors: VerificationError[] = [];
const typeNames = contract.types.map(type => type.name);
if (uniq(typeNames).length !== typeNames.length) {

const typesByName = groupBy(contract.types, type => type.name);
const duplicatedTypes = pickBy(
typesByName,
typeCollection => typeCollection.length > 1
);

keys(duplicatedTypes).forEach(typeName => {
errors.push({
message: "types must have unique names",
message: `types must have unique names: ${typeName}`,
// TODO: use a duplicated type location
location: contract.api.location,
line: contract.api.line
});
}
});

return errors;
}

0 comments on commit da7b16c

Please sign in to comment.