@@ -223,6 +223,81 @@ tape.test("pbjs keeps es6 as an ES module wrapper alias", function(test) {
223223 } ) ;
224224} ) ;
225225
226+ tape . test ( "pbjs escapes wrapper lint comments" , function ( test ) {
227+ cliTest ( test , function ( ) {
228+ var root = new protobuf . Root ( ) ;
229+ var staticModuleTarget = require ( "../cli/targets/static-module" ) ;
230+
231+ staticModuleTarget ( root , {
232+ wrap : "commonjs" ,
233+ lint : "note */ text"
234+ } , function ( err , jsCode ) {
235+ test . error ( err , "static-module code generation worked" ) ;
236+ test . equal ( jsCode . indexOf ( "note */ text" ) , - 1 , "does not emit raw comment terminator" ) ;
237+ test . ok ( jsCode . indexOf ( "note * / text" ) >= 0 , "escapes comment terminator" ) ;
238+ test . doesNotThrow ( function ( ) {
239+ new Function ( "require" , "module" , "exports" , jsCode ) ; // eslint-disable-line no-new-func
240+ } , "should generate parseable output" ) ;
241+ test . end ( ) ;
242+ } ) ;
243+ } ) ;
244+ } ) ;
245+
246+ tape . test ( "pbjs supports dictionary generated root names" , function ( test ) {
247+ cliTest ( test , function ( ) {
248+ var staticTarget = require ( "../cli/targets/static" ) ;
249+ var jsonModuleTarget = require ( "../cli/targets/json-module" ) ;
250+ var root = protobuf . Root . fromJSON ( {
251+ nested : {
252+ M : {
253+ fields : { }
254+ }
255+ }
256+ } ) ;
257+
258+ test . equal ( Object . getPrototypeOf ( protobuf . roots ) , null , "roots uses dictionary semantics" ) ;
259+
260+ delete protobuf . roots . __proto__ ;
261+ delete protobuf . roots . constructor ;
262+ staticTarget ( root , {
263+ root : "__proto__"
264+ } , function ( staticErr , staticCode ) {
265+ test . error ( staticErr , "static target accepts dictionary root name" ) ;
266+ test . ok ( staticCode . indexOf ( "$protobuf.roots[\"__proto__\"]" ) >= 0 , "static target uses bracket root access" ) ;
267+ var $protobuf = protobuf ;
268+ test . doesNotThrow ( function ( ) {
269+ eval ( staticCode ) ;
270+ } , "static output should execute" ) ;
271+ test . ok ( Object . prototype . hasOwnProperty . call ( protobuf . roots , "__proto__" ) , "static target creates own root property" ) ;
272+
273+ jsonModuleTarget ( root , {
274+ wrap : "commonjs" ,
275+ root : "constructor" ,
276+ lint : ""
277+ } , function ( jsonErr , jsonCode ) {
278+ test . error ( jsonErr , "json-module target accepts dictionary root name" ) ;
279+ test . ok ( jsonCode . indexOf ( "$protobuf.roots[\"constructor\"]" ) >= 0 , "json-module target uses bracket root access" ) ;
280+
281+ var module = { exports : { } } ;
282+ function localRequire ( request ) {
283+ if ( request . indexOf ( "protobufjs" ) === 0 )
284+ return protobuf ;
285+ throw Error ( "unexpected require: " + request ) ;
286+ }
287+
288+ test . doesNotThrow ( function ( ) {
289+ new Function ( "require" , "module" , "exports" , jsonCode ) ( localRequire , module , module . exports ) ; // eslint-disable-line no-new-func
290+ } , "json-module output should execute" ) ;
291+ test . ok ( Object . prototype . hasOwnProperty . call ( protobuf . roots , "constructor" ) , "json-module target creates own root property" ) ;
292+ test . equal ( module . exports , protobuf . roots . constructor , "json-module exports dictionary root" ) ;
293+ delete protobuf . roots . __proto__ ;
294+ delete protobuf . roots . constructor ;
295+ test . end ( ) ;
296+ } ) ;
297+ } ) ;
298+ } ) ;
299+ } ) ;
300+
226301tape . test ( "pbjs supports custom target paths" , function ( test ) {
227302 cliTest ( test , function ( ) {
228303 var pbjs = require ( "../cli/pbjs" ) ;
@@ -748,6 +823,83 @@ tape.test("pbjs escapes static target names", function(test) {
748823 } ) ;
749824} ) ;
750825
826+ tape . test ( "pbjs rejects static target escaped name collisions" , function ( test ) {
827+ cliTest ( test , function ( ) {
828+ var root = protobuf . Root . fromJSON ( {
829+ nested : {
830+ "pkg-name" : {
831+ nested : { }
832+ } ,
833+ pkgname : {
834+ nested : { }
835+ }
836+ }
837+ } ) ;
838+ var staticTarget = require ( "../cli/targets/static" ) ;
839+
840+ staticTarget ( root , { } , function ( err ) {
841+ test . match ( err && err . message , / d u p l i c a t e g e n e r a t e d n a m e ' p k g n a m e ' / , "rejects ambiguous generated names" ) ;
842+ test . end ( ) ;
843+ } ) ;
844+ } ) ;
845+ } ) ;
846+
847+ tape . test ( "pbjs builds static references from escaped path segments" , function ( test ) {
848+ cliTest ( test , function ( ) {
849+ var root = protobuf . Root . fromJSON ( {
850+ nested : {
851+ "pkg.name" : {
852+ nested : {
853+ Child : {
854+ fields : {
855+ value : { type : "string" , id : 1 }
856+ }
857+ } ,
858+ Kind : {
859+ values : {
860+ UNKNOWN : 0 ,
861+ READY : 1
862+ }
863+ } ,
864+ Parent : {
865+ fields : {
866+ child : { type : "Child" , id : 1 } ,
867+ kind : { type : "Kind" , id : 2 }
868+ }
869+ } ,
870+ TestService : {
871+ methods : {
872+ Call : {
873+ requestType : "Child" ,
874+ responseType : "Parent"
875+ }
876+ }
877+ }
878+ }
879+ }
880+ }
881+ } ) ;
882+ var staticTarget = require ( "../cli/targets/static" ) ;
883+
884+ staticTarget ( root , {
885+ create : true ,
886+ service : true ,
887+ verify : true ,
888+ convert : true
889+ } , function ( err , jsCode ) {
890+ test . error ( err , "static code generation worked" ) ;
891+ test . equal ( jsCode . indexOf ( "$root.pkg.name" ) , - 1 , "does not split dotted descriptor names" ) ;
892+ test . ok ( jsCode . indexOf ( "$root.pkgname.Child" ) >= 0 , "uses escaped path segment for request type" ) ;
893+ test . ok ( jsCode . indexOf ( "$root.pkgname.Parent" ) >= 0 , "uses escaped path segment for response type" ) ;
894+ test . doesNotThrow ( function ( ) {
895+ new Function ( "$protobuf" , jsCode ) ; // eslint-disable-line no-new-func
896+ } , "should generate parseable output" ) ;
897+
898+ test . end ( ) ;
899+ } ) ;
900+ } ) ;
901+ } ) ;
902+
751903tape . test ( "pbjs generates static code with message filter" , function ( test ) {
752904 cliTest ( test , function ( ) {
753905 var root = protobuf . loadSync ( "tests/data/cli/test-filter.proto" ) ;
0 commit comments