marked icon indicating copy to clipboard operation
marked copied to clipboard

Rules on lexers should be deep copied

Open kramerc opened this issue 9 years ago • 5 comments

If I change a rule on one lexer, that change ends up getting applied to other lexers as observed with the following:

> var lexer = new marked.Lexer();
undefined
> lexer.rules.heading
/^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/
> var newLexer = new marked.Lexer();
undefined
> newLexer.rules.heading
/^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/
> lexer.rules.heading = /^ *(#{1,6} ) +([^\n]+?) *#* *(?:\n+|$)/
/^ *(#{1,6} ) +([^\n]+?) *#* *(?:\n+|$)/
> lexer.rules.heading
/^ *(#{1,6} ) +([^\n]+?) *#* *(?:\n+|$)/
> newLexer.rules.heading
/^ *(#{1,6} ) +([^\n]+?) *#* *(?:\n+|$)/

The change should have only been applied to lexer and not newLexer. This happens because the rules are not deep copied when a new lexer is constructed.

kramerc avatar May 01 '16 16:05 kramerc

Got exactly the same problem.

Yimiprod avatar Jun 01 '16 15:06 Yimiprod

You've got a point. @joshbruce I'd flag this for developer experience

Feder1co5oave avatar Jan 25 '18 21:01 Feder1co5oave

@Feder1co5oave: Thinking labels for lexer, parser (already have), and whatever else might be beneficial as well. Thoughts??

joshbruce avatar Jan 25 '18 23:01 joshbruce

I honestly don't think those kind of categories would help

Feder1co5oave avatar Jan 25 '18 23:01 Feder1co5oave

Fair enough. Let's see how it goes then. :)

joshbruce avatar Jan 25 '18 23:01 joshbruce

Rules is not strictly a public API, so I feel like this is not a valid way to extend marked. Now we have custom extensions that should be used instead.

UziTech avatar Sep 01 '23 17:09 UziTech