|
| 1 | +--- |
| 2 | +stage: accepted |
| 3 | +start-date: 2023-01-07T00:00:00.000Z |
| 4 | +release-date: # In format YYYY-MM-DDT00:00:00.000Z |
| 5 | +release-versions: |
| 6 | +teams: # delete teams that aren't relevant |
| 7 | + - cli |
| 8 | +prs: |
| 9 | + accepted: https://github.com/emberjs/rfcs/pull/894 |
| 10 | +project-link: |
| 11 | +suite: |
| 12 | +--- |
| 13 | + |
| 14 | +<!--- |
| 15 | +Directions for above: |
| 16 | +
|
| 17 | +stage: Leave as is |
| 18 | +start-date: Fill in with today's date, 2032-12-01T00:00:00.000Z |
| 19 | +release-date: Leave as is |
| 20 | +release-versions: Leave as is |
| 21 | +teams: Include only the [team(s)](README.md#relevant-teams) for which this RFC applies |
| 22 | +prs: |
| 23 | + accepted: Fill this in with the URL for the Proposal RFC PR |
| 24 | +project-link: Leave as is |
| 25 | +suite: Leave as is |
| 26 | +--> |
| 27 | + |
| 28 | +# Add eslint-plugin-import to ember-cli blueprint |
| 29 | + |
| 30 | +## Summary |
| 31 | + |
| 32 | +This RFC proposes adding [eslint-plugin-import](https://github.com/import-js/eslint-plugin-import) to the blueprints |
| 33 | +that back `ember new` and `ember addon`. |
| 34 | + |
| 35 | +## Motivation |
| 36 | + |
| 37 | +Ember apps and addons already come with a number of linting plugins targeting various areas of the code: |
| 38 | + |
| 39 | +* [ember-template-lint](https://github.com/ember-template-lint/ember-template-lint) - Handlebars / templating best practices |
| 40 | +* [eslint](https://eslint.org/) - General JavaScript best practices |
| 41 | +* [eslint-plugin-ember](https://github.com/ember-cli/eslint-plugin-ember) - Ember best practices |
| 42 | +* [eslint-plugin-node](https://github.com/mysticatea/eslint-plugin-node) - Node best practices |
| 43 | +* [eslint-plugin-qunit](https://github.com/platinumazure/eslint-plugin-qunit) - Testing best practices ([RFC](https://github.com/emberjs/rfcs/blob/master/text/0702-eslint-plugin-qunit.md)) |
| 44 | +* [prettier](https://prettier.io/) - Code styling ([RFC](https://github.com/emberjs/rfcs/blob/master/text/0628-prettier.md)) |
| 45 | +* [stylelint](https://stylelint.io/) - CSS styling ([RFC](https://github.com/emberjs/rfcs/blob/master/text/0814-stylelint.md)) |
| 46 | + |
| 47 | +But there's an important aspect of Ember apps that is not yet targeted by specialized linting: import statements. Imports are present in almost every file, and various problems or even crashes can result from invalid imports. |
| 48 | + |
| 49 | +[eslint-plugin-import](https://github.com/import-js/eslint-plugin-import) is one of the top few, most popular ESLint plugins, with around 8 million dependents on GitHub, 4.5k stars on GitHub, and 17 million weekly downloads on NPM, as of this writing. It contains 40+ rules for detecting problems with imports as well as enforcing usage preferences. |
| 50 | + |
| 51 | +## Detailed design |
| 52 | + |
| 53 | +The general idea is that we will update the `app` and `addon` blueprints to add the eslint-plugin-import package to the `package.json`, and update the linting configuration to extend the relevant configurations. |
| 54 | + |
| 55 | +### Packages |
| 56 | + |
| 57 | +The following packages will be added to the `package.json` of both `app` and `addon` blueprints: |
| 58 | + |
| 59 | +* [eslint-plugin-import](https://www.npmjs.com/package/eslint-plugin-import) |
| 60 | + |
| 61 | +### Configuration changes |
| 62 | + |
| 63 | +The ESLint config file that is generated will be updated to extend the `recommended` linting configuration: |
| 64 | + |
| 65 | +```js |
| 66 | +// .eslintrc.js |
| 67 | +{ |
| 68 | + extends: ['plugin:import/recommended'], |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +We can also configure the [ember-cli-typescript](https://github.com/typed-ember/ember-cli-typescript) blueprint to enable the `typescript` config, but only for TypeScript files: |
| 73 | + |
| 74 | +```js |
| 75 | +// .eslintrc.js |
| 76 | +{ |
| 77 | + overrides: [ |
| 78 | + { |
| 79 | + files: ['*.ts'], |
| 80 | + extends: ['plugin:import/typescript'], |
| 81 | + }, |
| 82 | + ], |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +Note that these configurations enable only a handful of key rules from the plugin's 40+ rules. We may optionally choose to individually enable additional rules that we determine to be particularly valuable for Ember apps, either in this RFC, or in the future. But regardless, getting the plugin with its basic linting setup into place right now is worthwhile on its own, as this will also make it much easier for users to enable rules on their own too. |
| 87 | + |
| 88 | +### Resolver |
| 89 | + |
| 90 | +There are a few issues to fix with the [import/no-unresolved](https://github.com/import-js/eslint-plugin-import/blob/HEAD/docs/rules/no-unresolved.md) rule. |
| 91 | + |
| 92 | +**TODO:** These issues, described in more detail below, still need to be addressed, and I'm interested to hear feedback on solving them. Worst case, we can disable the rule for now, and enable it once the issues are resolved: |
| 93 | + |
| 94 | +```js |
| 95 | +// .eslintrc.js |
| 96 | +{ |
| 97 | + rules: { |
| 98 | + 'import/no-unresolved': 'off', |
| 99 | + }, |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +#### Ember package imports |
| 104 | + |
| 105 | +First, there are some false positives with Ember packages, such as: |
| 106 | + |
| 107 | +```js |
| 108 | +import { service } from '@ember/service'; |
| 109 | +// Triggers violation: Unable to resolve path to module '@ember/service' |
| 110 | + |
| 111 | +import Ember from 'ember'; |
| 112 | +// Triggers violation: Unable to resolve path to module 'ember' |
| 113 | +``` |
| 114 | + |
| 115 | +It's possible we can ignore these using the recommendation from [eslint-import-resolver-ember](https://github.com/gabrielcsapo/eslint-import-resolver-ember#usage): |
| 116 | + |
| 117 | +```js |
| 118 | +// .eslintrc.js |
| 119 | +{ |
| 120 | + rules: { |
| 121 | + 'import/no-unresolved': ['error', { ignore: ['^\@ember'] }], |
| 122 | + }, |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +But ideally, the resolver we use will correctly resolve these packages without needing to ignore them. |
| 127 | + |
| 128 | +#### App-name prefix imports |
| 129 | + |
| 130 | +Second, there are false positives with import paths prefixed with the current Ember app name, such as this one: |
| 131 | + |
| 132 | +```js |
| 133 | +// app/app.js |
| 134 | +import config from 'my-app-name/config/environment'; |
| 135 | +// Triggers violation: Unable to resolve path to module 'my-app-name/config/environment' |
| 136 | +``` |
| 137 | + |
| 138 | +There's a resolver [eslint-import-resolver-ember](https://github.com/gabrielcsapo/eslint-import-resolver-ember) that is supposed to address this: |
| 139 | + |
| 140 | +```js |
| 141 | +// .eslintrc.js |
| 142 | +{ |
| 143 | + settings: { |
| 144 | + 'import/resolver': 'eslint-import-resolver-ember', |
| 145 | + }, |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +Based on some initial testing, this configuration isn't working for me in a new Ember app. I'm not sure if it's a bug in the resolver, which hasn't been updated since 2018, or if there's some other setup needed. Either way, this resolver will likely require some modernization, and testing to ensure it works with TypeScript and different app/addon/engine structures. We may want to begin maintaining it by moving it to the [ember-cli](https://github.com/ember-cli) organization, assuming it's a good starting point and owner is interested. |
| 150 | + |
| 151 | +## How we teach this |
| 152 | + |
| 153 | +We do not currently discuss linting in either [guides.emberjs.com](https://guides.emberjs.com/) or [cli.emberjs.com](https://cli.emberjs.com/). |
| 154 | + |
| 155 | +Users will be able to find documentation for the new rules on the [eslint-plugin-import](https://github.com/import-js/eslint-plugin-import) GitHub page. Many IDEs will provide a link directly to the rule documentation on highlighted lint violations. |
| 156 | + |
| 157 | +Any violations of the new rules will be detected by our existing `npm run lint:js` script and in some cases autofixed by `npm run lint:js:fix`. |
| 158 | + |
| 159 | +## Drawbacks |
| 160 | + |
| 161 | +For those introducing eslint-plugin-import to an existing codebase, the largest drawback is generally the initial cost of fixing linting violations. This can be mitigated by individually disabling noisy lint rules and working to fix violations over time. But note that this RFC is more likely to impact newly-generated applications where there will be no existing lint violations to fix, as opposed to existing applications. |
| 162 | + |
| 163 | +Users not interested in linting imports can safely disable or remove eslint-plugin-import. |
| 164 | + |
| 165 | +## Alternatives |
| 166 | + |
| 167 | +The implementation, aside from the [resolver](#resolver) issues, is fairly straightforward. |
| 168 | + |
| 169 | +The main alternative is to do nothing, and leave it up to users to add eslint-plugin-import on their own. But if we are able to install and correctly setup this plugin for users, it will enable many more users to take advantage of this additional type of linting. |
| 170 | + |
| 171 | +## Unresolved questions |
| 172 | + |
| 173 | +* The [resolver](#resolver) issues are still outstanding. |
| 174 | +* This needs further testing with TypeScript and various app/addon/engine structures. |
0 commit comments