This repository contains everything you should need for writing JavaScript at Shopify. In it you’ll find such things as our linting configs, custom linting rules, and project generators. Below, you’ll find the most important thing: a living styleguide documenting how and why we write JavaScript the way we do.
Why? All code in any code-base should look like a single person typed it, no matter how many people contributed. If we all follow along with the rules detailed below, we can focus our efforts on more meaningful problems.
In addition to the above, we have created a specific guide for the tools and conventions surrounding JavaScript testing: our Testing styleguide. There are also a few additional guides for libraries commonly used at Shopify:
Have a legacy codebase? Can’t use ESNext? Our legacy styleguide is available in this repo just for you. We also have a dedicated CoffeeScript styleguide for projects that are still using CoffeeScript (new projects should use ESNext, though!).
Using this guide
Many of the following rules are enforced by our shared ESLint config/plugin, which you can use in most editors and CI environments. To use it, you will need to have Node.js >=5.7.0 and Yarn installed. As an alternative to Yarn, you can use npm which ships with Node.js.
Once these are installed, you must then install ESLint and the Shopify plugin:
Once these are installed, you will need to add an ESLint configuration in your project’s package.json.
{"eslintConfig": {// or "plugin:shopify/es5" for the ES5 config, "plugin:shopify/react" for the React config."extends": "plugin:shopify/esnext",// choose your environments: http://eslint.org/docs/user-guide/configuring.html#specifying-environments"env": {}}}
Note: you can also provide an array of configurations, if you want to have linting rules for tools like lodash. See the eslint-plugin-shopify repo for details.
You can now use ESLint. The easiest way to do this is by adding a linting script to your package.json:
2.1 Use camelCase when naming functions, objects, and instances. Snake case is acceptable when interacting with an external API that provides objects with snake-cased keys, like Rails.
// badconstbad_snake_name='Larry';constBADname='this';constbadObject={some_prop: 'some-value'};// goodconstgoodSnakeName='Basilisk';constprettyName='this';constgoodObject={someProp: 'some-value'};// not ideal, but sometimes necessary and acceptableconstobjectProvidedByRails={some_rails_provided_prop: 'some-value'};
2.2 Use PascalCase when naming classes, factories, enumerations, or singletons (cases of enums are written in screaming snake case).
2.3 Use a leading underscore when naming "private" properties. Functions and variables in scope should be named normally.
Why? The leading underscore sends a signal to other developers that these methods should not be called or relied upon. Some tools can also obfuscate methods with leading underscores to ensure that they are not called by outside objects. Items in scope but not exported are completely private, so no signaling is required for these.
Note: Use these underscore-prefixed members as a last resort. Prefer moving them to be functions/ variables in scope or making them part of the public API over using this naming convention.
2.4 Avoid single letter names; be descriptive with the names you choose. Note that exceptions can be made for common one-letter identifiers, particularly for use as properties (x, y, i, j, _).
3.3 Objects and arrays should use trailing commas, unless they are on a single line. Commas should always be followed by a space, but never preceded by one.
Why? Trailing commas allow you to add and remove a given property of an object without also editing the surrounding lines, which keeps the change isolated to the property in question.
Note: trailing commas are not permitted in JSON, so be sure to omit them.
4.1 In general, add whitespace to improve legibility of code and to group statements together in a way that will produce a more coherent “story” for the next developer to look at code. Never optimize for code size: minifiers will do a much better job than you ever could.
constcondition=true;// badif(condition)doSomething();if(condition)doSomething();doSomethingElse();// will run even if condition is false!// goodif(condition){doSomething();}if(condition){doSomething();doSomethingElse();}
4.4 Place one space before a leading brace. When using if-else and try-catch constructs, the second part starts on the same line as the closing brace of the first, with a single space between them.
4.5 Place one space before the opening parenthesis in control statements (if, while, etc). Place no space between the function name and argument list in function calls and declarations.
4.9 Do not include extra space inside parentheses, brackets, or curly braces that define an object literal. Include spaces for curly braces that define a single-line function.
// badfunctionbad(){dont='do this';returnplease;}// goodfunctiongood(){doThis=true;returnthanks;}// fine as well, as long as there is only one, short statement.functionfine(){return'but don’t push it!';}
4.11 Line comments should appear above the line they are commenting, rather than at the end of the line or below it.
Why? End-of-line comments can be harder to read since they lead to longer lines. Comments above the line you are documenting provides a more natural experience when reading the code, as it follows how we typically read other forms of text.
5.1 Always use let or const (as described in the following rule) to declare variables. Forgetting to do so will result in global variables, which is bad news.
5.2 Use const for all references that do not need to be re-assigned. Use let only for references that will be re-assigned. Never use var.
Why? const and let are block scoped, rather than being function-scoped like var. const indicates that a given reference will always refer to the same object or primitive throughout the current scope, which makes code easier to reason about.
5.3 Initialize variables on declaration as often as possible. If a variable has no value until a later point (for example, it is calculated inside a complex conditional expression, or it is a variable in scope that is redeclared for each unit test), you can declare a variable without initialization.
// badletbad=null;// ...bad='bad';// goodconstgoodOne='good';// or, if you can't provide a value immediately:letgoodTwo;// ...goodTwo='good';
5.4 don’t refer a reference before it is defined. The only exception to this rule is for functions; use the hoisting feature of functions liberally to allow the primary export of a file to appear first (and any helper or utility functions it uses to appear afterwards).
请发表评论