DEV: Introduce syntax_tree for ruby formatting (#36)

This commit is contained in:
David Taylor 2022-12-29 12:34:38 +00:00 committed by GitHub
parent 625de74642
commit 8e3dcb97c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -1,2 +1,2 @@
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

@ -4,4 +4,5 @@ source "https://rubygems.org"
group :development do
gem "rubocop-discourse"
gem "syntax_tree"
end

View File

@ -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

View File

@ -14,43 +14,46 @@ DiscoursePluginRegistry.serialized_current_user_fields << "signature_url"
DiscoursePluginRegistry.serialized_current_user_fields << "signature_raw"
after_initialize do
User.register_custom_field_type('see_signatures', :boolean)
User.register_custom_field_type('signature_url', :text)
User.register_custom_field_type('signature_raw', :text)
User.register_custom_field_type("see_signatures", :boolean)
User.register_custom_field_type("signature_url", :text)
User.register_custom_field_type("signature_raw", :text)
# add to class and serializer to allow for default value for the setting
add_to_class(:user, :see_signatures) do
if custom_fields['see_signatures'] != nil
custom_fields['see_signatures']
if custom_fields["see_signatures"] != nil
custom_fields["see_signatures"]
else
SiteSetting.signatures_visible_by_default
end
end
add_to_serializer(:user, :see_signatures) do
object.see_signatures
end
add_to_serializer(:user, :see_signatures) { object.see_signatures }
register_editable_user_custom_field [:see_signatures, :signature_url, :signature_raw]
register_editable_user_custom_field %i[see_signatures signature_url signature_raw]
allow_public_user_custom_field :signature_cooked
allow_public_user_custom_field :signature_url
add_to_serializer(:post, :user_signature) do
if SiteSetting.signatures_advanced_mode
object.user.custom_fields['signature_cooked'] if object.user
object.user.custom_fields["signature_cooked"] if object.user
else
object.user.custom_fields['signature_url'] if object.user
object.user.custom_fields["signature_url"] if object.user
end
end
# This is the code responsible for cooking a new advanced mode sig on user update
DiscourseEvent.on(:user_updated) do |user|
if SiteSetting.signatures_enabled? && SiteSetting.signatures_advanced_mode && user.custom_fields['signature_raw']
cooked_sig = PrettyText.cook(user.custom_fields['signature_raw'], omit_nofollow: user.has_trust_level?(TrustLevel[3]) && !SiteSetting.tl3_links_no_follow)
if SiteSetting.signatures_enabled? && SiteSetting.signatures_advanced_mode &&
user.custom_fields["signature_raw"]
cooked_sig =
PrettyText.cook(
user.custom_fields["signature_raw"],
omit_nofollow: user.has_trust_level?(TrustLevel[3]) && !SiteSetting.tl3_links_no_follow,
)
# avoid infinite recursion
if cooked_sig != user.custom_fields['signature_cooked']
user.custom_fields['signature_cooked'] = cooked_sig
if cooked_sig != user.custom_fields["signature_cooked"]
user.custom_fields["signature_cooked"] = cooked_sig
user.save
end
end