Skip to content

Commit

Permalink
Merge commit 'ff50964df1234515bc6f7969b90d87ada1982008' into add-glob…
Browse files Browse the repository at this point in the history
…-to-except

# Conflicts:
#	package.json
  • Loading branch information
KevinHerklotz committed Jan 8, 2021
2 parents e607ece + ff50964 commit 6b11201
Show file tree
Hide file tree
Showing 38 changed files with 353 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .coveralls.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
repo_token: fW3moW39Z8pKOgqTnUMT68DnNCd2SM8Ly
repo_token: fW3moW39Z8pKOgqTnUMT68DnNCd2SM8Ly
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## [Unreleased]

### Fixed
- [`export`]/TypeScript: properly detect export specifiers as children of a TS module block ([#1889], thanks [@andreubotella])
- [`order`]: ignore non-module-level requires ([#1940], thanks [@golopot])
- [`no-webpack-loader-syntax`]/TypeScript: avoid crash on missing name ([#1947], thanks @leonardodino)
- [`no-extraneous-dependencies`]: Add package.json cache ([#1948], thanks @fa93hws)
- [`prefer-default-export`]: handle empty array destructuring ([#1965], thanks @ljharb)

## [2.22.1] - 2020-09-27
### Fixed
- [`default`]/TypeScript: avoid crash on `export =` with a MemberExpression ([#1841], thanks [@ljharb])
- [`extensions`]/importType: Fix @/abc being treated as scoped module ([#1854], thanks [@3nuc])
Expand Down Expand Up @@ -732,6 +740,11 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#1965]: https://github.com/benmosher/eslint-plugin-import/issues/1965
[#1948]: https://github.com/benmosher/eslint-plugin-import/pull/1948
[#1947]: https://github.com/benmosher/eslint-plugin-import/pull/1947
[#1940]: https://github.com/benmosher/eslint-plugin-import/pull/1940
[#1889]: https://github.com/benmosher/eslint-plugin-import/pull/1889
[#1878]: https://github.com/benmosher/eslint-plugin-import/pull/1878
[#1854]: https://github.com/benmosher/eslint-plugin-import/issues/1854
[#1848]: https://github.com/benmosher/eslint-plugin-import/pull/1848
Expand Down Expand Up @@ -1040,7 +1053,8 @@ for info on changes for earlier releases.
[#119]: https://github.com/benmosher/eslint-plugin-import/issues/119
[#89]: https://github.com/benmosher/eslint-plugin-import/issues/89

[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v2.22.0...HEAD
[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v2.22.1...HEAD
[2.22.1]: https://github.com/benmosher/eslint-plugin-import/compare/v2.22.0...v2.22.1
[2.22.0]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.1...v2.22.0
[2.21.2]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.1...v2.21.2
[2.21.1]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.0...v2.21.1
Expand Down Expand Up @@ -1277,3 +1291,4 @@ for info on changes for earlier releases.
[@foray1010]: https://github.com/foray1010
[@tomprats]: https://github.com/tomprats
[@straub]: https://github.com/straub
[@andreubotella]: https://github.com/andreubotella
12 changes: 6 additions & 6 deletions docs/rules/no-unused-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Note: dynamic imports are currently not supported.

In order for this plugin to work, one of the options `missingExports` or `unusedExports` must be enabled (see "Options" section below). In the future, these options will be enabled by default (see https://github.com/benmosher/eslint-plugin-import/issues/1324)

Example:
Example:
```
"rules: {
...otherRules,
Expand All @@ -27,24 +27,24 @@ This rule takes the following option:
- **`missingExports`**: if `true`, files without any exports are reported (defaults to `false`)
- **`unusedExports`**: if `true`, exports without any static usage within other modules are reported (defaults to `false`)
- `src`: an array with files/paths to be analyzed. It only applies to unused exports. Defaults to `process.cwd()`, if not provided
- `ignoreExports`: an array with files/paths for which unused exports will not be reported (e.g module entry points in a published package)
- `ignoreExports`: an array with files/paths for which unused exports will not be reported (e.g module entry points in a published package)


### Example for missing exports
#### The following will be reported
```js
const class MyClass { /*...*/ }
const class MyClass { /*...*/ }

function makeClass() { return new MyClass(...arguments) }
```

#### The following will not be reported

```js
export default function () { /*...*/ }
export default function () { /*...*/ }
```
```js
export const foo = function () { /*...*/ }
export const foo = function () { /*...*/ }
```
```js
export { foo, bar }
Expand All @@ -61,7 +61,7 @@ import { f } from 'file-b'
import * as fileC from 'file-c'
export { default, i0 } from 'file-d' // both will be reported

export const j = 99 // will be reported
export const j = 99 // will be reported
```
and file-d:
```js
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-import",
"version": "2.22.0",
"version": "2.22.1",
"description": "Import with sanity.",
"engines": {
"node": ">=4"
Expand Down Expand Up @@ -67,17 +67,17 @@
"babel-register": "^6.26.0",
"babylon": "^6.18.0",
"chai": "^4.2.0",
"coveralls": "^3.0.6",
"coveralls": "^3.1.0",
"cross-env": "^4.0.0",
"eslint": "^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0",
"eslint-import-resolver-node": "file:./resolvers/node",
"eslint-import-resolver-typescript": "^1.0.2",
"eslint-import-resolver-typescript": "^1.1.1",
"eslint-import-resolver-webpack": "file:./resolvers/webpack",
"eslint-import-test-order-redirect": "file:./tests/files/order-redirect",
"eslint-module-utils": "file:./utils",
"eslint-plugin-eslint-plugin": "^2.2.1",
"eslint-plugin-eslint-plugin": "^2.3.0",
"eslint-plugin-import": "2.x",
"eslint-plugin-json": "^2.1.1",
"eslint-plugin-json": "^2.1.2",
"fs-copy-file-sync": "^1.1.1",
"glob": "^7.1.6",
"in-publish": "^2.0.1",
Expand All @@ -102,10 +102,11 @@
"contains-path": "^0.1.0",
"debug": "^2.6.9",
"doctrine": "1.5.0",
"eslint-import-resolver-node": "^0.3.3",
"eslint-import-resolver-node": "^0.3.4",
"eslint-module-utils": "^2.6.0",
"has": "^1.0.3",
"is-glob": "^4.0.1",
"is-core-module": "^1.0.2",
"minimatch": "^3.0.4",
"object.values": "^1.1.1",
"read-pkg-up": "^2.0.0",
Expand Down
5 changes: 5 additions & 0 deletions resolvers/webpack/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## Unreleased

## 0.13.0 - 2020-09-27

### Breaking
- [Breaking] Allow to resolve config path relative to working directory (#1276)

## 0.12.2 - 2020-06-16

### Fixed
Expand Down
12 changes: 11 additions & 1 deletion resolvers/webpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ settings:
config: 'webpack.dev.config.js'
```
or with explicit config file name:
or with explicit config file index:
```yaml
---
Expand All @@ -53,6 +53,16 @@ settings:
config-index: 1 # take the config at index 1
```
or with explicit config file path relative to your projects's working directory:
```yaml
---
settings:
import/resolver:
webpack:
config: './configs/webpack.dev.config.js'
```
or with explicit config object:
```yaml
Expand Down
12 changes: 8 additions & 4 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var findRoot = require('find-root')
, find = require('array-find')
, interpret = require('interpret')
, fs = require('fs')
, coreLibs = require('node-libs-browser')
, isCore = require('is-core-module')
, resolve = require('resolve')
, semver = require('semver')
, has = require('has')
Expand Down Expand Up @@ -48,7 +48,7 @@ exports.resolve = function (source, file, settings) {

var webpackConfig

var configPath = get(settings, 'config')
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.
Expand All @@ -59,6 +59,10 @@ exports.resolve = function (source, file, settings) {
, argv = get(settings, 'argv', {})
, packageDir

var configPath = typeof _configPath === 'string' && _configPath.startsWith('.')
? path.resolve(_configPath)
: _configPath

log('Config path from settings:', configPath)

// see if we've got a config path, a config object, an array of config objects or a config function
Expand Down Expand Up @@ -138,8 +142,8 @@ exports.resolve = function (source, file, settings) {
try {
return { found: true, path: resolveSync(path.dirname(file), source) }
} catch (err) {
if (source in coreLibs) {
return { found: true, path: coreLibs[source] }
if (isCore(source)) {
return { found: true, path: null }
}

log('Error during module resolution:', err)
Expand Down
4 changes: 2 additions & 2 deletions resolvers/webpack/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-import-resolver-webpack",
"version": "0.12.2",
"version": "0.13.0",
"description": "Resolve paths to dependencies, given a webpack.config.js. Plugin for eslint-plugin-import.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -38,8 +38,8 @@
"find-root": "^1.1.0",
"has": "^1.0.3",
"interpret": "^1.2.0",
"is-core-module": "^2.0.0",
"lodash": "^4.17.15",
"node-libs-browser": "^1.0.0 || ^2.0.0",
"resolve": "^1.13.1",
"semver": "^5.7.1"
},
Expand Down
8 changes: 8 additions & 0 deletions resolvers/webpack/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ describe("config", function () {
.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'))
})

it("finds config object when config uses a path relative to working dir", function () {
var settings = {
config: './test/files/some/absolute.path.webpack.config.js',
}
expect(resolve('foo', file, settings)).to.have.property('path')
.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'))
})

it("finds the first config with a resolve section when config is an array of config objects", function () {
var settings = {
config: require(path.join(__dirname, './files/webpack.config.multiple.js')),
Expand Down
4 changes: 2 additions & 2 deletions src/core/importType.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import coreModules from 'resolve/lib/core'
import isCoreModule from 'is-core-module'

import resolve from 'eslint-module-utils/resolve'

Expand All @@ -20,7 +20,7 @@ export function isBuiltIn(name, settings, path) {
if (path || !name) return false
const base = baseModule(name)
const extras = (settings && settings['import/core-modules']) || []
return coreModules[base] || extras.indexOf(base) > -1
return isCoreModule(base) || extras.indexOf(base) > -1
}

function isExternalPath(path, name, settings) {
Expand Down
6 changes: 5 additions & 1 deletion src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ module.exports = {
return {
'ExportDefaultDeclaration': (node) => addNamed('default', node, getParent(node)),

'ExportSpecifier': (node) => addNamed(node.exported.name, node.exported, getParent(node)),
'ExportSpecifier': (node) => addNamed(
node.exported.name,
node.exported,
getParent(node.parent)
),

'ExportNamedDeclaration': function (node) {
if (node.declaration == null) return
Expand Down
13 changes: 10 additions & 3 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import moduleVisitor from 'eslint-module-utils/moduleVisitor'
import importType from '../core/importType'
import docsUrl from '../docsUrl'

const depFieldCache = new Map()

function hasKeys(obj = {}) {
return Object.keys(obj).length > 0
}
Expand Down Expand Up @@ -49,9 +51,14 @@ function getDependencies(context, packageDir) {
if (paths.length > 0) {
// use rule config to find package.json
paths.forEach(dir => {
const _packageContent = extractDepFields(
JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'))
)
const packageJsonPath = path.join(dir, 'package.json')
if (!depFieldCache.has(packageJsonPath)) {
const depFields = extractDepFields(
JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
)
depFieldCache.set(packageJsonPath, depFields)
}
const _packageContent = depFieldCache.get(packageJsonPath)
Object.keys(packageContent).forEach(depsKey =>
Object.assign(packageContent[depsKey], _packageContent[depsKey])
)
Expand Down
4 changes: 0 additions & 4 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ const IMPORT_DEFAULT_SPECIFIER = 'ImportDefaultSpecifier'
const VARIABLE_DECLARATION = 'VariableDeclaration'
const FUNCTION_DECLARATION = 'FunctionDeclaration'
const CLASS_DECLARATION = 'ClassDeclaration'
const INTERFACE_DECLARATION = 'InterfaceDeclaration'
const TYPE_ALIAS = 'TypeAlias'
const TS_INTERFACE_DECLARATION = 'TSInterfaceDeclaration'
const TS_TYPE_ALIAS_DECLARATION = 'TSTypeAliasDeclaration'
const TS_ENUM_DECLARATION = 'TSEnumDeclaration'
Expand All @@ -75,8 +73,6 @@ function forEachDeclarationIdentifier(declaration, cb) {
if (
declaration.type === FUNCTION_DECLARATION ||
declaration.type === CLASS_DECLARATION ||
declaration.type === INTERFACE_DECLARATION ||
declaration.type === TYPE_ALIAS ||
declaration.type === TS_INTERFACE_DECLARATION ||
declaration.type === TS_TYPE_ALIAS_DECLARATION ||
declaration.type === TS_ENUM_DECLARATION
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-webpack-loader-syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import isStaticRequire from '../core/staticRequire'
import docsUrl from '../docsUrl'

function reportIfNonStandard(context, node, name) {
if (name.indexOf('!') !== -1) {
if (name && name.indexOf('!') !== -1) {
context.report(node, `Unexpected '!' in '${name}'. ` +
'Do not use import syntax to configure webpack loaders.'
)
Expand Down
38 changes: 16 additions & 22 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,21 @@ function registerNode(context, importEntry, ranks, imported, excludedImportTypes
}
}

function isInVariableDeclarator(node) {
return node &&
(node.type === 'VariableDeclarator' || isInVariableDeclarator(node.parent))
function isModuleLevelRequire(node) {
let n = node
// Handle cases like `const baz = require('foo').bar.baz`
// and `const foo = require('foo')()`
while (
(n.parent.type === 'MemberExpression' && n.parent.object === n) ||
(n.parent.type === 'CallExpression' && n.parent.callee === n)
) {
n = n.parent
}
return (
n.parent.type === 'VariableDeclarator' &&
n.parent.parent.type === 'VariableDeclaration' &&
n.parent.parent.parent.type === 'Program'
)
}

const types = ['builtin', 'external', 'internal', 'unknown', 'parent', 'sibling', 'index', 'object']
Expand Down Expand Up @@ -583,14 +595,6 @@ module.exports = {
}
}
let imported = []
let level = 0

function incrementLevel() {
level++
}
function decrementLevel() {
level--
}

return {
ImportDeclaration: function handleImports(node) {
Expand Down Expand Up @@ -641,7 +645,7 @@ module.exports = {
)
},
CallExpression: function handleRequires(node) {
if (level !== 0 || !isStaticRequire(node) || !isInVariableDeclarator(node.parent)) {
if (!isStaticRequire(node) || !isModuleLevelRequire(node)) {
return
}
const name = node.arguments[0].value
Expand Down Expand Up @@ -671,16 +675,6 @@ module.exports = {

imported = []
},
FunctionDeclaration: incrementLevel,
FunctionExpression: incrementLevel,
ArrowFunctionExpression: incrementLevel,
BlockStatement: incrementLevel,
ObjectExpression: incrementLevel,
'FunctionDeclaration:exit': decrementLevel,
'FunctionExpression:exit': decrementLevel,
'ArrowFunctionExpression:exit': decrementLevel,
'BlockStatement:exit': decrementLevel,
'ObjectExpression:exit': decrementLevel,
}
},
}
Loading

0 comments on commit 6b11201

Please sign in to comment.