DEV: Introduce syntax_tree for ruby formatting (#62)

This commit is contained in:
David Taylor 2022-12-29 12:33:02 +00:00 committed by GitHub
parent 45563f691a
commit 6cbcd00df3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 23 deletions

View File

@ -55,3 +55,12 @@ jobs:
- name: Rubocop - name: Rubocop
if: ${{ !cancelled() }} if: ${{ !cancelled() }}
run: bundle exec rubocop . 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

View File

@ -80,7 +80,7 @@ jobs:
- name: Get yarn cache directory - name: Get yarn cache directory
id: yarn-cache-dir id: yarn-cache-dir
run: echo "::set-output name=dir::$(yarn cache dir)" run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
- name: Yarn cache - name: Yarn cache
uses: actions/cache@v3 uses: actions/cache@v3
@ -130,7 +130,7 @@ jobs:
shell: bash shell: bash
run: | run: |
if [ 0 -lt $(find plugins/${{ github.event.repository.name }}/spec -type f -name "*.rb" 2> /dev/null | wc -l) ]; then 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 fi
- name: Plugin RSpec - name: Plugin RSpec
@ -142,7 +142,7 @@ jobs:
shell: bash shell: bash
run: | 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 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 fi
- name: Plugin QUnit - name: Plugin QUnit

View File

@ -1,2 +1,2 @@
inherit_gem: inherit_gem:
rubocop-discourse: default.yml rubocop-discourse: stree-compat.yml

2
.streerc Normal file
View File

@ -0,0 +1,2 @@
--print-width=100
--plugins=plugin/trailing_comma

View File

@ -1,7 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
source 'https://rubygems.org' source "https://rubygems.org"
group :development do group :development do
gem 'rubocop-discourse' gem "rubocop-discourse"
gem "syntax_tree"
end end

View File

@ -6,6 +6,7 @@ GEM
parallel (1.22.1) parallel (1.22.1)
parser (3.1.2.1) parser (3.1.2.1)
ast (~> 2.4.1) ast (~> 2.4.1)
prettier_print (1.2.0)
rainbow (3.1.1) rainbow (3.1.1)
regexp_parser (2.6.0) regexp_parser (2.6.0)
rexml (3.2.5) rexml (3.2.5)
@ -27,6 +28,8 @@ GEM
rubocop-rspec (2.13.2) rubocop-rspec (2.13.2)
rubocop (~> 1.33) rubocop (~> 1.33)
ruby-progressbar (1.11.0) ruby-progressbar (1.11.0)
syntax_tree (5.1.0)
prettier_print (>= 1.2.0)
unicode-display_width (2.3.0) unicode-display_width (2.3.0)
PLATFORMS PLATFORMS
@ -39,6 +42,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
rubocop-discourse rubocop-discourse
syntax_tree
BUNDLED WITH BUNDLED WITH
2.3.10 2.3.10

View File

@ -1,21 +1,18 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'rails_helper' require "rails_helper"
describe PrettyText do describe PrettyText do
context "with discourse math" do
before { SiteSetting.discourse_math_enabled = true }
context 'with discourse math' do it "can handle inline math" do
before do
SiteSetting.discourse_math_enabled = true
end
it 'can handle inline math' do
cooked = PrettyText.cook('I like $\{a,b\}\$<a>$ etc') cooked = PrettyText.cook('I like $\{a,b\}\$<a>$ etc')
html = '<p>I like <span class="math">\{a,b\}\$&lt;a&gt;</span> etc</p>' html = '<p>I like <span class="math">\{a,b\}\$&lt;a&gt;</span> etc</p>'
expect(cooked).to eq(html) expect(cooked).to eq(html)
end end
it 'can correctly ignore bad blocks' do it "can correctly ignore bad blocks" do
cooked = PrettyText.cook <<~MD cooked = PrettyText.cook <<~MD
$$a $$a
a a
@ -31,17 +28,17 @@ describe PrettyText do
expect(cooked).to eq(html.strip) expect(cooked).to eq(html.strip)
end end
it 'can handle inline edge cases' do it "can handle inline edge cases" do
expect(PrettyText.cook ",$+500\\$").not_to include('math') 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 ",$+500$,").to include('math') expect(PrettyText.cook ",$+500$,").to include("math")
expect(PrettyText.cook "200$ + 500$").not_to include('math') expect(PrettyText.cook "200$ + 500$").not_to include("math")
expect(PrettyText.cook ",$+500$x").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 "y$+500$").not_to include("math")
expect(PrettyText.cook "($ +500 $)").to include('math') expect(PrettyText.cook "($ +500 $)").to include("math")
end end
it 'can handle inline math' do it "can handle inline math" do
cooked = PrettyText.cook <<~MD cooked = PrettyText.cook <<~MD
I like I like
$$ $$