Skip to content

Commit c4486cc

Browse files
committed
Update: tryExtensions option is added (fixes #14)
And more compatibility to check published.
1 parent 6413ca8 commit c4486cc

24 files changed

+487
-45
lines changed

docs/rules/no-missing-import.md

+35
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,41 @@ import existingFile from "./existing-file";
2727
import existingModule from "existing-module";
2828
```
2929

30+
### Options
31+
32+
```json
33+
{
34+
"rules": {
35+
"node/no-missing-import": [2, {
36+
"tryExtensions": [".js", ".json", ".node"]
37+
}]
38+
}
39+
}
40+
```
41+
42+
#### `tryExtensions`
43+
44+
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
45+
`tryExtensions` option is the extension list this rule uses at the time.
46+
47+
Default is `[".js", ".json", ".node"]`.
48+
49+
This option can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
50+
Several rules have this option, but we can set this option at once.
51+
52+
```json
53+
{
54+
"settings": {
55+
"node": {
56+
"tryExtensions": [".js", ".json", ".node"]
57+
}
58+
},
59+
"rules": {
60+
"node/no-missing-import": 2
61+
}
62+
}
63+
```
64+
3065
## When Not To Use It
3166

3267
This rule should not be used in ES3/5 environments.

docs/rules/no-missing-require.md

+35
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,41 @@ var existingModule = require("existing-module");
3333
var foo = require(FOO_NAME);
3434
```
3535

36+
### Options
37+
38+
```json
39+
{
40+
"rules": {
41+
"node/no-missing-require": [2, {
42+
"tryExtensions": [".js", ".json", ".node"]
43+
}]
44+
}
45+
}
46+
```
47+
48+
#### `tryExtensions`
49+
50+
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
51+
`tryExtensions` option is the extension list this rule uses at the time.
52+
53+
Default is `[".js", ".json", ".node"]`.
54+
55+
This option can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
56+
Several rules have this option, but we can set this option at once.
57+
58+
```json
59+
{
60+
"settings": {
61+
"node": {
62+
"tryExtensions": [".js", ".json", ".node"]
63+
}
64+
},
65+
"rules": {
66+
"node/no-missing-require": 2
67+
}
68+
}
69+
```
70+
3671
## When Not To Use It
3772

3873
If you don't want to be notified about usage of `require()`, then it's safe to disable this rule.

docs/rules/no-unpublished-import.md

+35
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,41 @@ import publishedFile from "./published-file";
3636
import dependedModule from "depended-module";
3737
```
3838

39+
### Options
40+
41+
```json
42+
{
43+
"rules": {
44+
"node/no-missing-import": [2, {
45+
"tryExtensions": [".js", ".json", ".node"]
46+
}]
47+
}
48+
}
49+
```
50+
51+
#### `tryExtensions`
52+
53+
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
54+
`tryExtensions` option is the extension list this rule uses at the time.
55+
56+
Default is `[".js", ".json", ".node"]`.
57+
58+
This option can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
59+
Several rules have this option, but we can set this option at once.
60+
61+
```json
62+
{
63+
"settings": {
64+
"node": {
65+
"tryExtensions": [".js", ".json", ".node"]
66+
}
67+
},
68+
"rules": {
69+
"node/no-missing-import": 2
70+
}
71+
}
72+
```
73+
3974
## When Not To Use It
4075

4176
This rule should not be used in ES3/5 environments.

docs/rules/no-unpublished-require.md

+35
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,41 @@ var dependedModule = require("depended-module");
3838
var foo = require(FOO_NAME);
3939
```
4040

41+
### Options
42+
43+
```json
44+
{
45+
"rules": {
46+
"node/no-missing-import": [2, {
47+
"tryExtensions": [".js", ".json", ".node"]
48+
}]
49+
}
50+
}
51+
```
52+
53+
#### `tryExtensions`
54+
55+
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
56+
`tryExtensions` option is the extension list this rule uses at the time.
57+
58+
Default is `[".js", ".json", ".node"]`.
59+
60+
This option can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
61+
Several rules have this option, but we can set this option at once.
62+
63+
```json
64+
{
65+
"settings": {
66+
"node": {
67+
"tryExtensions": [".js", ".json", ".node"]
68+
}
69+
},
70+
"rules": {
71+
"node/no-missing-import": 2
72+
}
73+
}
74+
```
75+
4176
## When Not To Use It
4277

4378
If you don't want to be notified about usage of `require()`, then it's safe to disable this rule.

lib/rules/no-missing-import.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,19 @@ module.exports = function(context) {
3434
};
3535
};
3636

