From 697b7c80db68944f781e13a3ea1f67a66d32f23e Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Thu, 9 Apr 2020 19:08:10 +0900 Subject: [PATCH 1/9] Fix name and description of TSX lexer --- lib/rouge/lexers/tsx.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rouge/lexers/tsx.rb b/lib/rouge/lexers/tsx.rb index c735703de6..fe747956f5 100644 --- a/lib/rouge/lexers/tsx.rb +++ b/lib/rouge/lexers/tsx.rb @@ -9,8 +9,8 @@ module Lexers class TSX < JSX extend TypescriptCommon - title 'TypeScript' - desc 'tsx' + title 'TSX' + desc 'TypeScript-compatible JSX (www.typescriptlang.org/docs/handbook/jsx.html)' tag 'tsx' filenames '*.tsx' From d947ce0f58d26a337d38ec717e57b62175620371 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Thu, 9 Apr 2020 19:08:50 +0900 Subject: [PATCH 2/9] Add samples of TSX tags --- spec/visual/samples/tsx | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/visual/samples/tsx b/spec/visual/samples/tsx index 29242a8703..ca9d96246c 100644 --- a/spec/visual/samples/tsx +++ b/spec/visual/samples/tsx @@ -81,3 +81,30 @@ ReactDOM.render( , document.getElementById('example') ); + +const withAuthRedirect = (route: string, redirectIfAuthed: boolean) => ( + Page: NextComponentType +) => { + return class extends React.Component

{ + static async getInitialProps(ctx: NextPageContext) { + const shouldContinue = await redirectBasedOnLogin( + ctx, + route, + redirectIfAuthed + ); + if (!shouldContinue) { + return {}; + } + if (Page.getInitialProps) { + return Page.getInitialProps(ctx); + } + } + + render() { + return ; + } + }; +}; + +export const withLoginRedirect = withAuthRedirect('/login', false); +export const withDashboardRedirect = withAuthRedirect('/dashboard', true); From c4b8a5898e9439b75f49f98c77b1bd75bc4e33da Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Fri, 10 Apr 2020 04:14:13 +0900 Subject: [PATCH 3/9] Simplify rules and remove unnecessary embed --- lib/rouge/lexers/jsx.rb | 104 ++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 58 deletions(-) diff --git a/lib/rouge/lexers/jsx.rb b/lib/rouge/lexers/jsx.rb index e934039d6f..09b8db6e5a 100644 --- a/lib/rouge/lexers/jsx.rb +++ b/lib/rouge/lexers/jsx.rb @@ -13,90 +13,78 @@ class JSX < Javascript mimetypes 'text/x-jsx', 'application/x-jsx' - id = Javascript.id_regex - - def start_embed! - @embed ||= JSX.new(options) - @embed.reset! - @embed.push(:expr_start) - push :jsx_embed_root - end - - def tag_token(name) - name[0] =~ /\p{Lower}/ ? Name::Tag : Name::Class - end - start { @html = HTML.new(options) } - state :jsx_tags do - rule %r/{]+/ do + rule %r/[^<{]+/ do delegate @html end - - mixin :jsx_tags - end - - prepend :expr_start do - mixin :jsx_tags end - state :jsx_tag do + state :element do mixin :comments_and_whitespace - rule %r/#{id}/ do |m| - token tag_token(m[0]) + rule %r/\/>/ do + token Punctuation + pop! 2 end - - rule %r/[.]/, Punctuation - end - - state :jsx_end_tag do - mixin :jsx_tag rule %r/>/, Punctuation, :pop! - end - - state :jsx_element do - rule %r/#{id}=/, Name::Attribute, :jsx_attribute - mixin :jsx_tag - rule %r/>/ do token Punctuation; goto :jsx_internal end - rule %r(/>), Punctuation, :pop! - end - - state :jsx_attribute do - rule %r/"(\\[\\"]|[^"])*"/, Str::Double, :pop! - rule %r/'(\\[\\']|[^'])*'/, Str::Single, :pop! rule %r/{/ do token Str::Interpol - pop! - start_embed! + push :interpol + push :expr_start end + rule %r/\w+/, Name::Attribute + rule %r/=/, Punctuation + rule %r/(["']).*?(\1)/, Str + end + + state :element_name do + rule %r/[A-Z]\w*/, Name::Class + rule %r/\w+/, Name::Tag + rule %r/\./, Punctuation + rule(//) { pop! } end - state :jsx_embed_root do - rule %r/[.][.][.]/, Punctuation + state :interpol do rule %r/}/, Str::Interpol, :pop! - mixin :jsx_embed + rule %r/{/ do + token Punctuation + push :interpol_inner + push :statement + end + mixin :root end - state :jsx_embed do - rule %r/{/ do delegate @embed; push :jsx_embed end - rule %r/}/ do delegate @embed; pop! end - rule %r/[^{}]+/ do - delegate @embed + state :interpol_inner do + rule %r/}/ do + token Punctuation + goto :statement end + mixin :root end end end From a44322f00cf7d5481882a1816c24dab4d6abb1d8 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Fri, 10 Apr 2020 04:14:40 +0900 Subject: [PATCH 4/9] Improve support for TSX --- lib/rouge/lexers/tsx.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/rouge/lexers/tsx.rb b/lib/rouge/lexers/tsx.rb index fe747956f5..e1b670adff 100644 --- a/lib/rouge/lexers/tsx.rb +++ b/lib/rouge/lexers/tsx.rb @@ -14,6 +14,13 @@ class TSX < JSX tag 'tsx' filenames '*.tsx' + + prepend :element_name do + rule %r/(\w+)(,)/ do + groups Name::Other, Punctuation + pop! 3 + end + end end end end From 5c79d068bab815740b29ed882597b101e75130f5 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Fri, 10 Apr 2020 04:15:58 +0900 Subject: [PATCH 5/9] Remove unnecessary newlines in JSX sample --- spec/visual/samples/jsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/visual/samples/jsx b/spec/visual/samples/jsx index 5cb4d4d65f..3e2ce9b7a2 100644 --- a/spec/visual/samples/jsx +++ b/spec/visual/samples/jsx @@ -1,8 +1,6 @@ var myDivElement =

; ReactDOM.render(myDivElement, document.getElementById('example')); - - var MyComponent = React.createClass({/*...*/}); var myElement = ; ReactDOM.render(myElement, document.getElementById('example')); From 5d8fbe7286de314d8cb342475f07e1169a05a886 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Fri, 10 Apr 2020 04:17:50 +0900 Subject: [PATCH 6/9] Update JSX description --- lib/rouge/lexers/jsx.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rouge/lexers/jsx.rb b/lib/rouge/lexers/jsx.rb index 09b8db6e5a..cbf0e903e0 100644 --- a/lib/rouge/lexers/jsx.rb +++ b/lib/rouge/lexers/jsx.rb @@ -6,7 +6,7 @@ module Lexers class JSX < Javascript title 'JSX' - desc 'React JSX (https://facebook.github.io/react/)' + desc 'An XML-like syntax extension to JavaScript (facebook.github.io/jsx/)' tag 'jsx' aliases 'jsx', 'react' filenames '*.jsx' From d0bdbf1e3b63795409a12ca0147b76e965c64a66 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Fri, 10 Apr 2020 04:19:47 +0900 Subject: [PATCH 7/9] Remove duplicate example from JSX sample --- spec/visual/samples/jsx | 9 --------- 1 file changed, 9 deletions(-) diff --git a/spec/visual/samples/jsx b/spec/visual/samples/jsx index 3e2ce9b7a2..7127952d6d 100644 --- a/spec/visual/samples/jsx +++ b/spec/visual/samples/jsx @@ -48,15 +48,6 @@ var content = ( ); -var App = ( -
- - - - -
-); - var thing = "; }()}/> class LikeButton extends React.Component { From c375d0d42c0ac061228c3063179e48a4184bf64f Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Sun, 24 May 2020 12:06:01 +0900 Subject: [PATCH 8/9] Push expr_start on lexing start --- lib/rouge/lexers/jsx.rb | 2 +- spec/visual/samples/jsx | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/rouge/lexers/jsx.rb b/lib/rouge/lexers/jsx.rb index cbf0e903e0..90f3cc69cc 100644 --- a/lib/rouge/lexers/jsx.rb +++ b/lib/rouge/lexers/jsx.rb @@ -13,7 +13,7 @@ class JSX < Javascript mimetypes 'text/x-jsx', 'application/x-jsx' - start { @html = HTML.new(options) } + start { @html = HTML.new(options); push :expr_start } prepend :expr_start do mixin :tag diff --git a/spec/visual/samples/jsx b/spec/visual/samples/jsx index 7127952d6d..121270aea6 100644 --- a/spec/visual/samples/jsx +++ b/spec/visual/samples/jsx @@ -1,3 +1,7 @@ +
+ Hello React! +
+ var myDivElement =
; ReactDOM.render(myDivElement, document.getElementById('example')); From 38389d5fef28eedda25364d6791adc1fe0e80370 Mon Sep 17 00:00:00 2001 From: Michael Camilleri Date: Sun, 31 May 2020 04:17:24 +0900 Subject: [PATCH 9/9] Remove fix for generics --- lib/rouge/lexers/tsx.rb | 7 ------- spec/visual/samples/tsx | 27 --------------------------- 2 files changed, 34 deletions(-) diff --git a/lib/rouge/lexers/tsx.rb b/lib/rouge/lexers/tsx.rb index e1b670adff..fe747956f5 100644 --- a/lib/rouge/lexers/tsx.rb +++ b/lib/rouge/lexers/tsx.rb @@ -14,13 +14,6 @@ class TSX < JSX tag 'tsx' filenames '*.tsx' - - prepend :element_name do - rule %r/(\w+)(,)/ do - groups Name::Other, Punctuation - pop! 3 - end - end end end end diff --git a/spec/visual/samples/tsx b/spec/visual/samples/tsx index ca9d96246c..29242a8703 100644 --- a/spec/visual/samples/tsx +++ b/spec/visual/samples/tsx @@ -81,30 +81,3 @@ ReactDOM.render( , document.getElementById('example') ); - -const withAuthRedirect = (route: string, redirectIfAuthed: boolean) => ( - Page: NextComponentType -) => { - return class extends React.Component

{ - static async getInitialProps(ctx: NextPageContext) { - const shouldContinue = await redirectBasedOnLogin( - ctx, - route, - redirectIfAuthed - ); - if (!shouldContinue) { - return {}; - } - if (Page.getInitialProps) { - return Page.getInitialProps(ctx); - } - } - - render() { - return ; - } - }; -}; - -export const withLoginRedirect = withAuthRedirect('/login', false); -export const withDashboardRedirect = withAuthRedirect('/dashboard', true);