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

WIP: Add Coffeelint #2738

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ This file is generated by `grunt build`, do not edit it by hand.
watch:
default:
files: ['coffee/**/*.coffee', 'sass/*.scss']
tasks: ['build', 'jasmine']
tasks: ['coffeelint', 'build', 'jasmine']
test:
files: ['spec/**/*.coffee']
tasks: ['jasmine']
Expand All @@ -108,6 +108,12 @@ This file is generated by `grunt build`, do not edit it by hand.
specs: 'spec/public/proto_specs.js'
src: [ 'public/chosen.proto.js' ]

coffeelint:
main:
src: [ 'coffee/**/*.coffee' ]
options:
configFile: 'coffeelint.json'

grunt.loadTasks 'tasks'

grunt.registerTask 'default', ['build']
Expand Down
149 changes: 81 additions & 68 deletions coffee/chosen.jquery.coffee

Large diffs are not rendered by default.

216 changes: 106 additions & 110 deletions coffee/chosen.proto.coffee

Large diffs are not rendered by default.

74 changes: 39 additions & 35 deletions coffee/lib/abstract-chosen.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class AbstractChosen

constructor: (@form_field, @options={}) ->
constructor: (@form_field, @options = {}) ->
return unless AbstractChosen.browser_is_supported()
@is_multiple = @form_field.multiple
this.set_default_text()
Expand All @@ -21,7 +21,10 @@ class AbstractChosen
@results_showing = false
@result_highlighted = null
@is_rtl = @options.rtl || /\bchosen-rtl\b/.test(@form_field.className)
@allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0]? and @form_field.options[0].text is "" then @options.allow_single_deselect else false
@allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0]?.text is ''
@options.allow_single_deselect
else
false
@disable_search_threshold = @options.disable_search_threshold || 0
@disable_search = @options.disable_search || false
@enable_split_word_search = if @options.enable_split_word_search? then @options.enable_split_word_search else true
Expand All @@ -38,16 +41,17 @@ class AbstractChosen
@hide_results_on_select = if @options.hide_results_on_select? then @options.hide_results_on_select else true

set_default_text: ->
if @form_field.getAttribute("data-placeholder")
@default_text = @form_field.getAttribute("data-placeholder")
@default_text = if @form_field.getAttribute('data-placeholder')
@form_field.getAttribute('data-placeholder')
else if @is_multiple
@default_text = @options.placeholder_text_multiple || @options.placeholder_text || AbstractChosen.default_multiple_text
@options.placeholder_text_multiple || @options.placeholder_text || AbstractChosen.default_multiple_text
else
@default_text = @options.placeholder_text_single || @options.placeholder_text || AbstractChosen.default_single_text
@options.placeholder_text_single || @options.placeholder_text || AbstractChosen.default_single_text

@default_text = this.escape_html(@default_text)

@results_none_found = @form_field.getAttribute("data-no_results_text") || @options.no_results_text || AbstractChosen.default_no_result_text
@results_none_found = @form_field.getAttribute('data-no_results_text') ||
@options.no_results_text || AbstractChosen.default_no_result_text

choice_label: (item) ->
if @include_group_label_in_selected and item.group_label?
Expand Down Expand Up @@ -106,16 +110,16 @@ class AbstractChosen
return '' unless this.include_option_in_results(option)

classes = []
classes.push "active-result" if !option.disabled and !(option.selected and @is_multiple)
classes.push "disabled-result" if option.disabled and !(option.selected and @is_multiple)
classes.push "result-selected" if option.selected
classes.push "group-option" if option.group_array_index?
classes.push option.classes if option.classes != ""

option_el = document.createElement("li")
option_el.className = classes.join(" ")
classes.push 'active-result' if !option.disabled and !(option.selected and @is_multiple)
classes.push 'disabled-result' if option.disabled and !(option.selected and @is_multiple)
classes.push 'result-selected' if option.selected
classes.push 'group-option' if option.group_array_index?
classes.push option.classes if option.classes != ''

option_el = document.createElement('li')
option_el.className = classes.join(' ')
option_el.style.cssText = option.style
option_el.setAttribute("data-option-array-index", option.array_index)
option_el.setAttribute('data-option-array-index', option.array_index)
option_el.innerHTML = option.search_text
option_el.title = option.title if option.title

Expand All @@ -126,11 +130,11 @@ class AbstractChosen
return '' unless group.active_options > 0

classes = []
classes.push "group-result"
classes.push 'group-result'
classes.push group.classes if group.classes

group_el = document.createElement("li")
group_el.className = classes.join(" ")
group_el = document.createElement('li')
group_el.className = classes.join(' ')
group_el.innerHTML = group.search_text
group_el.title = group.title if group.title

Expand All @@ -143,7 +147,7 @@ class AbstractChosen
this.results_build()
this.winnow_results() if @results_showing

reset_single_select_options: () ->
reset_single_select_options: ->
for result in @results_data
result.selected = false if result.selected

Expand All @@ -165,7 +169,7 @@ class AbstractChosen
results = 0

searchText = this.get_search_text()
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
regex = this.get_search_regex(escapedSearchText)
highlightRegex = this.get_highlight_regex(escapedSearchText)

Expand Down Expand Up @@ -194,7 +198,8 @@ class AbstractChosen
if option.search_match
if searchText.length
startpos = option.search_text.search highlightRegex
text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length)
text = option.search_text.substr(0, startpos + searchText.length) + '</em>' +
option.search_text.substr(startpos + searchText.length)
option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos)

results_group.group_match = true if results_group?
Expand All @@ -205,28 +210,28 @@ class AbstractChosen
this.result_clear_highlight()

if results < 1 and searchText.length
this.update_results_content ""
this.update_results_content ''
this.no_results searchText
else
this.update_results_content this.results_option_build()
this.winnow_results_set_highlight()

get_search_regex: (escaped_search_string) ->
regex_anchor = if @search_contains then "" else "^"
regex_flag = if @case_sensitive_search then "" else "i"
regex_anchor = if @search_contains then '' else '^'
regex_flag = if @case_sensitive_search then '' else 'i'
new RegExp(regex_anchor + escaped_search_string, regex_flag)

get_highlight_regex: (escaped_search_string) ->
regex_anchor = if @search_contains then "" else "\\b"
regex_flag = if @case_sensitive_search then "" else "i"
regex_anchor = if @search_contains then '' else '\\b'
regex_flag = if @case_sensitive_search then '' else 'i'
new RegExp(regex_anchor + escaped_search_string, regex_flag)

search_string_match: (search_string, regex) ->
if regex.test search_string
return true
else if @enable_split_word_search and (search_string.indexOf(" ") >= 0 or search_string.indexOf("[") == 0)
else if @enable_split_word_search and (search_string.indexOf(' ') >= 0 or search_string.indexOf('[') == 0)
#TODO: replace this substitution of /\[\]/ with a list of characters to skip.
parts = search_string.replace(/\[|\]/g, "").split(" ")
parts = search_string.replace(/\[|\]/g, '').split(' ')
if parts.length
for part in parts
if regex.test part
Expand Down Expand Up @@ -329,7 +334,7 @@ class AbstractChosen

outerHTML: (element) ->
return element.outerHTML if element.outerHTML
tmp = document.createElement("div")
tmp = document.createElement('div')
tmp.appendChild(element)
tmp.innerHTML

Expand Down Expand Up @@ -369,7 +374,7 @@ class AbstractChosen
# class methods and variables ============================================================

@browser_is_supported: ->
if "Microsoft Internet Explorer" is window.navigator.appName
if 'Microsoft Internet Explorer' is window.navigator.appName
return document.documentMode >= 8
if /iP(od|hone)/i.test(window.navigator.userAgent) or
/IEMobile/i.test(window.navigator.userAgent) or
Expand All @@ -380,7 +385,6 @@ class AbstractChosen
return false
return true

@default_multiple_text: "Select Some Options"
@default_single_text: "Select an Option"
@default_no_result_text: "No results match"

@default_multiple_text: 'Select Some Options'
@default_single_text: 'Select an Option'
@default_no_result_text: 'No results match'
20 changes: 10 additions & 10 deletions coffee/lib/select-parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SelectParser
@parsed = []

add_node: (child) ->
if child.nodeName.toUpperCase() is "OPTGROUP"
if child.nodeName.toUpperCase() is 'OPTGROUP'
this.add_group child
else
this.add_option child
Expand All @@ -23,8 +23,8 @@ class SelectParser
this.add_option( option, group_position, group.disabled ) for option in group.childNodes

add_option: (option, group_position, group_disabled) ->
if option.nodeName.toUpperCase() is "OPTION"
if option.text != ""
if option.nodeName.toUpperCase() is 'OPTION'
if option.text != ''
if group_position?
@parsed[group_position].children += 1
@parsed.push
Expand All @@ -49,18 +49,18 @@ class SelectParser

escapeExpression: (text) ->
if not text? or text is false
return ""
return ''
unless /[\&\<\>\"\'\`]/.test(text)
return text
map =
"<": "&lt;"
">": "&gt;"
'"': "&quot;"
"'": "&#x27;"
"`": "&#x60;"
'<': '&lt;'
'>': '&gt;'
'"': '&quot;'
"'": '&#x27;'
'`': '&#x60;'
unsafe_chars = /&(?!\w+;)|[\<\>\"\'\`]/g
text.replace unsafe_chars, (chr) ->
map[chr] || "&amp;"
map[chr] || '&amp;'

SelectParser.select_to_array = (select) ->
parser = new SelectParser()
Expand Down
135 changes: 135 additions & 0 deletions coffeelint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"arrow_spacing": {
"level": "error"
},
"braces_spacing": {
"level": "error",
"spaces": 1,
"empty_object_spaces": 0
},
"camel_case_classes": {
"level": "error"
},
"coffeescript_error": {
"level": "error"
},
"colon_assignment_spacing": {
"level": "error",
"spacing": {
"left": 0,
"right": 1
}
},
"cyclomatic_complexity": {
"level": "ignore",
"value": 10
},
"duplicate_key": {
"level": "error"
},
"empty_constructor_needs_parens": {
"level": "ignore"
},
"ensure_comprehensions": {
"level": "warn"
},
"eol_last": {
"level": "error"
},
"indentation": {
"value": 2,
"level": "error"
},
"line_endings": {
"level": "ignore",
"value": "unix"
},
"max_line_length": {
"value": 120,
"level": "error",
"limitComments": true
},
"missing_fat_arrows": {
"level": "ignore",
"is_strict": false
},
"newlines_after_classes": {
"value": 3,
"level": "ignore"
},
"no_backticks": {
"level": "error"
},
"no_debugger": {
"level": "warn",
"console": false
},
"no_empty_functions": {
"level": "ignore"
},
"no_empty_param_list": {
"level": "warn"
},
"no_implicit_braces": {
"level": "ignore",
"strict": true
},
"no_implicit_parens": {
"level": "ignore",
"strict": true
},
"no_interpolation_in_single_quotes": {
"level": "warn"
},
"no_nested_string_interpolation": {
"level": "warn"
},
"no_plusplus": {
"level": "ignore"
},
"no_private_function_fat_arrows": {
"level": "warn"
},
"no_stand_alone_at": {
"level": "error"
},
"no_tabs": {
"level": "error"
},
"no_this": {
"level": "ignore"
},
"no_throwing_strings": {
"level": "error"
},
"no_trailing_semicolons": {
"level": "error"
},
"no_trailing_whitespace": {
"level": "error",
"allowed_in_comments": false,
"allowed_in_empty_lines": true
},
"no_unnecessary_double_quotes": {
"level": "error"
},
"no_unnecessary_fat_arrows": {
"level": "error"
},
"non_empty_constructor_needs_parens": {
"level": "ignore"
},
"prefer_english_operator": {
"level": "ignore",
"doubleNotLevel": "ignore"
},
"space_operators": {
"level": "error"
},
"spacing_after_comma": {
"level": "error"
},
"transform_messes_up_line_numbers": {
"level": "warn"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
"node": ">=0.4.0"
},
"scripts": {
"test": "grunt test"
"test": "grunt coffeelint test"
},
"dependencies": {},
"devDependencies": {
"autoprefixer": "^6.1.2",
"coffee-script": ">= 1.6",
"grunt": "~0.4.1",
"grunt-coffeelint": "^0.0.16",
"grunt-contrib-coffee": "~0.6.4",
"grunt-contrib-concat": "~0.1.3",
"grunt-contrib-cssmin": "~0.6.1",
Expand Down
Loading