diff --git a/.coffeelintignore b/.coffeelintignore new file mode 100644 index 000000000..3c3629e64 --- /dev/null +++ b/.coffeelintignore @@ -0,0 +1 @@ +node_modules diff --git a/CODE_STYLE.md b/CODE_STYLE.md new file mode 100644 index 000000000..5e3eea0df --- /dev/null +++ b/CODE_STYLE.md @@ -0,0 +1,496 @@ +# CoffeeScript Style Guide + +This guide presents a collection of best-practices and coding conventions for the [CoffeeScript][coffeescript] programming language. + +This guide is intended to be community-driven, and contributions are highly encouraged. + +Please note that this is a work-in-progress: there is much more that can be specified, and some of the guidelines that have been specified may not be deemed to be idiomatic by the community (in which case, these offending guidelines will be modified or removed, as appropriate). + +## Inspiration + +The details in this guide have been very heavily inspired by several existing style guides and other resources. In particular: + +- [PEP-8][pep8]: Style Guide for Python Code +- Bozhidar Batsov's [Ruby Style Guide][ruby-style-guide] +- [Google's JavaScript Style Guide][google-js-styleguide] +- [Common CoffeeScript Idioms][common-coffeescript-idioms] +- Thomas Reynolds' [CoffeeScript-specific Style Guide][coffeescript-specific-style-guide] +- Jeremy Ashkenas' [code review][spine-js-code-review] of [Spine][spine-js] +- The [CoffeeScript FAQ][coffeescript-faq] + +## Table of Contents + +* [The CoffeeScript Style Guide](#guide) + * [Code Layout](#code_layout) + * [Tabs or Spaces?](#tabs_or_spaces) + * [Maximum Line Length](#maximum_line_length) + * [Blank Lines](#blank_lines) + * [Trailing Whitespace](#trailing_whitespace) + * [Optional Commas](#optional_commas) + * [Encoding](#encoding) + * [Module Imports](#module_imports) + * [Whitespace in Expressions and Statements](#whitespace) + * [Comments](#comments) + * [Block Comments](#block_comments) + * [Inline Comments](#inline_comments) + * [Naming Conventions](#naming_conventions) + * [Functions](#functions) + * [Strings](#strings) + * [Conditionals](#conditionals) + * [Looping and Comprehensions](#looping_and_comprehensions) + * [Extending Native Objects](#extending_native_objects) + * [Exceptions](#exceptions) + * [Annotations](#annotations) + * [Miscellaneous](#miscellaneous) + + +## Code layout + + +### Tabs or Spaces? + +Use **spaces only**, with **2 spaces** per indentation level. Never mix tabs and spaces. + + +### Maximum Line Length + +Limit all lines to a maximum of 109 characters. + + +### Blank Lines + +Separate top-level function and class definitions with a single blank line. + +Separate method definitions inside of a class with a single blank line. + +Use a single blank line within the bodies of methods or functions in cases where this improves readability (e.g., for the purpose of delineating logical sections). + + +### Trailing Whitespace + +Do not include trailing whitespace on any lines. + + +### Optional Commas + +Avoid the use of commas before newlines when properties or elements of an Object or Array are listed on separate lines. + +```coffeescript +# Yes +foo = [ + 'some' + 'string' + 'values' +] +bar: + label: 'test' + value: 87 + +# No +foo = [ + 'some', + 'string', + 'values' +] +bar: + label: 'test', + value: 87 +``` + + +### Encoding + +UTF-8 is the preferred source file encoding. + + +## Module Imports + +If using a module system (CommonJS Modules, AMD, etc.), `require` statements should be placed on separate lines. + +```coffeescript +require 'lib/setup' +Backbone = require 'backbone' +``` +These statements should be grouped in the following order: + +1. Standard library imports _(if a standard library exists)_ +2. Third party library imports +3. Local imports _(imports specific to this application or library)_ + + +## Whitespace in Expressions and Statements + +Avoid extraneous whitespace in the following situations: + +- Immediately inside parentheses, brackets or braces + + ```coffeescript + ($ 'body') # Yes + ( $ 'body' ) # No + ``` + +- Immediately before a comma + + ```coffeescript + console.log x, y # Yes + console.log x , y # No + ``` + +Additional recommendations: + +- Always surround these binary operators with a **single space** on either side + + - assignment: `=` + + - _Note that this also applies when indicating default parameter value(s) in a function declaration_ + + ```coffeescript + test: (param = null) -> # Yes + test: (param=null) -> # No + ``` + + - augmented assignment: `+=`, `-=`, etc. + - comparisons: `==`, `<`, `>`, `<=`, `>=`, `unless`, etc. + - arithmetic operators: `+`, `-`, `*`, `/`, etc. + + - _(Do not use more than one space around these operators)_ + + ```coffeescript + # Yes + x = 1 + y = 1 + fooBar = 3 + + # No + x = 1 + y = 1 + fooBar = 3 + ``` + + +## Comments + +If modifying code that is described by an existing comment, update the comment such that it accurately reflects the new code. (Ideally, improve the code to obviate the need for the comment, and delete the comment entirely.) + +The first word of the comment should be capitalized, unless the first word is an identifier that begins with a lower-case letter. + +If a comment is short, the period at the end can be omitted. + + +### Block Comments + +Block comments apply to the block of code that follows them. + +Each line of a block comment starts with a `#` and a single space, and should be indented at the same level of the code that it describes. + +Paragraphs inside of block comments are separated by a line containing a single `#`. + +```coffeescript + # This is a block comment. Note that if this were a real block + # comment, we would actually be describing the proceeding code. + # + # This is the second paragraph of the same block comment. Note + # that this paragraph was separated from the previous paragraph + # by a line containing a single comment character. + + init() + start() + stop() +``` + + +### Inline Comments + +Inline comments are placed on the line immediately above the statement that they are describing. If the inline comment is sufficiently short, it can be placed on the same line as the statement (separated by a single space from the end of the statement). + +All inline comments should start with a `#` and a single space. + +The use of inline comments should be limited, because their existence is typically a sign of a code smell. + +Do not use inline comments when they state the obvious: + +```coffeescript + # No + x = x + 1 # Increment x +``` + +However, inline comments can be useful in certain scenarios: + +```coffeescript + # Yes + x = x + 1 # Compensate for border +``` + + +## Naming Conventions + +Use `camelCase` (with a leading lowercase character) to name all variables, methods, and object properties. + +Use `CamelCase` (with a leading uppercase character) to name all classes. _(This style is also commonly referred to as `PascalCase`, `CamelCaps`, or `CapWords`, among [other alternatives][camel-case-variations].)_ + +_(The **official** CoffeeScript convention is camelcase, because this simplifies interoperability with JavaScript. For more on this decision, see [here][coffeescript-issue-425].)_ + +For constants, use all uppercase with underscores: + +```coffeescript +CONSTANT_LIKE_THIS +``` + +Methods and variables that are intended to be "private" should begin with a leading underscore: + +```coffeescript +_privateMethod: -> +``` + + +## Functions + +_(These guidelines also apply to the methods of a class.)_ + +When declaring a function that takes arguments, always use a single space after the closing parenthesis of the arguments list: + +```coffeescript +foo = (arg1, arg2) -> # Yes +foo = (arg1, arg2)-> # No +``` + +Do not use parentheses when declaring functions that take no arguments: + +```coffeescript +bar = -> # Yes +bar = () -> # No +``` + +In cases where method calls are being chained and the code does not fit on a single line, each call should be placed on a separate line and indented by one level (i.e., two spaces), with a leading `.`. + +```coffeescript +[1..3] + .map((x) -> x * x) + .concat([10..12]) + .filter((x) -> x < 11) + .reduce((x, y) -> x + y) +``` + +When calling functions, choose to omit or include parentheses in such a way that optimizes for readability. Keeping in mind that "readability" can be subjective, the following examples demonstrate cases where parentheses have been omitted or included in a manner that the community deems to be optimal: + +```coffeescript +baz 12 + +brush.ellipse x: 10, y: 20 # Braces can also be omitted or included for readability + +foo(4).bar(8) + +obj.value(10, 20) / obj.value(20, 10) + +print inspect value + +new Tag(new Value(a, b), new Arg(c)) +``` + +You will sometimes see parentheses used to group functions (instead of being used to group function parameters). Examples of using this style (hereafter referred to as the "function grouping style"): + +```coffeescript +($ '#selektor').addClass 'klass' + +(foo 4).bar 8 +``` + +This is in contrast to: + +```coffeescript +$('#selektor').addClass 'klass' + +foo(4).bar 8 +``` + +In cases where method calls are being chained, some adopters of this style prefer to use function grouping for the initial call only: + +```coffeescript +($ '#selektor').addClass('klass').hide() # Initial call only +(($ '#selektor').addClass 'klass').hide() # All calls +``` + +The function grouping style is not recommended. However, **if the function grouping style is adopted for a particular project, be consistent with its usage.** + + +## Strings + +Use string interpolation instead of string concatenation: + +```coffeescript +"this is an #{adjective} string" # Yes +"this is an " + adjective + " string" # No +``` + +Prefer double quoted strings (`""`) instead of single quoted (`''`) strings, unless features like string interpolation are being used for the given string. + + +## Conditionals + +Favor `unless` over `if` for negative conditions. + +Instead of using `unless...else`, use `if...else`: + +```coffeescript + # Yes + if true + ... + else + ... + + # No + unless false + ... + else + ... +``` + +Multi-line if/else clauses should use indentation: + +```coffeescript + # Yes + if true + ... + else + ... + + # No + if true then ... + else ... +``` + + +## Looping and Comprehensions + +Take advantage of comprehensions whenever possible: + +```coffeescript + # Yes + result = (item.name for item in array) + + # No + results = [] + for item in array + results.push item.name +``` + +To filter: + +```coffeescript +result = (item for item in array when item.name is "test") +``` + +To iterate over the keys and values of objects: + +```coffeescript +object = one: 1, two: 2 +alert("#{key} = #{value}") for key, value of object +``` + + +## Extending Native Objects + +Do not modify native objects. + +For example, do not modify `Array.prototype` to introduce `Array#forEach`. + + +## Exceptions + +Do not suppress exceptions. + + +## Annotations + +Use annotations when necessary to describe a specific action that must be taken against the indicated block of code. + +Write the annotation on the line immediately above the code that the annotation is describing. + +The annotation keyword should be followed by a colon and a space, and a descriptive note. + +```coffeescript + # FIXME: The client's current state should *not* affect payload processing. + resetClientState() + processPayload() +``` + +If multiple lines are required by the description, indent subsequent lines with two spaces: + +```coffeescript + # TODO: Ensure that the value returned by this call falls within a certain + # range, or throw an exception. + analyze() +``` + +Annotation types: + +- `TODO`: describe missing functionality that should be added at a later date +- `FIXME`: describe broken code that must be fixed +- `OPTIMIZE`: describe code that is inefficient and may become a bottleneck +- `HACK`: describe the use of a questionable (or ingenious) coding practice +- `REVIEW`: describe code that should be reviewed to confirm implementation + +If a custom annotation is required, the annotation should be documented in the project's README. + + +## Miscellaneous + +`and` is preferred over `&&`. + +`or` is preferred over `||`. + +`is` is preferred over `==`. + +`isnt` is preferred over `!=`. + +`not` is preferred over `!`. + +`or=` should be used when possible: + +```coffeescript +temp or= {} # Yes +temp = temp || {} # No +``` + +Prefer shorthand notation (`::`) for accessing an object's prototype: + +```coffeescript +Array::slice # Yes +Array.prototype.slice # No +``` + +Prefer `@property` over `this.property`. + +```coffeescript +return @property # Yes +return this.property # No +``` + +However, avoid the use of **standalone** `@`: + +```coffeescript +return this # Yes +return @ # No +``` + +Avoid `return` where not required, unless the explicit return increases clarity. + +Use splats (`...`) when working with functions that accept variable numbers of arguments: + +```coffeescript +console.log args... # Yes + +(a, b, c, rest...) -> # Yes +``` + +[coffeescript]: http://jashkenas.github.com/coffee-script/ +[coffeescript-issue-425]: https://github.com/jashkenas/coffee-script/issues/425 +[spine-js]: http://spinejs.com/ +[spine-js-code-review]: https://gist.github.com/1005723 +[pep8]: http://www.python.org/dev/peps/pep-0008/ +[ruby-style-guide]: https://github.com/bbatsov/ruby-style-guide +[google-js-styleguide]: http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml +[common-coffeescript-idioms]: http://arcturo.github.com/library/coffeescript/04_idioms.html +[coffeescript-specific-style-guide]: http://awardwinningfjords.com/2011/05/13/coffeescript-specific-style-guide.html +[coffeescript-faq]: https://github.com/jashkenas/coffee-script/wiki/FAQ +[camel-case-variations]: http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e4ed8b8bf..f4ee9ecd3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -62,9 +62,9 @@ reports: ## Coding Style - * We follow the recommendations from + * We follow the recommendations in [our style guide](CODE_STYLE.md), based off [this style guide](https://github.com/polarmobile/coffeescript-style-guide). - * We follow two major differences from this style guide: + * Our style guide contains two major differences from the original: * Wrap lines at 110 characters instead of 80. * Use double-quoted strings by default. * When writing comments, uppercase the first letter of your sentence, and put a period at the end. @@ -77,6 +77,10 @@ reports: # Yes return if i < 10 +To test code style against the style guide programatically: +1. `npm install -g coffeelint` to install [CoffeeLint](http://www.coffeelint.org). +1. `cake lint` to run the tests. + ## Pull Requests When you're done with your changes, send us a pull request on Github. Feel free to include a change to the diff --git a/Cakefile b/Cakefile index d188ea128..9e4ed7166 100644 --- a/Cakefile +++ b/Cakefile @@ -160,3 +160,7 @@ task "coverage", "generate coverage report", -> source: (Utils.escapeHtml fs.readFileSync fname, 'utf-8').split '\n' fs.writeFileSync 'jscoverage.json', JSON.stringify(result) + +task "lint", "check code style against the style guide", (options) -> + linter = spawn "coffeelint", ["--color=always", "-f", "coffeelint.json", "."] + linter.on "exit", (returnCode) -> process.exit returnCode diff --git a/coffeelint.json b/coffeelint.json new file mode 100644 index 000000000..14161d58b --- /dev/null +++ b/coffeelint.json @@ -0,0 +1,114 @@ +{ + "coffeescript_error": { + "level": "error" + }, + "arrow_spacing": { + "name": "arrow_spacing", + "level": "warn" + }, + "no_tabs": { + "name": "no_tabs", + "level": "error" + }, + "no_trailing_whitespace": { + "name": "no_trailing_whitespace", + "level": "warn", + "allowed_in_comments": false, + "allowed_in_empty_lines": true + }, + "max_line_length": { + "name": "max_line_length", + "value": 110, + "level": "warn", + "limitComments": true + }, + "line_endings": { + "name": "line_endings", + "level": "ignore", + "value": "unix" + }, + "no_trailing_semicolons": { + "name": "no_trailing_semicolons", + "level": "error" + }, + "indentation": { + "name": "indentation", + "value": 2, + "level": "error" + }, + "camel_case_classes": { + "name": "camel_case_classes", + "level": "error" + }, + "colon_assignment_spacing": { + "name": "colon_assignment_spacing", + "level": "warn", + "spacing": { + "left": 0, + "right": 1 + } + }, + "no_implicit_braces": { + "name": "no_implicit_braces", + "level": "ignore", + "strict": true + }, + "no_plusplus": { + "name": "no_plusplus", + "level": "ignore" + }, + "no_throwing_strings": { + "name": "no_throwing_strings", + "level": "error" + }, + "no_backticks": { + "name": "no_backticks", + "level": "error" + }, + "no_implicit_parens": { + "name": "no_implicit_parens", + "level": "ignore" + }, + "no_empty_param_list": { + "name": "no_empty_param_list", + "level": "warn" + }, + "no_stand_alone_at": { + "name": "no_stand_alone_at", + "level": "ignore" + }, + "space_operators": { + "name": "space_operators", + "level": "warn" + }, + "duplicate_key": { + "name": "duplicate_key", + "level": "error" + }, + "empty_constructor_needs_parens": { + "name": "empty_constructor_needs_parens", + "level": "ignore" + }, + "cyclomatic_complexity": { + "name": "cyclomatic_complexity", + "value": 10, + "level": "ignore" + }, + "newlines_after_classes": { + "name": "newlines_after_classes", + "value": 3, + "level": "ignore" + }, + "no_unnecessary_fat_arrows": { + "name": "no_unnecessary_fat_arrows", + "level": "warn" + }, + "missing_fat_arrows": { + "name": "missing_fat_arrows", + "level": "ignore" + }, + "non_empty_constructor_needs_parens": { + "name": "non_empty_constructor_needs_parens", + "level": "ignore" + } +}