Skip to content

Commit

Permalink
Merge commit '3c46d308ccb462a52554257c49c374045d1a6cf7' into file_pat…
Browse files Browse the repository at this point in the history
…h_in_parser_options

* commit '3c46d308ccb462a52554257c49c374045d1a6cf7':
  rollback utils dependency to 2.0.0
  add yank note to utils change log
  add yanking note to root change log
  Upgrade debug version of eslint-module-utils (import-js#844)
  remove obsolete dad joke
  update utils changelog
  bump eslint-module-utils to v2.1.0
  bump v2.4.0
  fix typos, enforce type of array of strings in allow option
  update CHANGELOG.md
  eslint-module-utils: filePath in parserOptions (import-js#840)
  write doc, add two more tests
  add allow glob for rule no-unassigned-import, fix import-js#671

# Conflicts:
#	utils/CHANGELOG.md
  • Loading branch information
sompylasar committed Jun 4, 2017
2 parents 5732742 + 3c46d30 commit 60b524b
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 9 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Added
- Add `filePath` into `parserOptions` passed to `parser` ([#839], thanks [@sompylasar])

## [2.4.0] - 2017-06-02 [YANKED]

Yanked due to critical issue in eslint-module-utils with cache key resulting from [#839].

### Added
- Add `filePath` into `parserOptions` passed to `parser` ([#839], thanks [@sompylasar])
- Add `allow` option to [`no-unassigned-import`] to allow for files that match the globs ([#671], [#737], thanks [@kevin940726]).

## [2.3.0] - 2017-05-18
### Added
- [`no-anonymous-default-export`] rule: report anonymous default exports ([#712], thanks [@duncanbeevers]).
Expand Down Expand Up @@ -390,6 +398,7 @@ for info on changes for earlier releases.
[`no-anonymous-default-export`]: ./docs/rules/no-anonymous-default-export.md

[#742]: https://github.com/benmosher/eslint-plugin-import/pull/742
[#737]: https://github.com/benmosher/eslint-plugin-import/pull/737
[#712]: https://github.com/benmosher/eslint-plugin-import/pull/712
[#685]: https://github.com/benmosher/eslint-plugin-import/pull/685
[#680]: https://github.com/benmosher/eslint-plugin-import/pull/680
Expand Down Expand Up @@ -446,6 +455,8 @@ for info on changes for earlier releases.
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
[#314]: https://github.com/benmosher/eslint-plugin-import/pull/314

[#839]: https://github.com/benmosher/eslint-plugin-import/issues/839
[#671]: https://github.com/benmosher/eslint-plugin-import/issues/671
[#660]: https://github.com/benmosher/eslint-plugin-import/issues/660
[#653]: https://github.com/benmosher/eslint-plugin-import/issues/653
[#627]: https://github.com/benmosher/eslint-plugin-import/issues/627
Expand Down Expand Up @@ -503,7 +514,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.3.0...HEAD
[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v2.4.0...HEAD
[2.4.0]: https://github.com/benmosher/eslint-plugin-import/compare/v2.3.0...v2.4.0
[2.3.0]: https://github.com/benmosher/eslint-plugin-import/compare/v2.2.0...v2.3.0
[2.2.0]: https://github.com/benmosher/eslint-plugin-import/compare/v2.1.0...v2.2.0
[2.1.0]: https://github.com/benmosher/eslint-plugin-import/compare/v2.0.1...v2.1.0
Expand Down Expand Up @@ -583,3 +595,5 @@ for info on changes for earlier releases.
[@giodamelio]: https://github.com/giodamelio
[@ntdb]: https://github.com/ntdb
[@ramasilveyra]: https://github.com/ramasilveyra
[@sompylasar]: https://github.com/sompylasar
[@kevin940726]: https://github.com/kevin940726
3 changes: 1 addition & 2 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
```

Generally, don't use `npm version` for this because it creates a tag, which I normally
wait until signoff from all contributors (`new Set(["@jfmengels"])`) and actually
`npm publish`-ing to snap the tag.
wait until signoff from contributors and actually `npm publish`-ing to snap the tag.

3. create pull request from `release-[x.y.z]` into `release` branch

Expand Down
22 changes: 22 additions & 0 deletions docs/rules/no-unassigned-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ With both CommonJS' `require` and the ES6 modules' `import` syntax, it is possib

This rule aims to remove modules with side-effects by reporting when a module is imported but not assigned.

### Options

This rule supports the following option:

`allow`: An Array of globs. The files that match any of these patterns would be ignored/allowed by the linter. This can be useful for some build environments (e.g. css-loader in webpack).

Note that the globs start from the where the linter is executed (usually project root), but not from each file that includes the source. Learn more in both the pass and fail examples below.


## Fail

```js
import 'should'
require('should')

// In <PROJECT_ROOT>/src/app.js
import '../styles/app.css'
// {"allow": ["styles/*.css"]}
```


Expand All @@ -34,4 +47,13 @@ bar(require('foo'))
require('foo').bar
require('foo').bar()
require('foo')()

// With allow option set
import './style.css' // {"allow": ["**/*.css"]}
import 'babel-register' // {"allow": ["babel-register"]}

// In <PROJECT_ROOT>/src/app.js
import './styles/app.css'
import '../scripts/register.js'
// {"allow": ["src/styles/**", "**/scripts/*.js"]}
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-import",
"version": "2.3.0",
"version": "2.4.0",
"description": "Import with sanity.",
"engines": {
"node": ">=4"
Expand Down
37 changes: 35 additions & 2 deletions src/rules/no-unassigned-import.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import isStaticRequire from '../core/staticRequire'
import path from 'path'
import minimatch from 'minimatch'

function report(context, node) {
context.report({
Expand All @@ -7,15 +9,40 @@ function report(context, node) {
})
}

function testIsAllow(globs, filename, source) {
if (!Array.isArray(globs)) {
return false // default doesn't allow any patterns
}

let filePath

if (source[0] !== '.' && source[0] !== '/') { // a node module
filePath = source
} else {
filePath = path.resolve(path.dirname(filename), source) // get source absolute path
}

return globs.find(glob => (
minimatch(filePath, glob) ||
minimatch(filePath, path.join(process.cwd(), glob))
)) !== undefined
}

function create(context) {
const options = context.options[0] || {}
const filename = context.getFilename()
const isAllow = source => testIsAllow(options.allow, filename, source)

return {
ImportDeclaration(node) {
if (node.specifiers.length === 0) {
if (node.specifiers.length === 0 && !isAllow(node.source.value)) {
report(context, node)
}
},
ExpressionStatement(node) {
if (node.expression.type === 'CallExpression' && isStaticRequire(node.expression)) {
if (node.expression.type === 'CallExpression' &&
isStaticRequire(node.expression) &&
!isAllow(node.expression.arguments[0].value)) {
report(context, node.expression)
}
},
Expand All @@ -33,6 +60,12 @@ module.exports = {
'devDependencies': { 'type': ['boolean', 'array'] },
'optionalDependencies': { 'type': ['boolean', 'array'] },
'peerDependencies': { 'type': ['boolean', 'array'] },
'allow': {
'type': 'array',
'items': {
'type': 'string',
},
},
},
'additionalProperties': false,
},
Expand Down
67 changes: 67 additions & 0 deletions tests/src/rules/no-unassigned-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,52 @@ ruleTester.run('no-unassigned-import', rule, {
test({ code: 'require("lodash").foo'}),
test({ code: 'require("lodash").foo()'}),
test({ code: 'require("lodash")()'}),
test({
code: 'import "app.css"',
options: [{ 'allow': ['**/*.css'] }],
}),
test({
code: 'import "app.css";',
options: [{ 'allow': ['*.css'] }],
}),
test({
code: 'import "./app.css"',
options: [{ 'allow': ['**/*.css'] }],
}),
test({
code: 'import "foo/bar"',
options: [{ 'allow': ['foo/**'] }],
}),
test({
code: 'import "foo/bar"',
options: [{ 'allow': ['foo/bar'] }],
}),
test({
code: 'import "../dir/app.css"',
options: [{ 'allow': ['**/*.css'] }],
}),
test({
code: 'import "../dir/app.js"',
options: [{ 'allow': ['**/dir/**'] }],
}),
test({
code: 'require("./app.css")',
options: [{ 'allow': ['**/*.css'] }],
}),
test({
code: 'import "babel-register"',
options: [{ 'allow': ['babel-register'] }],
}),
test({
code: 'import "./styles/app.css"',
options: [{ 'allow': ['src/styles/**'] }],
filename: path.join(process.cwd(), 'src/app.js'),
}),
test({
code: 'import "../scripts/register.js"',
options: [{ 'allow': ['src/styles/**', '**/scripts/*.js'] }],
filename: path.join(process.cwd(), 'src/app.js'),
}),
],
invalid: [
test({
Expand All @@ -39,5 +85,26 @@ ruleTester.run('no-unassigned-import', rule, {
code: 'require("lodash")',
errors: [error],
}),
test({
code: 'import "./app.css"',
options: [{ 'allow': ['**/*.js'] }],
errors: [error],
}),
test({
code: 'import "./app.css"',
options: [{ 'allow': ['**/dir/**'] }],
errors: [error],
}),
test({
code: 'require("./app.css")',
options: [{ 'allow': ['**/*.js'] }],
errors: [error],
}),
test({
code: 'import "./styles/app.css"',
options: [{ 'allow': ['styles/*.css'] }],
filename: path.join(process.cwd(), 'src/app.js'),
errors: [error],
}),
],
})
8 changes: 7 additions & 1 deletion utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ All notable changes to this module will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
## Unreleased


## v2.1.0 - 2017-06-02 [YANKED]

Yanked due to critical issue with cache key resulting from #839.

### Added
- `parse` now additionally passes `filePath` to `parser` in `parserOptions` like `eslint` core does

Expand Down
4 changes: 2 additions & 2 deletions utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-module-utils",
"version": "2.0.0",
"version": "2.1.0",
"description": "Core utilities to support eslint-plugin-import and other module-related plugins.",
"engines": {
"node": ">=4"
Expand All @@ -25,7 +25,7 @@
},
"homepage": "https://github.com/benmosher/eslint-plugin-import#readme",
"dependencies": {
"debug": "2.2.0",
"debug": "^2.6.8",
"pkg-dir": "^1.0.0"
}
}

0 comments on commit 60b524b

Please sign in to comment.