Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Twig/Jinja: incorrect recognition of some special tokens like keywords #1949

Merged
merged 1 commit into from
May 4, 2023

Conversation

nsfisis
Copy link
Contributor

@nsfisis nsfisis commented May 3, 2023

Bug

Twig and Jinja may return incorrect token types for some special tokens like keywords. It depends on loading order.

cf. #1939 (comment)

Cause

Twig and Jinja lexers share class variables. Both classes define keywords method like this:

class Jinja
  def self.keywords
    @@keywords ||= ...
  end
end

class Twig < Jinja
  def self.keywords
    @@keywords ||= ...
  end
end

If Twig.keywords is called before Jinja.keywords, @@keywords is set to Twig's and both methods return the same value. If not, @@keywords is initialized to Jinja's.

Solution

I changed class variables to instance variables. Other lexer classes define their keywords as instance variables.

…words

`Twig` and `Jinja` lexers share some variables as class variables.
Both classes define `keywords` method like this:

```
class Jinja
  def self.keywords
    @@Keywords ||= ...
  end
end

class Twig < Jinja
  def self.keywords
    @@Keywords ||= ...
  end
end
```

If `Twig.keywords` is called before `Jinja.keywords`, `@@keywords` is
set to Twig's and both classes return the same value. If not,
`@@keywords` is initialized to Jinja's.
@tancnle
Copy link
Collaborator

tancnle commented May 4, 2023

Thanks, @nsfisis. The change looks great to me 🚀

@tancnle tancnle added this pull request to the merge queue May 4, 2023
Merged via the queue into rouge-ruby:master with commit 96b11b5 May 4, 2023
@nsfisis nsfisis deleted the fix/twig-jinja branch May 7, 2023 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants