Skip to content

Commit

Permalink
perf(core): remove usage of arrays while forming duplicate item errors (
Browse files Browse the repository at this point in the history
accordproject#804)

* refactor: don't use arrays to check uniqueness

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* refactor: also refactor unique property name check

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* refactor: remove array for decorator uniqueness check

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* refactor: remove uniqueness check from scalar declarations as well

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

---------

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>
  • Loading branch information
ekarademir authored and mttrbrts committed Mar 10, 2024
1 parent 475de52 commit 5489600
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
31 changes: 14 additions & 17 deletions packages/concerto-core/src/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,24 +274,21 @@ class ClassDeclaration extends Declaration {
}
// we also have to check fields defined in super classes
const properties = this.getProperties();
const propertyFieldNames = properties.map(
d => d.getName()
);
const uniquePropertyFieldNames = new Set(propertyFieldNames);

if (uniquePropertyFieldNames.size !== properties.length) {
const duplicateElements = propertyFieldNames
.filter(
(item, index) => propertyFieldNames.indexOf(item) !== index
const uniquePropertyNames = new Set();
properties.forEach(p => {
const propertyName = p.getName();
if (!uniquePropertyNames.has(propertyName)) {
uniquePropertyNames.add(propertyName);
} else {
const formatter = Globalize('en').messageFormatter(
'classdeclaration-validate-duplicatefieldname'
);
const formatter = Globalize('en').messageFormatter(
'classdeclaration-validate-duplicatefieldname'
);
throw new IllegalModelException(formatter({
'class': this.name,
'fieldName': duplicateElements[0]
}), this.modelFile, this.ast.location);
}
throw new IllegalModelException(formatter({
'class': this.name,
'fieldName': propertyName
}), this.modelFile, this.ast.location);
}
});

for (let n = 0; n < properties.length; n++) {
let field = properties[n];
Expand Down
29 changes: 13 additions & 16 deletions packages/concerto-core/src/introspect/decorated.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,20 @@ class Decorated extends ModelElement {
}

// check we don't have this decorator twice
const decoratorNames = this.decorators.map(
d => d.getName()
);
const uniqueDecoratorNames = new Set(decoratorNames);

if (uniqueDecoratorNames.size !== this.decorators.length) {
const duplicateElements = decoratorNames
.filter(
(item, index) => decoratorNames.indexOf(item) !== index
const uniqueDecoratorNames = new Set();
this.decorators.forEach(d => {
const decoratorName = d.getName();
if(!uniqueDecoratorNames.has(decoratorName)) {
uniqueDecoratorNames.add(decoratorName);
} else {
const modelFile = this.getModelFile();
throw new IllegalModelException(
`Duplicate decorator ${decoratorName}`,
modelFile,
this.ast.location,
);
const modelFile = this.getModelFile();
throw new IllegalModelException(
`Duplicate decorator ${duplicateElements[0]}`,
modelFile,
this.ast.location,
);
}
}
});
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/concerto-core/test/introspect/modelfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ describe('ModelFile', () => {

describe('#validate', () => {

it('should throw when scalar name is duplicted in a modelfile', () => {
let asset = introspectUtils.loadModelFile('test/data/parser/scalardeclaration.dupeboolean.cto');
(() => {
asset.validate();
}).should.throw(/Duplicate class/);
});

it('should throw when asset name is duplicted in a modelfile', () => {
let asset = introspectUtils.loadModelFile('test/data/parser/classdeclaration.dupeassetname.cto');
(() => {
Expand Down

0 comments on commit 5489600

Please sign in to comment.