Skip to content

Commit

Permalink
Fix #161: naming collisions with message named Object
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm committed Sep 19, 2021
1 parent 2803503 commit 297ac41
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### unreleased changes

Bug fixes:

- Naming collisions with message named Object #161


### v2.0.5

Expand Down
5 changes: 4 additions & 1 deletion packages/plugin/src/code-gen/local-type-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import {AnyTypeDescriptorProto, FileDescriptorProto, IDescriptorTree} from "@pro
import {assert} from "@protobuf-ts/runtime";


// TODO in generated code, use globalThis for all types in global scope
// rationale: keywords are much less likely to change than objects in the global scope
// TODO move all code creating names into one place (see interpreter.ts for more code)
const reservedKeywords = 'break,case,catch,class,const,continue,debugger,default,delete,do,else,enum,export,extends,false,finally,for,function,if,import,in,instanceof,new,null,return,super,switch,this,throw,true,try,typeof,var,void,while,with,as,implements,interface,let,package,private,protected,public,static,yield,any,boolean,constructor,declare,get,module,require,number,set,string,symbol,type,from,of'.split(',');
const reservedTypeNames = 'Long,Uint8Array,array,Array,string,String,number,Number,boolean,Boolean,bigint,BigInt'.split(',');
const reservedTypeNames = 'object,Uint8Array,array,Array,string,String,number,Number,boolean,Boolean,bigint,BigInt'.split(',');
const escapeCharacter = '$';


Expand Down
5 changes: 4 additions & 1 deletion packages/plugin/src/message-type-extensions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ export class Create implements CustomMethodGenerator {

return ts.createExpressionStatement(ts.createCall(
ts.createPropertyAccess(
ts.createIdentifier("Object"),
ts.createPropertyAccess(
ts.createIdentifier("globalThis"),
ts.createIdentifier("Object")
),
ts.createIdentifier("defineProperty")
),
undefined,
Expand Down
38 changes: 38 additions & 0 deletions packages/test-fixtures/name-clash.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,44 @@ message Error {
string field_name = 1;
string message = 2;
}
// clash with global type
message Object {
}
message object {
}
// clash with global type
message Uint8Array {
}
// clash with global type
message array {
}
// clash with global type
message Array {
}
// clash with global type
message string {
}
// clash with global type
message String {
}
// clash with global type
message number {
}
// clash with global type
message Number {
}
// clash with global type
message boolean {
}
// clash with global type
message Boolean {
}
// clash with global type
message bigint {
}
// clash with global type
message BigInt {
}


message ClashParent {
Expand Down

0 comments on commit 297ac41

Please sign in to comment.