Closure compiler doesn't treat "id" as a reserved word, leading to exceptions

23 views
Skip to first unread message

Erik Cota-Robles

unread,
Mar 16, 2021, 11:37:44 AM3/16/21
to Closure Compiler Discuss
"id" is a property of elements received in event listener functions.  I'm seeing sporadic exceptions caused by the "id" field for some events gets conflated with an "id()" function generated by the minified code.  The exceptions are TypeError exceptions about charCodeAt not being a function of event.id where the "id" property of the event is a string.

I've noticed that the closure compiler avoids renaming well known object properties such as ".candidate" for WebRTC ICE negotiation messages (https://developer.mozilla.org/en-US/docs/Web/API/RTCIceCandidate/candidate).  In our implementation we also have a ".token" field in our WebRTC ICE candidate messages and I had to take care to always refer to it as message['token'] whereas message.candidate was unaffected by minification.  "message" itself is of course minified.  So I believe the fix could be as simple as adding "id" (or perhaps "[^a-zA-Z0-9]id[^a-zA-Z0-9]") to the list of reserved identifiers.

Grepping through the minified file I see instances of ".id" and "id(".  When I renamed the function "id(" to "idid(" the exceptions were no longer generated.  I have worked around this by adding a post build step to patch the minified file with sed thusly:

sed -i 's/id(/idid(/g' <minified-file.js>

Thanks.

Bradford Smith

unread,
Mar 16, 2021, 11:42:04 AM3/16/21
to Closure Compiler Discuss
I think the answer to your problem is almost certainly adding externs files to your build.


HTH,
Bradford

Reply all
Reply to author
Forward
0 new messages