josephtzeng / hocon-parser

A HOCON Parser for Node.js
ISC License
31 stars 10 forks source link

Array's closeToken is not `]` #13

Closed enykeev closed 3 years ago

enykeev commented 3 years ago

Firstly, thanks for your work on this project!

Just want to let you know that I had to patch @pushcorn/hocon-parser@1.1.15 for the project I'm working on.

I'm doing some non-destructive programmatic editing of hocon files using AST from Context object and while doing so I noticed that closeToken of an ArrayNode is not ], but its last value. It does not seem to be intentional:

diff --git a/node_modules/@pushcorn/hocon-parser/lib/nodes/ArrayNode.js b/node_modules/@pushcorn/hocon-parser/lib/nodes/ArrayNode.js
index 39b2371..4001835 100644
--- a/node_modules/@pushcorn/hocon-parser/lib/nodes/ArrayNode.js
+++ b/node_modules/@pushcorn/hocon-parser/lib/nodes/ArrayNode.js
@@ -77,7 +77,7 @@ class ArrayNode extends Node
     {
         if (!(node instanceof ArrayNode) && node.lastToken.type == Token.TYPE.CLOSE_SQUARE)
         {
-            return this.close ();
+            return this.close (node.closeToken);
         }
     }

While at it, I figured it might be useful to add references to previous and next tokens during build to get access to tokens that are otherwise discarded (comments, whitespaces):

diff --git a/node_modules/@pushcorn/hocon-parser/lib/builders/ConfigBuilder.js b/node_modules/@pushcorn/hocon-parser/lib/builders/ConfigBuilder.js
index ae325db..263f500 100644
--- a/node_modules/@pushcorn/hocon-parser/lib/builders/ConfigBuilder.js
+++ b/node_modules/@pushcorn/hocon-parser/lib/builders/ConfigBuilder.js
@@ -12,6 +12,15 @@ class ConfigBuilder extends BuilderAdapter
     {
         let { rootType, tokens } = this.tokenize (data);

+        for (const i in tokens) {
+            if (tokens[i + 1]) {
+                tokens[i + 1].previousToken = tokens[i];
+            }
+            if (tokens[i - 1]) {
+                tokens[i - 1].nextToken = tokens[i]
+            }
+        }
+
         this.context.createNode (rootType).append (...tokens);
     }

I'm not sure if you want to add any of that to your code base. I myself are perfectly content using locally patched version. Still, I figured I'd let you know of my findings.

This issue body was partially generated by patch-package.

josephtzeng commented 3 years ago

Hi Kirill,

Thanks for the patch. I moved the token linking code to the Tokenizer.tokenize() method.

Best, Joseph