Skip to content

Commit

Permalink
Merge branch 'master' into support-ts-resolver-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
joaovieira committed Dec 13, 2019
2 parents 1ed020e + 4e8960d commit f46dc45
Show file tree
Hide file tree
Showing 51 changed files with 1,242 additions and 144 deletions.
122 changes: 86 additions & 36 deletions CHANGELOG.md

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -402,6 +402,20 @@ settings:
[`eslint_d`]: https://www.npmjs.com/package/eslint_d
[`eslint-loader`]: https://www.npmjs.com/package/eslint-loader

#### `import/internal-regex`

A regex for packages should be treated as internal. Useful when you are utilizing a monorepo setup or developing a set of packages that depend on each other.

By default, any package referenced from [`import/external-module-folders`](#importexternal-module-folders) will be considered as "external", including packages in a monorepo like yarn workspace or lerna emvironentment. If you want to mark these packages as "internal" this will be useful.

For example, if you pacakges in a monorepo are all in `@scope`, you can configure `import/internal-regex` like this

```yaml
# .eslintrc.yml
settings:
import/internal-regex: ^@scope/
```


## SublimeLinter-eslint

Expand Down
19 changes: 17 additions & 2 deletions docs/rules/no-commonjs.md
Expand Up @@ -27,15 +27,30 @@ If `allowRequire` option is set to `true`, `require` calls are valid:

```js
/*eslint no-commonjs: [2, { allowRequire: true }]*/
var mod = require('./mod');
```

but `module.exports` is reported as usual.

### Allow conditional require

By default, conditional requires are allowed:

```js
var a = b && require("c")

if (typeof window !== "undefined") {
require('that-ugly-thing');
}

var fs = null;
try {
fs = require("fs")
} catch (error) {}
```

but `module.exports` is reported as usual.
If the `allowConditionalRequire` option is set to `false`, they will be reported.

This is useful for conditional requires.
If you don't rely on synchronous module loading, check out [dynamic import](https://github.com/airbnb/babel-plugin-dynamic-import-node).

### Allow primitive modules
Expand Down
25 changes: 25 additions & 0 deletions docs/rules/no-duplicates.md
Expand Up @@ -36,6 +36,31 @@ The motivation is that this is likely a result of two developers importing diffe
names from the same module at different times (and potentially largely different
locations in the file.) This rule brings both (or n-many) to attention.

### Query Strings

By default, this rule ignores query strings (i.e. paths followed by a question mark), and thus imports from `./mod?a` and `./mod?b` will be considered as duplicates. However you can use the option `considerQueryString` to handle them as different (primarily because browsers will resolve those imports differently).

Config:

```json
"import/no-duplicates": ["error", {"considerQueryString": true}]
```

And then the following code becomes valid:
```js
import minifiedMod from './mod?minify'
import noCommentsMod from './mod?comments=0'
import originalMod from './mod'
```

It will still catch duplicates when using the same module and the exact same query string:
```js
import SomeDefaultClass from './mod?minify'

// This is invalid, assuming `./mod` and `./mod.js` are the same target:
import * from './mod.js?minify'
```

## When Not To Use It

If the core ESLint version is good enough (i.e. you're _not_ using Flow and you _are_ using [`import/extensions`](./extensions.md)), keep it and don't use this.
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/no-useless-path-segments.md
Expand Up @@ -73,3 +73,7 @@ import "./pages/index.js"; // should be "./pages" (auto-fixable)
```

Note: `noUselessIndex` only avoids ambiguous imports for `.js` files if you haven't specified other resolved file extensions. See [Settings: import/extensions](https://github.com/benmosher/eslint-plugin-import#importextensions) for details.

### commonjs

When set to `true`, this rule checks CommonJS imports. Default to `false`.
64 changes: 63 additions & 1 deletion docs/rules/order.md
Expand Up @@ -94,8 +94,33 @@ You can set the options like this:
"import/order": ["error", {"groups": ["index", "sibling", "parent", "internal", "external", "builtin"]}]
```

### `newlines-between: [ignore|always|always-and-inside-groups|never]`:
### `pathGroups: [array of objects]`:

To be able so group by paths mostly needed with aliases pathGroups can be defined.

Properties of the objects

| property | required | type | description |
|----------------|:--------:|--------|---------------|
| pattern | x | string | minimatch pattern for the paths to be in this group (will not be used for builtins or externals) |
| patternOptions | | object | options for minimatch, default: { nocomment: true } |
| group | x | string | one of the allowed groups, the pathGroup will be positioned relative to this group |
| position | | string | defines where around the group the pathGroup will be positioned, can be 'after' or 'before', if not provided pathGroup will be positioned like the group |

```json
{
"import/order": ["error", {
"pathGroups": [
{
"pattern": "~/**",
"group": "external"
}
]
}]
}
```

### `newlines-between: [ignore|always|always-and-inside-groups|never]`:

Enforces or forbids new lines between import groups:

Expand Down Expand Up @@ -164,8 +189,45 @@ import index from './';
import sibling from './foo';
```

### `alphabetize: {order: asc|desc|ignore}`:

Sort the order within each group in alphabetical manner based on **import path**:

- `order`: use `asc` to sort in ascending order, and `desc` to sort in descending order (default: `ignore`).

Example setting:
```js
alphabetize: {
order: 'asc', /* sort in ascending order. Options: ['ignore', 'asc', 'desc'] */
}
```

This will fail the rule check:

```js
/* eslint import/order: ["error", {"alphabetize": true}] */
import React, { PureComponent } from 'react';
import aTypes from 'prop-types';
import { compose, apply } from 'xcompose';
import * as classnames from 'classnames';
```

While this will pass:

```js
/* eslint import/order: ["error", {"alphabetize": true}] */
import * as classnames from 'classnames';
import aTypes from 'prop-types';
import React, { PureComponent } from 'react';
import { compose, apply } from 'xcompose';
```

## Related

- [`import/external-module-folders`] setting

- [`import/internal-regex`] setting

[`import/external-module-folders`]: ../../README.md#importexternal-module-folders

[`import/internal-regex`]: ../../README.md#importinternal-regex
3 changes: 3 additions & 0 deletions memo-parser/package.json
Expand Up @@ -26,5 +26,8 @@
"homepage": "https://github.com/benmosher/eslint-plugin-import#readme",
"peerDependencies": {
"eslint": ">=3.5.0"
},
"dependencies": {
"eslint-module-utils": "^2.5.0"
}
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-import",
"version": "2.18.2",
"version": "2.19.1",
"description": "Import with sanity.",
"engines": {
"node": ">=4"
Expand Down
2 changes: 1 addition & 1 deletion resolvers/node/package.json
Expand Up @@ -29,7 +29,7 @@
"homepage": "https://github.com/benmosher/eslint-plugin-import",
"dependencies": {
"debug": "^2.6.9",
"resolve": "^1.10.0"
"resolve": "^1.13.1"
},
"devDependencies": {
"chai": "^3.5.0",
Expand Down
7 changes: 7 additions & 0 deletions resolvers/webpack/CHANGELOG.md
Expand Up @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## Unreleased

## 0.12.0 - 2019-12-07

### Added
- [New] enable passing cwd as an option to `eslint-import-resolver-webpack` ([#1503], thanks [@Aghassi])

## 0.11.1 - 2019-04-13

### Fixed
Expand Down Expand Up @@ -117,6 +122,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- `interpret` configs (such as `.babel.js`).
Thanks to [@gausie] for the initial PR ([#164], ages ago! 😅) and [@jquense] for tests ([#278]).

[#1503]: https://github.com/benmosher/eslint-plugin-import/pull/1503
[#1297]: https://github.com/benmosher/eslint-plugin-import/pull/1297
[#1261]: https://github.com/benmosher/eslint-plugin-import/pull/1261
[#1220]: https://github.com/benmosher/eslint-plugin-import/pull/1220
Expand Down Expand Up @@ -166,3 +172,4 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
[@mattkrick]: https://github.com/mattkrick
[@idudinov]: https://github.com/idudinov
[@keann]: https://github.com/keann
[@Aghassi]: https://github.com/Aghassi
28 changes: 22 additions & 6 deletions resolvers/webpack/index.js
Expand Up @@ -20,7 +20,15 @@ exports.interfaceVersion = 2
* resolveImport('./foo', '/Users/ben/bar.js') => '/Users/ben/foo.js'
* @param {string} source - the module to resolve; i.e './some-module'
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
* TODO: take options as a third param, with webpack config file name
* @param {object} settings - the webpack config file name, as well as cwd
* @example
* options: {
* // Path to the webpack config
* config: 'webpack.config.js',
* // Path to be used to determine where to resolve webpack from
* // (may differ from the cwd in some cases)
* cwd: process.cwd()
* }
* @return {string?} the resolved path to source, undefined if not resolved, or null
* if resolved to a non-FS resource (i.e. script tag at page load)
*/
Expand All @@ -41,6 +49,11 @@ exports.resolve = function (source, file, settings) {
var webpackConfig

var configPath = get(settings, 'config')
/**
* Attempt to set the current working directory.
* If none is passed, default to the `cwd` where the config is located.
*/
, cwd = get(settings, 'cwd')
, configIndex = get(settings, 'config-index')
, env = get(settings, 'env')
, argv = get(settings, 'argv', {})
Expand Down Expand Up @@ -114,7 +127,7 @@ exports.resolve = function (source, file, settings) {
}

// otherwise, resolve "normally"
var resolveSync = getResolveSync(configPath, webpackConfig)
var resolveSync = getResolveSync(configPath, webpackConfig, cwd)

try {
return { found: true, path: resolveSync(path.dirname(file), source) }
Expand All @@ -130,13 +143,13 @@ exports.resolve = function (source, file, settings) {

var MAX_CACHE = 10
var _cache = []
function getResolveSync(configPath, webpackConfig) {
function getResolveSync(configPath, webpackConfig, cwd) {
var cacheKey = { configPath: configPath, webpackConfig: webpackConfig }
var cached = find(_cache, function (entry) { return isEqual(entry.key, cacheKey) })
if (!cached) {
cached = {
key: cacheKey,
value: createResolveSync(configPath, webpackConfig),
value: createResolveSync(configPath, webpackConfig, cwd),
}
// put in front and pop last item
if (_cache.unshift(cached) > MAX_CACHE) {
Expand All @@ -146,15 +159,18 @@ function getResolveSync(configPath, webpackConfig) {
return cached.value
}

function createResolveSync(configPath, webpackConfig) {
function createResolveSync(configPath, webpackConfig, cwd) {
var webpackRequire
, basedir = null

if (typeof configPath === 'string') {
basedir = path.dirname(configPath)
// This can be changed via the settings passed in when defining the resolver
basedir = cwd || configPath
log(`Attempting to load webpack path from ${basedir}`)
}

try {
// Attempt to resolve webpack from the given `basedir`
var webpackFilename = resolve.sync('webpack', { basedir, preserveSymlinks: false })
var webpackResolveOpts = { basedir: path.dirname(webpackFilename), preserveSymlinks: false }

Expand Down
16 changes: 8 additions & 8 deletions resolvers/webpack/package.json
@@ -1,6 +1,6 @@
{
"name": "eslint-import-resolver-webpack",
"version": "0.11.1",
"version": "0.12.0",
"description": "Resolve paths to dependencies, given a webpack.config.js. Plugin for eslint-plugin-import.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -31,15 +31,15 @@
"homepage": "https://github.com/benmosher/eslint-plugin-import/tree/master/resolvers/webpack",
"dependencies": {
"array-find": "^1.0.0",
"debug": "^2.6.8",
"enhanced-resolve": "~0.9.0",
"debug": "^2.6.9",
"enhanced-resolve": "^0.9.1",
"find-root": "^1.1.0",
"has": "^1.0.1",
"interpret": "^1.0.0",
"lodash": "^4.17.4",
"has": "^1.0.3",
"interpret": "^1.2.0",
"lodash": "^4.17.15",
"node-libs-browser": "^1.0.0 || ^2.0.0",
"resolve": "^1.10.0",
"semver": "^5.3.0"
"resolve": "^1.13.1",
"semver": "^5.7.1"
},
"peerDependencies": {
"eslint-plugin-import": ">=1.4.0",
Expand Down
9 changes: 9 additions & 0 deletions resolvers/webpack/test/example.js
@@ -0,0 +1,9 @@
var path = require('path')

var resolve = require('../index').resolve

var file = path.join(__dirname, 'files', 'src', 'dummy.js')

var webpackDir = path.join(__dirname, "different-package-location")

console.log(resolve('main-module', file, { config: "webpack.config.js", cwd: webpackDir}))
11 changes: 10 additions & 1 deletion resolvers/webpack/test/root.js
Expand Up @@ -6,6 +6,7 @@ var resolve = require('../index').resolve


var file = path.join(__dirname, 'files', 'src', 'dummy.js')
var webpackDir = path.join(__dirname, "different-package-location")

describe("root", function () {
it("works", function () {
Expand All @@ -32,5 +33,13 @@ describe("root", function () {
.property('path')
.to.equal(path.join(__dirname, 'files', 'bower_components', 'typeahead.js'))
})

it("supports passing a different directory to load webpack from", function () {
// Webpack should still be able to resolve the config here
expect(resolve('main-module', file, { config: "webpack.config.js", cwd: webpackDir}))
.property('path')
.to.equal(path.join(__dirname, 'files', 'src', 'main-module.js'))
expect(resolve('typeahead', file, { config: "webpack.config.js", cwd: webpackDir}))
.property('path')
.to.equal(path.join(__dirname, 'files', 'bower_components', 'typeahead.js'))
})
})

0 comments on commit f46dc45

Please sign in to comment.