Skip to content

Commit

Permalink
Error on duplicate enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
iccir committed Mar 26, 2016
1 parent 088ac0d commit 2192096
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ build()
}

e.addValue(declaration.id.name, currentValue);
model.registerDeclaration(declaration.id.name, declaration);

declaration.enumValue = currentValue;

Expand Down
2 changes: 1 addition & 1 deletion src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ _runOnCompileCallback(onCompileCallback, ojFile, doneCallback)
},

getContents: () => {
return lines.join("\n");
return lines ? lines.join("\n") : ""
},

getPath: () => {
Expand Down
16 changes: 9 additions & 7 deletions src/model/OJModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,13 @@ getAggregateClass()
return result;
}

_registerDeclaration(name)

registerDeclaration(name, node)
{
let existing = this._declarationMap[name];

if (existing) {
Utils.throwError(OJError.DuplicateDeclaration, "Duplicate declaration of '" + name + "'.")
Utils.throwError(OJError.DuplicateDeclaration, "Duplicate declaration of '" + name + "'.", node)
}

this._declarationMap[name] = true;
Expand All @@ -400,7 +401,7 @@ addConst(ojConst)
let name = ojConst.name;

this.consts[name] = ojConst;
this._registerDeclaration(name);
this.registerDeclaration(name);
}


Expand All @@ -421,7 +422,8 @@ addEnum(ojEnum)
}

this.enums[name] = ojEnum;
this._registerDeclaration(name);

this.registerDeclaration(name);
}


Expand All @@ -448,7 +450,7 @@ addClass(ojClass)

} else {
this.classes[name] = ojClass;
this._registerDeclaration(name);
this.registerDeclaration(name);
}
}

Expand All @@ -474,7 +476,7 @@ addType(ojType)
}

this.types[name] = ojType;
this._registerDeclaration(name);
this.registerDeclaration(name);
}


Expand All @@ -483,7 +485,7 @@ addGlobal(ojGlobal)
let name = ojGlobal.name;

this.globals[name] = ojGlobal;
this._registerDeclaration(name);
this.registerDeclaration(name);
}


Expand Down
1 change: 1 addition & 0 deletions src/typechecker/Typechecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ check(model, defs, files, callback)
let defsKey = ojFile.path + path.sep + defsSuffix;

sourceFileMap[defsKey] = this._getSourceFile(defsKey, ojFile.contents);
originalFileMap[defsKey] = ojFile.path;
});

if (!this._globalDefsSourceFile) {
Expand Down
2 changes: 1 addition & 1 deletion test/issues/TestIssue112.oj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

function runTests()
{
return [Foo runTest] == 5
return [TestIssue112 runTest] == 5
}

runTests();
22 changes: 20 additions & 2 deletions test/multi/TestErrors.oj
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,31 @@ function test() {
@end



// --------------------------------------------------------------------
// @name Duplicate Enum
// @name Duplicate Enum Name

@enum Foo { Foo1, Foo2 }
@enum Foo { Bar1, Bar2 } //@error OJDuplicateDeclarationError


// --------------------------------------------------------------------
// @name Duplicate Enum Value 1

@enum Foo { Foo1, Foo2 }
@enum Bar { Foo1, Foo2 } //@error OJDuplicateDeclarationError


// --------------------------------------------------------------------
// @name Duplicate Enum Value 2

@enum { Foo1, FooDuplicate }

@enum {
Foo2,
FooDuplicate //@error OJDuplicateDeclarationError
}


// --------------------------------------------------------------------
// @name Circular Class 1
// @error-no-line OJCircularClassHierarchyError
Expand Down

0 comments on commit 2192096

Please sign in to comment.