Shopify / javascript

The home for all things JavaScript at Shopify.
MIT License
253 stars 38 forks source link

import JavaScript from 'Shopify'

Circle CI

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.

Table of contents

  1. Using this guide
  2. Naming
  3. Punctuation
  4. Whitespace
  5. References
  6. Control flow
  7. Objects
  8. Arrays
  9. Strings
  10. Functions
  11. Types and casting
  12. ESNext features
  13. Project structure
  14. Resources

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:

With Yarn

yarn add --dev eslint eslint-plugin-shopify

With npm

npm install eslint eslint-plugin-shopify --save-dev

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:

{
  "scripts": {
    "lint": "eslint . --max-warnings 0"
  }
}

And, finally, run your new script:

With Yarn

yarn run lint

With npm

npm run lint

Naming

↑ scrollTo('#table-of-contents')

Punctuation

↑ scrollTo('#table-of-contents')

Whitespace

↑ scrollTo('#table-of-contents')

References

↑ scrollTo('#table-of-contents')

Control flow

↑ scrollTo('#table-of-contents')

Objects

↑ scrollTo('#table-of-contents')

Arrays

↑ scrollTo('#table-of-contents')

Strings

↑ scrollTo('#table-of-contents')

Functions

Arrow Functions

Parameters

↑ scrollTo('#table-of-contents')

Types and casting

↑ scrollTo('#table-of-contents')

ESNext Features

Destructuring

Classes

Modules

↑ scrollTo('#table-of-contents')

Project structure

↑ scrollTo('#table-of-contents')

Resources

Performance

Writing performant JavaScript is our top priority. No amount of well-structured, clean JavaScript can compensate for the end user having a bad experience. You can find a collection of great performance-related resources on the FED resources page.

Tools

Books

Blogs

Articles

Podcasts

Other

↑ scrollTo('#table-of-contents')