Skip to content

Commit

Permalink
Fix: no-unpublished-* rules get to catch files outside of the module.
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jan 17, 2016
1 parent 900e64e commit 37eee2f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/util/get-npmignore.js
Expand Up @@ -22,13 +22,19 @@ var getPackageJson = require("./get-package-json");

var cache = new Cache();
var TAIL_SLASH = /\/+$/;
var PARENT_RELATIVE_PATH = /^\.\./;
var NEVER_IGNORED = /^(?:readme\.[^\.]*|(?:licen[cs]e|changes|changelog|history)(?:\.[^\.]*)?)$/i;

/**
* @returns {boolean} `false` always.
* Checks whether or not a given file name is a relative path to a ancestor
* directory.
*
* @param {string} filePath - A file name to check.
* @returns {boolean} `true` if the file name is a relative path to a ancestor
* directory.
*/
function alwaysFalse() {
return false;
function notAncestorFiles(filePath) {
return PARENT_RELATIVE_PATH.test(filePath);
}

/**
Expand All @@ -45,11 +51,12 @@ function and(f, g) {
/**
* @param {function} f - A function.
* @param {function} g - A function.
* @returns {function} A logical-or function of `f` and `g`.
* @param {function|null} h - A function.
* @returns {function} A logical-or function of `f`, `g`, and `h`.
*/
function or(f, g) {
function or(f, g, h) {
return function(filePath) {
return f(filePath) || g(filePath);
return f(filePath) || g(filePath) || (h && h(filePath));
};
}

Expand Down Expand Up @@ -151,7 +158,7 @@ function parseNpmignore(basedir, filesFieldExists) {
* `match` returns `true` if a given file path should be ignored.
*/
module.exports = function getNpmignore(startPath) {
var retv = {match: alwaysFalse};
var retv = {match: notAncestorFiles};

var p = getPackageJson(startPath);
if (p) {
Expand All @@ -164,13 +171,13 @@ module.exports = function getNpmignore(startPath) {
var npmignoreIgnore = parseNpmignore(path.dirname(p.filePath), Boolean(filesIgnore));

if (filesIgnore && npmignoreIgnore) {
retv.match = and(filterNeverIgnoredFiles(p), or(filesIgnore, npmignoreIgnore));
retv.match = and(filterNeverIgnoredFiles(p), or(notAncestorFiles, filesIgnore, npmignoreIgnore));
}
else if (filesIgnore) {
retv.match = and(filterNeverIgnoredFiles(p), filesIgnore);
retv.match = and(filterNeverIgnoredFiles(p), or(notAncestorFiles, filesIgnore));
}
else if (npmignoreIgnore) {
retv.match = and(filterNeverIgnoredFiles(p), npmignoreIgnore);
retv.match = and(filterNeverIgnoredFiles(p), or(notAncestorFiles, npmignoreIgnore));
}

cache.put(p.filePath, retv);
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/no-unpublished-import.js
Expand Up @@ -247,6 +247,14 @@ ruleTester.run("no-unpublished-import", rule, {
parserOptions: {sourceType: "module"}
},

{
filename: fixture("1/test.js"),
code: "import a from '../a.js';",
errors: ["\"../a.js\" is not published."],
ecmaFeatures: {modules: true},
parserOptions: {sourceType: "module"}
},

// Should work fine if the filename is relative.
{
filename: "tests/fixtures/no-unpublished/2/test.js",
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/no-unpublished-require.js
Expand Up @@ -251,6 +251,13 @@ ruleTester.run("no-unpublished-require", rule, {
errors: ["\"../test\" is not published."]
},

{
filename: fixture("1/test.js"),
code: "require('../a.js');",
env: {node: true},
errors: ["\"../a.js\" is not published."]
},

// `convertPath` option.
{
filename: fixture("3/src/test.jsx"),
Expand Down

0 comments on commit 37eee2f

Please sign in to comment.