protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

fix code gen error, if proto pacakge name contains javascript reserved word

Open jation opened this issue 3 years ago • 3 comments

fix codegen error, if proto pacakge name contains javascript reserved word, codegen will gen error code in static/static-module target

e.g.

test.proto

package function.b; // `function` is a reserved word
message Msg {
  optional int32 a = 1;
}

use pbjs -t static-module -w es6 -o test.js test.proto

incorrect result test.js

/// ...part of code
export const function_ = $root.function_ = (() => { // correct

/// ...part of code
Msg.decode = function decode(reader, length) {
    if (!(reader instanceof $Reader))
        reader = $Reader.create(reader);
    let end = length === undefined ? reader.len : reader.pos + length, message = new $root.function.b.Msg(); // incorrect

/// ...part of code
Msg.fromObject = function fromObject(object) {
    if (object instanceof $root.function.b.Msg) //  incorrect

--> incorrect code $root.function.b.Msg() --> correct code is $root.function_.b.Msg()

correct result after fix

/// ...part of code
export const function_ = $root.function_ = (() => { // correct

/// ...part of code
Msg.decode = function decode(reader, length) {
    if (!(reader instanceof $Reader))
        reader = $Reader.create(reader);
    let end = length === undefined ? reader.len : reader.pos + length, message = new $root.function_.b.Msg(); // correct

/// ...part of code
Msg.fromObject = function fromObject(object) {
    if (object instanceof $root.function_.b.Msg) //  correct

jation avatar Aug 18 '21 19:08 jation

Hi @alexander-fenster please merge this pr.

jation avatar Apr 25 '22 08:04 jation

@jation Sorry, I don't have contributor access to this repo, please ask @dcodeIO for help with this.

alexander-fenster avatar May 03 '22 17:05 alexander-fenster

Hi @dcodeIO , can you please merge this PR?

jiashenghuang-ponyai avatar Sep 07 '22 02:09 jiashenghuang-ponyai