Skip to content

Commit

Permalink
Fix: support negative patterns in files field
Browse files Browse the repository at this point in the history
Refs stylelint/stylelint#2978

The `files` field of package.json supports negative patterns (e.g. `!**/__tests__`), but `no-unpublished-*` rules have not supported it.
This commit makes those rules supporting the negative patterns.
  • Loading branch information
mysticatea committed Oct 23, 2017
1 parent 155f714 commit 2aaa994
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 6 deletions.
11 changes: 6 additions & 5 deletions lib/util/get-npmignore.js
Expand Up @@ -21,7 +21,7 @@ const getPackageJson = require("./get-package-json")
//------------------------------------------------------------------------------

const cache = new Cache()
const SLASH_AT_BEGIN_END = /(?:^\/+|\/+$)/g
const SLASH_AT_BEGIN_AND_END = /^!?\/+|^!|\/+$/g
const PARENT_RELATIVE_PATH = /^\.\./
const NEVER_IGNORED = /^(?:readme\.[^.]*|(?:licen[cs]e|changes|changelog|history)(?:\.[^.]*)?)$/i

Expand Down Expand Up @@ -96,10 +96,11 @@ function parseWhiteList(files) {
ig.add("*")

for (const file of files) {
if (typeof file === "string") {
const normalized = `/${file.replace(SLASH_AT_BEGIN_END, "")}`
ig.add(`!${normalized}`)
ig.add(`!${normalized}/**`)
if (typeof file === "string" && file) {
const prefix = file.startsWith("!") ? "" : "!"
const body = file.replace(SLASH_AT_BEGIN_AND_END, "")
ig.add(`${prefix}/${body}`)
ig.add(`${prefix}/${body}/**`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,7 +26,7 @@
"eslint": ">=3.1.0"
},
"dependencies": {
"ignore": "^3.3.3",
"ignore": "^3.3.6",
"minimatch": "^3.0.4",
"resolve": "^1.3.3",
"semver": "5.3.0"
Expand Down
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions tests/fixtures/no-unpublished/negative-in-files/package.json
@@ -0,0 +1,15 @@
{
"private": true,
"name": "test",
"version": "0.0.0",
"files": [
"lib",
"!**/__test__"
],
"dependencies": {
"aaa": "0.0.0"
},
"devDependencies": {
"bbb": "0.0.0"
}
}
6 changes: 6 additions & 0 deletions tests/lib/rules/no-unpublished-import.js
Expand Up @@ -128,6 +128,12 @@ ruleTester.run("no-unpublished-import", rule, {
code: "import bbb from 'bbb';",
env: {node: true},
},

// Negative patterns in files field.
{
filename: fixture("negative-in-files/lib/__test__/index.js"),
code: "import bbb from 'bbb';",
},
],
invalid: [
{
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/no-unpublished-require.js
Expand Up @@ -242,6 +242,13 @@ ruleTester.run("no-unpublished-require", rule, {
code: "require('bbb');",
env: {node: true},
},

// Negative patterns in files field.
{
filename: fixture("negative-in-files/lib/__test__/index.js"),
code: "require('bbb');",
env: {node: true},
},
],
invalid: [
{
Expand Down

0 comments on commit 2aaa994

Please sign in to comment.