From 6cbcd00df3653660685a4491e5b5569876bb915f Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 29 Dec 2022 12:33:02 +0000 Subject: [PATCH] DEV: Introduce syntax_tree for ruby formatting (#62) --- .github/workflows/plugin-linting.yml | 9 ++++++++ .github/workflows/plugin-tests.yml | 6 +++--- .rubocop.yml | 2 +- .streerc | 2 ++ Gemfile | 5 +++-- Gemfile.lock | 4 ++++ spec/pretty_text_spec.rb | 31 +++++++++++++--------------- 7 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 .streerc diff --git a/.github/workflows/plugin-linting.yml b/.github/workflows/plugin-linting.yml index c807794..6d2bb97 100644 --- a/.github/workflows/plugin-linting.yml +++ b/.github/workflows/plugin-linting.yml @@ -55,3 +55,12 @@ jobs: - name: Rubocop if: ${{ !cancelled() }} run: bundle exec rubocop . + + - name: Syntax Tree + if: ${{ !cancelled() }} + run: | + if test -f .streerc; then + bundle exec stree check Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake') + else + echo "Stree config not detected for this repository. Skipping." + fi diff --git a/.github/workflows/plugin-tests.yml b/.github/workflows/plugin-tests.yml index 9d390bc..f30a5be 100644 --- a/.github/workflows/plugin-tests.yml +++ b/.github/workflows/plugin-tests.yml @@ -80,7 +80,7 @@ jobs: - name: Get yarn cache directory id: yarn-cache-dir - run: echo "::set-output name=dir::$(yarn cache dir)" + run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT - name: Yarn cache uses: actions/cache@v3 @@ -130,7 +130,7 @@ jobs: shell: bash run: | if [ 0 -lt $(find plugins/${{ github.event.repository.name }}/spec -type f -name "*.rb" 2> /dev/null | wc -l) ]; then - echo "::set-output name=files_exist::true" + echo "files_exist=true" >> $GITHUB_OUTPUT fi - name: Plugin RSpec @@ -142,7 +142,7 @@ jobs: shell: bash run: | if [ 0 -lt $(find plugins/${{ github.event.repository.name }}/test/javascripts -type f \( -name "*.js" -or -name "*.es6" \) 2> /dev/null | wc -l) ]; then - echo "::set-output name=files_exist::true" + echo "files_exist=true" >> $GITHUB_OUTPUT fi - name: Plugin QUnit diff --git a/.rubocop.yml b/.rubocop.yml index d46296c..fb14dfa 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,2 +1,2 @@ inherit_gem: - rubocop-discourse: default.yml + rubocop-discourse: stree-compat.yml diff --git a/.streerc b/.streerc new file mode 100644 index 0000000..0bc4379 --- /dev/null +++ b/.streerc @@ -0,0 +1,2 @@ +--print-width=100 +--plugins=plugin/trailing_comma diff --git a/Gemfile b/Gemfile index 7da32ec..31d8bf7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,8 @@ # frozen_string_literal: true -source 'https://rubygems.org' +source "https://rubygems.org" group :development do - gem 'rubocop-discourse' + gem "rubocop-discourse" + gem "syntax_tree" end diff --git a/Gemfile.lock b/Gemfile.lock index 2434c95..3b79e02 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,6 +6,7 @@ GEM parallel (1.22.1) parser (3.1.2.1) ast (~> 2.4.1) + prettier_print (1.2.0) rainbow (3.1.1) regexp_parser (2.6.0) rexml (3.2.5) @@ -27,6 +28,8 @@ GEM rubocop-rspec (2.13.2) rubocop (~> 1.33) ruby-progressbar (1.11.0) + syntax_tree (5.1.0) + prettier_print (>= 1.2.0) unicode-display_width (2.3.0) PLATFORMS @@ -39,6 +42,7 @@ PLATFORMS DEPENDENCIES rubocop-discourse + syntax_tree BUNDLED WITH 2.3.10 diff --git a/spec/pretty_text_spec.rb b/spec/pretty_text_spec.rb index c803498..dee33f9 100644 --- a/spec/pretty_text_spec.rb +++ b/spec/pretty_text_spec.rb @@ -1,21 +1,18 @@ # frozen_string_literal: true -require 'rails_helper' +require "rails_helper" describe PrettyText do + context "with discourse math" do + before { SiteSetting.discourse_math_enabled = true } - context 'with discourse math' do - before do - SiteSetting.discourse_math_enabled = true - end - - it 'can handle inline math' do + it "can handle inline math" do cooked = PrettyText.cook('I like $\{a,b\}\$$ etc') html = '

I like \{a,b\}\$<a> etc

' expect(cooked).to eq(html) end - it 'can correctly ignore bad blocks' do + it "can correctly ignore bad blocks" do cooked = PrettyText.cook <<~MD $$a a @@ -31,17 +28,17 @@ describe PrettyText do expect(cooked).to eq(html.strip) end - it 'can handle inline edge cases' do - expect(PrettyText.cook ",$+500\\$").not_to include('math') - expect(PrettyText.cook "$+500$").to include('math') - expect(PrettyText.cook ",$+500$,").to include('math') - expect(PrettyText.cook "200$ + 500$").not_to include('math') - expect(PrettyText.cook ",$+500$x").not_to include('math') - expect(PrettyText.cook "y$+500$").not_to include('math') - expect(PrettyText.cook "($ +500 $)").to include('math') + it "can handle inline edge cases" do + expect(PrettyText.cook ",$+500\\$").not_to include("math") + expect(PrettyText.cook "$+500$").to include("math") + expect(PrettyText.cook ",$+500$,").to include("math") + expect(PrettyText.cook "200$ + 500$").not_to include("math") + expect(PrettyText.cook ",$+500$x").not_to include("math") + expect(PrettyText.cook "y$+500$").not_to include("math") + expect(PrettyText.cook "($ +500 $)").to include("math") end - it 'can handle inline math' do + it "can handle inline math" do cooked = PrettyText.cook <<~MD I like $$