1 | # eslint-plugin-shopify
|
2 |
|
3 | [![NPM version][npm-image]][npm-url]
|
4 | [![Circle CI](https://circleci.com/gh/Shopify/eslint-plugin-shopify.svg?style=shield)](https://circleci.com/gh/Shopify/eslint-plugin-shopify)
|
5 | [![David DM](https://david-dm.org/Shopify/eslint-plugin-shopify.svg)](https://david-dm.org/Shopify/eslint-plugin-shopify)
|
6 |
|
7 | Shopify’s ESLint rules and configs.
|
8 |
|
9 | ## Installation
|
10 |
|
11 | You'll first need to install [ESLint](http://eslint.org):
|
12 |
|
13 | **With Yarn**
|
14 |
|
15 | ```bash
|
16 | yarn add --dev eslint
|
17 | ```
|
18 |
|
19 | **With npm**
|
20 |
|
21 | ```bash
|
22 | $ npm i eslint --save-dev
|
23 | ```
|
24 |
|
25 | Next, install `eslint-plugin-shopify`:
|
26 |
|
27 | **With Yarn**
|
28 | ```bash
|
29 | yarn add --dev eslint-plugin-shopify
|
30 | ```
|
31 |
|
32 | **With npm**
|
33 | ```bash
|
34 | $ npm install eslint-plugin-shopify --save-dev
|
35 | ```
|
36 |
|
37 | **Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-shopify` globally.
|
38 |
|
39 | ## Usage
|
40 |
|
41 | Shopify’s ESLint configs come bundled in this package. In order to use them, you simply extend the relevant configuration in your project’s `.eslintrc`. For example, the following will extend the ESNext (ES2015 and later) config:
|
42 |
|
43 | ```json
|
44 | {
|
45 | "extends": "plugin:shopify/esnext"
|
46 | }
|
47 | ```
|
48 |
|
49 | If using React, extend the React version of the configuration (which adds some React-specific rules to those in the ESNext config):
|
50 |
|
51 | ```json
|
52 | {
|
53 | "extends": "plugin:shopify/react"
|
54 | }
|
55 | ```
|
56 |
|
57 | If working on an ES5 project, extend the ES5 version of the configuration:
|
58 |
|
59 | ```json
|
60 | {
|
61 | "extends": "plugin:shopify/es5"
|
62 | }
|
63 | ```
|
64 |
|
65 | You can also add some "augmenting" configs on top of the "core" config by extending an array of linting configs. For example, this package provides a lodash linting config, which can be added to the ESNext config with the following configuration file:
|
66 |
|
67 | ```json
|
68 | {
|
69 | "extends": [
|
70 | "plugin:shopify/esnext",
|
71 | "plugin:shopify/lodash",
|
72 | "plugin:shopify/mocha"
|
73 | ]
|
74 | }
|
75 | ```
|
76 |
|
77 | ## Provided configurations
|
78 |
|
79 | This plugin provides the following core configurations:
|
80 |
|
81 | - [esnext](lib/config/esnext.js): Use this for anything written with ES2015+ features.
|
82 | - [react](lib/config/react.js): Use this for React projects.
|
83 | - [es5](lib/config/es5.js): Use this for legacy projects.
|
84 |
|
85 | This plugin also provides the following tool-specific configurations, which can be used on top of the core configurations:
|
86 |
|
87 | - [lodash](lib/config/lodash.js): Use this for projects that use [lodash](https://lodash.com).
|
88 | - [mocha](lib/config/mocha.js): Use this for projects that use [mocha](http://mochajs.org)/ [sinon](http://sinonjs.org)/ [chai](http://chaijs.com) for testing.
|
89 | - [ava](lib/config/ava.js): Use this for projects that use the [AVA test runner](https://github.com/sindresorhus/ava).
|
90 | - [flow](lib/config/flow.js): Use this for projects that use [flow](http://flowtype.org) for type checking.
|
91 | - [jquery](lib/config/jquery.js): Use this for projects that use [jQuery](http://jquery.com).
|
92 | - [polaris](lib/config/polaris.js): Use this for projects that use [Shopify’s React Polaris components](https://polaris.shopify.com/components/get-started).
|
93 | - [prettier](lib/config/prettier.js): Use [prettier](https://github.com/prettier/prettier) for consistent formatting. Extending this Shopify's prettier config will [override](https://github.com/prettier/eslint-config-prettier/blob/master/index.js) the default Shopify eslint rules in favor of prettier formatting. Prettier must be installed within your project, as eslint-plugin-shopify does not provide the dependency itself.
|
94 | - [typescript-prettier](lib/config/typescript-prettier.js): Use [prettier](https://github.com/prettier/prettier) on typescript projects. Prettier must be installed within your project, as eslint-plugin-shopify does not provide the dependency itself.
|
95 | - [webpack](lib/config/webpack.js): Use this for projects built by [webpack](https://webpack.js.org/).
|
96 |
|
97 | ### node
|
98 |
|
99 | If you are working on a node module, we also provide the [node configuration](lib/config/esnext.js) for you. Note that this configuration needs to be used in conjunction with one of the core configurations (either `es5` or `esnext`). If you plan to transpile your code using Babel, use the `esnext` config. If you do not plan to do so, the config you choose depends on the version of node you wish to support, and how many ESNext features are natively available in that version. You can see a detailed list of what version of node supports what new JavaScript features by visiting http://node.green.
|
100 |
|
101 | A node project that will use Babel for transpilation would need the following ESLint config:
|
102 |
|
103 | ```json
|
104 | {
|
105 | "extends": [
|
106 | "plugin:shopify/esnext",
|
107 | "plugin:shopify/node"
|
108 | ]
|
109 | }
|
110 | ```
|
111 |
|
112 | ## Plugin-Provided Rules
|
113 |
|
114 | This plugin provides the following custom rules, which are included as appropriate in all core linting configs:
|
115 |
|
116 | - [binary-assignment-parens](docs/rules/binary-assignment-parens.md): Require (or disallow) assignments of binary, boolean-producing expressions to be wrapped in parentheses.
|
117 | - [class-property-semi](docs/rules/class-property-semi.md): Require (or disallow) semicolons for class properties.
|
118 | - [jest/no-vague-titles](docs/rules/jest/no-vague-titles.md): Prevent the usage of vague words in test statements.
|
119 | - [jquery-dollar-sign-reference](docs/rules/jquery-dollar-sign-reference.md): Require that all jQuery objects are assigned to references prefixed with `$`.
|
120 | - [jsx-no-complex-expressions](docs/rules/jsx-no-complex-expressions.md): Disallow complex expressions embedded in in JSX.
|
121 | - [jsx-no-hardcoded-content](docs/rules/jsx-no-hardcoded-content.md): Disallow hardcoded content in JSX.
|
122 | - [jsx-prefer-fragment-wrappers](docs/rules/jsx-prefer-fragment-wrappers.md): Disallow useless wrapping elements in favour of fragment shorthand in JSX.
|
123 | - [no-useless-computed-properties](docs/rules/no-useless-computed-properties.md): Prevent the usage of unnecessary computed properties.
|
124 | - [polaris-no-bare-stack-item](docs/rules/polaris-no-bare-stack-item.md): Disallow the use of Polaris’s `Stack.Item` without any custom props.
|
125 | - [polaris-prefer-sectioned-prop](docs/rules/polaris-prefer-sectioned-prop.md): Prefer the use of the `sectioned` props in Polaris components instead of wrapping all contents in a `Section` component.
|
126 | - [prefer-class-properties](docs/rules/prefer-class-properties.md): Prefer class properties to assignment of literals in constructors.
|
127 | - [prefer-early-return](docs/rules/prefer-early-return.md): Prefer early returns over full-body conditional wrapping in function declarations.
|
128 | - [prefer-module-scope-constants](docs/rules/prefer-module-scope-constants.md): Prefer that screaming snake case variables always be defined using `const`, and always appear at module scope.
|
129 | - [prefer-twine](docs/rules/prefer-twine.md): Prefer Twine over Bindings as the name for twine imports.
|
130 | - [react-initialize-state](docs/rules/react-initialize-state.md): Require that React component state be initialized when it has a non-empty type.
|
131 | - [react-no-multiple-render-methods](docs/rules/react-no-multiple-render-methods.md): Disallow multiple render methods in React component classes.
|
132 | - [react-prefer-private-members](docs/rules/react-prefer-private-members.md): Prefer all non-React-specific members be marked private in React class components.
|
133 | - [react-type-state](docs/rules/react-type-state.md): Require that React component state be typed in TypeScript.
|
134 | - [restrict-full-import](docs/rules/restrict-full-import.md): Prevent importing the entirety of a package.
|
135 | - [sinon-no-restricted-features](docs/rules/sinon-no-restricted-features.md): Restrict the use of specified sinon features.
|
136 | - [sinon-prefer-meaningful-assertions](docs/rules/sinon-prefer-meaningful-assertions.md): Require the use of meaningful sinon assertions through sinon.assert or sinon-chai.
|
137 | - [strict-component-boundaries](docs/rules/strict-component-boundaries.md): Prevent module imports between components.
|
138 | - [typescipt/prefer-pascal-case-enums](docs/rules/typescipt/prefer-pascal-case-enums.md): Prefer TypeScript enums be defined using Pascal case.
|
139 | - [typescipt/prefer-singular-enums](docs/rules/typescipt/prefer-singular-enums.md): Prefer TypeScript enums be singular.
|
140 | - [webpack/no-unnamed-dynamic-imports](docs/rules/webpack/no-unnamed-dynamic-imports.md): Require that all dynamic imports contain a `webpackChunkName` comment.
|
141 |
|
142 | ## Creating New Rules
|
143 |
|
144 | The easiest way to add new rules is to use the [ESLint Yeoman generator](https://www.npmjs.com/package/generator-eslint). Running `yo eslint:rule` from the root of this project should add the required main file, docs, and test for your new rules. Make sure that these are all filled out and consistent with the other rules before merging. All tests can be run using `npm test`.
|
145 |
|
146 | [npm-url]: https://npmjs.org/package/eslint-plugin-shopify
|
147 | [npm-image]: http://img.shields.io/npm/v/eslint-plugin-shopify.svg?style=shield
|