Skip to content

Commit

Permalink
Move rules from TypeScript lexer to TypeScript common module (#1530)
Browse files Browse the repository at this point in the history
The TypeScript lexer was refactored in a previous commit to move its
memoisation methods into a module that could be mixed in by the TSX
lexer. This is because the TSX lexer needs to inherit from both the
TypeScript lexer and the JSX lexer but can't do that direclty because
Ruby does not support multiple inheritance.

While the memoisation methods were moved, changes to states of
TypeScript lexer were not refactored out. This means that the TypeScript
and TSX lexers can lex the same code differently. This commit fixes that
problem by adopting the same approach used with the ObjectiveC common
module: using Ruby's `Module#extended` hook.
  • Loading branch information
pyrmont committed Jun 2, 2020
1 parent 865cde6 commit 30fc93f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 0 additions & 11 deletions lib/rouge/lexers/typescript.rb
Expand Up @@ -18,17 +18,6 @@ class Typescript < Javascript
filenames '*.ts', '*.d.ts'

mimetypes 'text/typescript'

prepend :root do
rule %r/[?][.]/, Punctuation
end

prepend :statement do
rule %r/(#{Javascript.id_regex})(\??)(\s*)(:)/ do
groups Name::Label, Punctuation, Text, Punctuation
push :expr_start
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/rouge/lexers/typescript/common.rb
Expand Up @@ -29,6 +29,19 @@ def builtins
Pick Partial Readonly Record
)
end

def self.extended(base)
base.prepend :root do
rule %r/[?][.]/, base::Punctuation
end

base.prepend :statement do
rule %r/(#{Javascript.id_regex})(\??)(\s*)(:)/ do
groups base::Name::Label, base::Punctuation, base::Text, base::Punctuation
push :expr_start
end
end
end
end
end
end

0 comments on commit 30fc93f

Please sign in to comment.