Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#806) Use ASCII quotes instead of Unicode quotes #807

Merged
merged 2 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions ext/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,48 @@ def autocorrect(node)
end
end
end

class SmartQuotes < Cop
MSG = 'Use ASCII quotes instead of Unicode smart quotes'.freeze
SINGLE_QUOTES_PAT = %r{(?:\u2018|\u2019)}
DOUBLE_QUOTES_PAT = %r{(?:\u201C|\u201D)}

def on_str(node)
return unless node.loc.respond_to?(:begin) && node.loc.begin
return if part_of_ignored_node?(node)

add_offense(node) if smart_quotes?(node)
end

def on_regexp(node)
add_offense(node) if smart_quotes?(node)
end

def smart_quotes?(node)
smart_single_quotes?(node) || smart_double_quotes?(node)
end

def smart_single_quotes?(node)
node.source.index(SINGLE_QUOTES_PAT)
end

def smart_double_quotes?(node)
node.source.index(DOUBLE_QUOTES_PAT)
end

def autocorrect(node)
->(corrector) do
if smart_single_quotes?(node)
new_str = node.source.gsub(SINGLE_QUOTES_PAT, "'")
if new_str.start_with?("'")
new_str[0] = '"'
new_str[-1] = '"'
end
corrector.replace(node.loc.expression, new_str)
end
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pdk/cli/module/build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module PDK::CLI
summary _('This command is now \'pdk build\'.')

run do |_opts, _args, _cmd|
PDK.logger.warn(_('Modules are built using the pdk build command.'))
PDK.logger.warn(_("Modules are built using the 'pdk build' command."))
exit 1
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pdk/cli/module/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module PDK::CLI

PDK::CLI::Util.validate_template_opts(opts)

PDK.logger.info(_('New modules are created using the pdk new module command.'))
PDK.logger.info(_("New modules are created using the 'pdk new module' command."))
prompt = TTY::Prompt.new(help_color: :cyan)
redirect = PDK::CLI::Util::CommandRedirector.new(prompt)
redirect.target_command('pdk new module')
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pdk/cli/module/build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

describe 'when called' do
it do
expect(logger).to receive(:warn).with(%r{Modules are built using the pdk build command}i)
expect(logger).to receive(:warn).with(%r{Modules are built using the 'pdk build' command}i)
expect {
PDK::CLI.run(%w[module build])
}.to exit_nonzero
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/pdk/cli/module/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
redirector = instance_double('PDK::CLI::Util::CommandRedirector')
allow(redirector).to receive(:target_command)
allow(redirector).to receive(:run).and_return(true)
expect(logger).to receive(:info).with(%r{New modules are created using the pdk new module command}i)
expect(logger).to receive(:info).with(%r{New modules are created using the 'pdk new module' command}i)
expect(PDK::CLI::Util::CommandRedirector).to receive(:new).and_return(redirector)
end

Expand Down