0

A brief background, I use closure compiler in a project and and trying to exclude some properties of a object from being renamed. I used externs along with type based optimization to exclude properties only from a particular object from being renamed. However, closure compiler then excludes all the properties with same name in all the objects, regardless of type annotation. Related discussions are here and here.

Hence I decide to manually convert dot notation to array notation in Javascript files just before compilation. I can still use dot notation while developing but convert before feeding to CC, like

this.myobject.test to this.myobject['test']

this.myobject.swift.cc to this.myobject['swift']['cc']

I am using sed. I could do it for one dot. However, I need help with multiple dots. Can someone help me with regular expression to convert dot notations up to depth level 4 to array notation.

Jim
  • 338
  • 1
  • 14
  • Is this [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? It could be that there is a configuration for the compiler that can satisfy your needs. Or maybe something else that can be done. If automated transformation is needed, then a proper AST parser can probably help a lot more than simply sed. You'd be able to read the actual structure of the file, not arbitrary matching strings where the property access `a.b.c.d` is the same as a selector `document.querySelector(".a.b.b.d")` and you need to invest useless time and effort to differentiate them. – VLAZ Aug 19 '22 at 09:12
  • This answer to a very similar question covers a basic approach: https://stackoverflow.com/a/51411046/1650337 Though it won't cover more complex formatting that well e.g. multi-line chaining – DBS Aug 19 '22 at 09:14
  • @DBS, thanks. I saw that before posting. But I need help with multi-level dots. – Jim Aug 19 '22 at 09:17
  • @VLAZ, thanks but please read the links I posted - especially the second link and answer by Stephen Chung - compiler can not do it and hence we have resorted to this approach. Also, document.querySelector will not match "this.myobject" hence arbitrary matching should do to begin with. Now can you help please? – Jim Aug 19 '22 at 09:29
  • @Jim you still went with regex to change code, which cannot work in any general case. I showed you one. There are *easily* many, many others: having new lines between the property access, having something with dots in a different context would all be hairy and very error-prone to handle with regex. And any regex that properly handles them would be a monstrosity. It's a Y solution to your problem. My suggestion is to use a *good* solution to X instead of throwing regex at it. – VLAZ Aug 19 '22 at 09:34
  • @VLAZ, I am not denying but it will give me a start and carefully chosen object name will mitigate at large extent. Can you help me with regex. – Jim Aug 19 '22 at 09:37

0 Answers0