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

Clean up CoffeeScript code #3651

Merged
merged 19 commits into from
Sep 11, 2019
Merged

Clean up CoffeeScript code #3651

merged 19 commits into from
Sep 11, 2019

Conversation

javierm
Copy link
Member

@javierm javierm commented Jul 1, 2019

References

Background

We're planning to convert all our CoffeeScript code to JavaScript. As a first step, this pull request cleans some of the CoffeeScript code up, and rewrites the parts which would generate complex JavaScript code.

Objectives

  • Adjust CoffeeScript code so it's easier to compile it to JavaScript
  • Use JavaScript strict mode so we avoid some JavaScript pitfalls in the future

@@ -82,8 +81,7 @@ App.Map =
e.target.bindPopup(getPopupContent(data)).openPopup()

getPopupContent = (data) ->
content = "<a href='/budgets/#{data["budget_id"]}/investments/#{data["investment_id"]}'>#{data["investment_title"]}</a>"
return content
"<a href='/budgets/#{data["budget_id"]}/investments/#{data["investment_id"]}'>#{data["investment_title"]}</a>"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line exceeds maximum allowed length. Length is 116, max is 100.

$("#new_legislation_annotation #legislation_annotation_quote").val(@annotation.quote)
$("#new_legislation_annotation #legislation_annotation_ranges").val(JSON.stringify(@annotation.ranges))
$("#new_legislation_annotation #legislation_annotation_quote").val(this.annotation.quote)
$("#new_legislation_annotation #legislation_annotation_ranges").val(JSON.stringify(this.annotation.ranges))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line exceeds maximum allowed length. Length is 115, max is 100.

process_background = "[name='legislation_process[background_color]']"
process_font = ", [name='legislation_process[font_color]']"
processes = process_background + process_font
processes = "[name='legislation_process[background_color]'], [name='legislation_process[font_color]']"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line exceeds maximum allowed length. Length is 106, max is 100.

@javierm javierm self-assigned this Jul 1, 2019
@javierm javierm force-pushed the simplify_coffeescript branch 2 times, most recently from a49685e to d713068 Compare July 2, 2019 12:30
@javierm javierm changed the base branch from remove_ie8_code to remove_unused_javascript July 2, 2019 12:32
@javierm javierm force-pushed the remove_unused_javascript branch 2 times, most recently from 05e5789 to 5167622 Compare July 2, 2019 15:35
@javierm javierm changed the base branch from remove_unused_javascript to fix_javascript_typos July 2, 2019 20:55
@javierm javierm force-pushed the simplify_coffeescript branch 4 times, most recently from e240e98 to febe71d Compare July 3, 2019 00:17
@javierm javierm force-pushed the simplify_coffeescript branch 2 times, most recently from efe1160 to 5392646 Compare July 3, 2019 02:08
@javierm javierm force-pushed the simplify_coffeescript branch 2 times, most recently from 798e90b to de7f0d5 Compare July 7, 2019 21:35

initialize: ->
if App.WatchFormChanges.forms().length == 0 || App.WatchFormChanges.msg() == undefined
return

$(document).off("page:before-change").on("page:before-change", -> App.WatchFormChanges.checkChanges())
$(document).off("page:before-change").on("page:before-change", App.WatchFormChanges.checkChanges)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line exceeds maximum allowed length. Length is 101, max is 100.

@javierm javierm force-pushed the fix_javascript_typos branch 2 times, most recently from 7b83d79 to eab35dc Compare September 11, 2019 00:07
@javierm javierm changed the base branch from fix_javascript_typos to master September 11, 2019 01:13
There were functions which were just simple wrappers to common jQuery
functions.
These methods were added in 2009 and are supported by 98.5% of the
browsers, including Internet Explorer 9, 10 and 11. We were already
using them in some places.
We're planning to replace CoffeeScript with JavaScript, and CoffeeScript
compiles `for .. in` to complex JavaScript code.
The code won't do anything if there are no elements, so there's no need
to check that condition.
The `append` method can handle arrays, so there's no need to loop
through each element.

There's more to improve on this method, like the `asc` variable being
global to a table instead of depending on the current column, but for
now I'm only refactoring and maintaining the current behaviour.
Including the table body is more intuitive than excluding the first row
and the table foot.
Using the same name means from that point the name doesn't refer to the
original parameter, which might be confusing when reading the method.
Iterating through the jQuery elements with `each` is more similar to
other iterators we use. Furthermore, we avoid declaring the `index`
variable we don't use.
Combining both the inline and "traditional" `if` styles made the code
difficult to read, particularly when compiled to JavaScript.
In foundation extras we were assigning functions to local variables, but
in the rest of the CoffeeScript files we use them as object properties.
We're using `this` most of the time, and it's what's used in JavaScript.
Strict mode is supported by 98% of the browsers, including Internet
Explorer 10, and it helps developers avoid common JavaScript pitfalls.
The assignment operator can easily be mistaken with the equality
operator when used in a condition, making the code more difficult to
read.
In JavaScript we cannot easily repeat something N times, and
CoffeeScript's range loop compiles into complex JavaScript.

We could also use `Array.from({ length: N })`, but that's not supported
by old browsers like Internet Explorer 11.
Local variables are one of the things CoffeeScript doesn't compile to
modern JavaScript automatically: it uses `var` instead of `const` or
`let`.

Besides, using `$this = $(this)` is usually done to reference the
current object in another function where the current object is a
different one. Here we were using it with no clear purpose.
We accidentally added some single quotes recently.
@javierm javierm merged commit 2ff2100 into master Sep 11, 2019
@javierm javierm deleted the simplify_coffeescript branch September 11, 2019 12:03
smarques pushed a commit to venetochevogliamo/consul that referenced this pull request Apr 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants