Skip to content

Commit 37eee2f

Browse files
committed
Fix: no-unpublished-* rules get to catch files outside of the module.
1 parent 900e64e commit 37eee2f

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

Diff for: lib/util/get-npmignore.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ var getPackageJson = require("./get-package-json");
2222

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

2728
/**
28-
* @returns {boolean} `false` always.
29+
* Checks whether or not a given file name is a relative path to a ancestor
30+
* directory.
31+
*
32+
* @param {string} filePath - A file name to check.
33+
* @returns {boolean} `true` if the file name is a relative path to a ancestor
34+
* directory.
2935
*/
30-
function alwaysFalse() {
31-
return false;
36+
function notAncestorFiles(filePath) {
37+
return PARENT_RELATIVE_PATH.test(filePath);
3238
}
3339

3440
/**
@@ -45,11 +51,12 @@ function and(f, g) {
4551
/**
4652
* @param {function} f - A function.
4753
* @param {function} g - A function.
48-
* @returns {function} A logical-or function of `f` and `g`.
54+
* @param {function|null} h - A function.
55+
* @returns {function} A logical-or function of `f`, `g`, and `h`.
4956
*/
50-
function or(f, g) {
57+
function or(f, g, h) {
5158
return function(filePath) {
52-
return f(filePath) || g(filePath);
59+
return f(filePath) || g(filePath) || (h && h(filePath));
5360
};
5461
}
5562

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

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

166173
if (filesIgnore && npmignoreIgnore) {
167-
retv.match = and(filterNeverIgnoredFiles(p), or(filesIgnore, npmignoreIgnore));
174+
retv.match = and(filterNeverIgnoredFiles(p), or(notAncestorFiles, filesIgnore, npmignoreIgnore));
168175
}
169176
else if (filesIgnore) {
170-
retv.match = and(filterNeverIgnoredFiles(p), filesIgnore);
177+
retv.match = and(filterNeverIgnoredFiles(p), or(notAncestorFiles, filesIgnore));
171178
}
172179
else if (npmignoreIgnore) {
173-
retv.match = and(filterNeverIgnoredFiles(p), npmignoreIgnore);
180+
retv.match = and(filterNeverIgnoredFiles(p), or(notAncestorFiles, npmignoreIgnore));
174181
}
175182

176183
cache.put(p.filePath, retv);

Diff for: tests/lib/rules/no-unpublished-import.js

+8
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ ruleTester.run("no-unpublished-import", rule, {
247247
parserOptions: {sourceType: "module"}
248248
},
249249

250+
{
251+
filename: fixture("1/test.js"),
252+
code: "import a from '../a.js';",
253+
errors: ["\"../a.js\" is not published."],
254+
ecmaFeatures: {modules: true},
255+
parserOptions: {sourceType: "module"}
256+
},
257+
250258
// Should work fine if the filename is relative.
251259
{
252260
filename: "tests/fixtures/no-unpublished/2/test.js",

Diff for: tests/lib/rules/no-unpublished-require.js

+7
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ ruleTester.run("no-unpublished-require", rule, {
251251
errors: ["\"../test\" is not published."]
252252
},
253253

254+
{
255+
filename: fixture("1/test.js"),
256+
code: "require('../a.js');",
257+
env: {node: true},
258+
errors: ["\"../a.js\" is not published."]
259+
},
260+
254261
// `convertPath` option.
255262
{
256263
filename: fixture("3/src/test.jsx"),

0 commit comments

Comments
 (0)