From 55ed3680b66dbc9b197291e610d4feb59dc6bcb8 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Sun, 31 May 2020 03:57:21 +0900 Subject: [PATCH] Change the way common methods are mixed in to TypeScript-based lexers (#1527) Sometimes it makes sense for a Rouge lexer to inherit methods from more than one class. Ruby does not have multiple class inheritance and so the idiomatic solution in Rouge is to extract the methods that should be shared into a common module that can be mixed in to the relevant lexers. A Ruby module can be mixed in via either calling `include` or `extend`. If the module is mixed in via `include`, then the methods are added as instance methods of the mixing class. If the module is mixed in via `extend`, the methods are added to the singleton class of the mixing class. Given the way Rouge is architected, it is more correct for the common module to be extended (not doing so can cause issues with overloading methods). This commit updates the TypeScript and TSX lexers to mix in the `TypescriptCommon` module by calling `extend` rather than `include`. --- lib/rouge/lexers/tsx.rb | 2 +- lib/rouge/lexers/typescript.rb | 2 +- lib/rouge/lexers/typescript/common.rb | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/rouge/lexers/tsx.rb b/lib/rouge/lexers/tsx.rb index 043a526f23..c735703de6 100644 --- a/lib/rouge/lexers/tsx.rb +++ b/lib/rouge/lexers/tsx.rb @@ -7,7 +7,7 @@ module Lexers load_lexer 'typescript/common.rb' class TSX < JSX - include TypescriptCommon + extend TypescriptCommon title 'TypeScript' desc 'tsx' diff --git a/lib/rouge/lexers/typescript.rb b/lib/rouge/lexers/typescript.rb index 5a4283fd59..5719c9f787 100644 --- a/lib/rouge/lexers/typescript.rb +++ b/lib/rouge/lexers/typescript.rb @@ -7,7 +7,7 @@ module Lexers load_lexer 'typescript/common.rb' class Typescript < Javascript - include TypescriptCommon + extend TypescriptCommon title "TypeScript" desc "TypeScript, a superset of JavaScript" diff --git a/lib/rouge/lexers/typescript/common.rb b/lib/rouge/lexers/typescript/common.rb index 7d161ff05f..8cb5026323 100644 --- a/lib/rouge/lexers/typescript/common.rb +++ b/lib/rouge/lexers/typescript/common.rb @@ -4,27 +4,27 @@ module Rouge module Lexers module TypescriptCommon - def self.keywords + def keywords @keywords ||= super + Set.new(%w( is namespace static private protected public implements readonly )) end - def self.declarations + def declarations @declarations ||= super + Set.new(%w( type abstract )) end - def self.reserved + def reserved @reserved ||= super + Set.new(%w( string any void number namespace module declare default interface keyof )) end - def self.builtins + def builtins @builtins ||= super + %w( Pick Partial Readonly Record )