question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reuse of mode containing "starts: { endWithParent: true }" not possible.

See original GitHub issue

I’m still doing my analysis, but maybe anyone already has an idea, so I write everything down here.

In the new rules for the Handlebars language, I found that reusing some mode-definitions did not work because highlight somehow modifies them in context to its parent mode.

I have created a minimal test-case to reproduce this issue:

let hljs = require('../../build');

describe("bugs", function () {
  describe("modes containing 'endsWithParent'", function () {
    it("should be allowed to be reused", function () {
      hljs.registerLanguage('test-language', function (hljs) {

        const TAG_CONTENTS = {className: 'name', begin: /\w+/, starts: {endsWithParent: true}};

        const PAREN_TAG = {className: 'tag', begin: /\(/, end: /\)/, contains: [TAG_CONTENTS]};
        const SQUARE_BRACKET_TAG = {className: 'tag', begin: /\[/, end: /\]/, contains: [TAG_CONTENTS]};

        return {
          contains: [PAREN_TAG, SQUARE_BRACKET_TAG]
        };
      });

      hljs.highlight('test-language', '(abc 123) [abc 123] (abc 123)').value
        .should.equal(
        '<span class="hljs-tag">(<span class="hljs-name">abc</span> 123)</span> ' +
        '<span class="hljs-tag">[<span class="hljs-name">abc</span> 123]</span> ' +
        '<span class="hljs-tag">(<span class="hljs-name">abc</span> 123)</span>'
      )
    })
  })
})

The expected output (“should.equal” above) is

<span class="hljs-tag">(<span class="hljs-name">abc</span> 123)</span> <span class="hljs-tag">[<span class="hljs-name">abc</span> 123]</span> <span class="hljs-tag">(<span class="hljs-name">abc</span> 123)</span>

but the actual output is

<span class="hljs-tag">(<span class="hljs-name">abc</span> 123)</span> <span class="hljs-tag">[<span class="hljs-name">abc</span> 123] (abc 123)</span>

As you can see, the mode for [abc 123] is not terminated after the closing square bracket, but remains until the end of the string.

Looking at the “compiled” language, I can see that the property SQUARE_BRACKET_TAG.contains[0].starts.terminator_end is /\)/ but it should by /\]/

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
joshgoebelcommented, Oct 16, 2019

Fixed in master!

0reactions
nknappcommented, Oct 15, 2019

Updated PR with modified handlebars definition reproducing the old bug (now fixed) #2207

Read more comments on GitHub >

github_iconTop Results From Across the Web

20211106 Nov 6 Data Drop
Toss it all, The new curriculum is not working for students or teachers. ... a written summary at year end. With parent/teacher interviews...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found