Advice on plugin to decorate identifiers

First, thank you for acorn. I was a financial supporter for many years, and finally found some time to advance a side project.

I am looking to add syntax to identifiers used in template literal quasi. For example:

`Hello ${@foo}, the result is ${3 + @value}`

My first goal is to recognize the identifiers, then limit to the quasi context. My initial naive approach is to augment the identifier check to include the “@” as an initial character and add the information to the node:

Node {
    "name": "foo",
    "type": "Identifier",
    "observable": true 
  },

But the isIdentifierStart and isIdentiferChar functions are not part of the Parser prototype methods. I certainly could override the limited number of methods that use these functions, but I’m wondering if I am fighting the parser and maybe I should add a new node type, yielding something more like:

Node {
    "expression": Node {
        "name": "foo",
        "type": "Identifier",
    },
    "type": "DecoratedIdentifier",
}

Any insights and advice appreciated!

Wrapping the identifiers in a bigger node is probably easier than changing the tokenizer. You may be able to check for template literal context via something like this.context.includes(acorn.tokContexts.q_tmpl), though I’m not sure what your precise rules are for how deep in nested expressions such identifiers may occur.

Thanks marijn, that approach for context looks spot on.

Any tips on which method to override to create that outer node? It would only be identifiers used as expressions, never definitions.

Maybe parseExprAtom.

After some studious debugging and insights from acorn-private-class-elements and acorn-jsx, I seem to have a basic solution for recognizing identifiers prefixed with @.

I ended up modifying the type of the Identifier node by using a custom token type and context. I still have more test cases to explore, so I’ll see how the solution holds up…