37-
module.exports.schema = [];
37+
module.exports.schema = [
38+
{
39+
type: "object",
40+
properties: {
41+
tryExtensions: {
42+
type: "array",
43+
items: {
44+
type: "string",
45+
pattern: "^\\."
46+
},
47+
uniqueItems: true
48+
}
49+
},
50+
additionalProperties: false
51+
}
52+
];

lib/rules/no-missing-require.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,19 @@ module.exports = function(context) {
3434
};
3535
};
3636

37-
module.exports.schema = [];
37+
module.exports.schema = [
38+
{
39+
type: "object",
40+
properties: {
41+
tryExtensions: {
42+
type: "array",
43+
items: {
44+
type: "string",
45+
pattern: "^\\."
46+
},
47+
uniqueItems: true
48+
}
49+
},
50+
additionalProperties: false
51+
}
52+
];

lib/rules/no-unpublished-import.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,19 @@ module.exports = function(context) {
3434
};
3535
};
3636

37-
module.exports.schema = [];
37+
module.exports.schema = [
38+
{
39+
type: "object",
40+
properties: {
41+
tryExtensions: {
42+
type: "array",
43+
items: {
44+
type: "string",
45+
pattern: "^\\."
46+
},
47+
uniqueItems: true
48+
}
49+
},
50+
additionalProperties: false
51+
}
52+
];

lib/rules/no-unpublished-require.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,19 @@ module.exports = function(context) {
3434
};
3535
};
3636

37-
module.exports.schema = [];
37+
module.exports.schema = [
38+
{
39+
type: "object",
40+
properties: {
41+
tryExtensions: {
42+
type: "array",
43+
items: {
44+
type: "string",
45+
pattern: "^\\."
46+
},
47+
uniqueItems: true
48+
}
49+
},
50+
additionalProperties: false
51+
}
52+
];

lib/util/check-existence.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ module.exports = function checkForExistence(context, filePath, targets) {
3838
var target = targets[i];
3939

4040
// Workaround for https://github.com/substack/node-resolve/issues/78
41-
if (target.relative) {
42-
var ext = path.extname(target.name);
43-
var name = (ext === ".js") ? target.name : target.name + ".js";
44-
if (exists(path.resolve(basedir, name))) {
41+
if (target.filePath) {
42+
if (exists(target.filePath)) {
4543
continue;
4644
}
4745
} else {

lib/util/check-publish.js

-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
var path = require("path");
1515
var assign = require("object-assign");
16-
var exists = require("./exists");
1716
var getNpmignore = require("./get-npmignore");
1817
var getPackageJson = require("./get-package-json");
1918

@@ -39,11 +38,7 @@ module.exports = function checkForPublish(context, filePath, targets) {
3938

4039
var basedir = path.dirname(packageInfo.filePath);
4140
var toRelative = function(fullPath) { // eslint-disable-line func-style
42-
var ext = path.extname(fullPath);
4341
var retv = path.relative(basedir, fullPath).replace(/\\/g, "/");
44-
if (!ext && exists(fullPath + ".js")) {
45-
retv += ".js";
46-
}
4742
return retv;
4843
};
4944
var npmignore = getNpmignore(filePath);

lib/util/get-import-export-targets.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
var path = require("path");
1414
var resolve = require("resolve");
15+
var getTryExtensions = require("./get-try-extensions");
1516
var ImportTarget = require("./import-target");
1617

1718
//------------------------------------------------------------------------------
@@ -37,6 +38,7 @@ module.exports = function getImportExportTargets(context, programNode) {
3738
var retv = [];
3839
var basedir = path.dirname(path.resolve(context.getFilename()));
3940
var statements = programNode.body;
41+
var extensions = getTryExtensions(context);
4042

4143
for (var i = 0; i < statements.length; ++i) {
4244
var statement = statements[i];
@@ -47,9 +49,10 @@ module.exports = function getImportExportTargets(context, programNode) {
4749
}
4850

4951
// Gets the target module.
50-
var name = statement.source && statement.source.value;
52+
var node = statement.source;
53+
var name = node && node.value;
5154
if (name && !resolve.isCore(name)) {
52-
retv.push(new ImportTarget(statement.source, name, basedir));
55+
retv.push(new ImportTarget(node, name, basedir, extensions));
5356
}
5457
}
5558

0 commit comments

Comments
 (0)