Skip to content

Commit caa1637

Browse files
committed
Fix ability to require() JSON and other files without extension.
Fixes #402
1 parent dead5bb commit caa1637

11 files changed

+166
-13
lines changed

lib/filter_js.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,21 @@ var path = require('path');
1010
* expect as argument a file as an objectg with the 'file' property
1111
*
1212
* @private
13-
* @param {String|Array} extensions to be filtered
13+
* @param {String|Array} extension to be filtered
1414
* @param {boolean} allowAll ignore the entire extension check and always
1515
* pass through files. This is used by the polglot mode.
1616
* @return {Function} a filter function, this function returns true if the input filename extension
1717
* is in the extension whitelist
1818
*/
19-
function filterJS(extensions, allowAll) {
19+
function filterJS(extension, allowAll) {
2020

2121
if (allowAll) {
2222
return function () {
2323
return true;
2424
};
2525
}
2626

27-
extensions = extensions || [];
28-
if (typeof extensions === 'string') {
29-
extensions = [extensions];
30-
}
31-
extensions = extensions.concat(['js', 'es6', 'jsx']);
27+
var extensions = [].concat(extension || []).concat(['js', 'es6', 'jsx']);
3228

3329
return function (data) {
3430
return extensions.indexOf(path.extname(data.file).substring(1)) !== -1;

lib/input/dependency.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var mdeps = require('module-deps-sortable'),
2323
*/
2424
function dependencyStream(indexes, options, callback) {
2525
var filterer = filterJS(options.extension, options.polyglot);
26+
2627
var md = mdeps({
2728
/**
2829
* Determine whether a module should be included in documentation
@@ -32,6 +33,11 @@ function dependencyStream(indexes, options, callback) {
3233
filter: function (id) {
3334
return !!options.external || moduleFilters.internalOnly(id);
3435
},
36+
extensions: [].concat(options.extension || [])
37+
.concat(['js', 'es6', 'jsx', 'json'])
38+
.map(function (ext) {
39+
return '.' + ext;
40+
}),
3541
transform: [babelify.configure({
3642
sourceMap: false,
3743
presets: [

test/fixture/es6-ext.es6

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* This is the default export frogs!
3+
*/
4+
export default 10;

test/fixture/es6-import.input.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hasEx6 from './es6-ext';
12
import multiply from "./es6.input.js";
23
import * as foo from "some-other-module";
34

test/fixture/es6-import.output.json

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,26 @@
6565
],
6666
"loc": {
6767
"start": {
68-
"line": 4,
68+
"line": 5,
6969
"column": 0
7070
},
7171
"end": {
72-
"line": 7,
72+
"line": 8,
7373
"column": 3
7474
}
7575
},
7676
"context": {
7777
"loc": {
7878
"start": {
79-
"line": 8,
79+
"line": 9,
8080
"column": 0
8181
},
8282
"end": {
83-
"line": 8,
83+
"line": 9,
8484
"column": 43
8585
}
8686
},
87-
"code": "import multiply from \"./es6.input.js\";\nimport * as foo from \"some-other-module\";\n\n/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nvar multiplyTwice = (a) => a * multiply(a);\n\nexport default multiplyTwice;\n"
87+
"code": "import hasEx6 from './es6-ext';\nimport multiply from \"./es6.input.js\";\nimport * as foo from \"some-other-module\";\n\n/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nvar multiplyTwice = (a) => a * multiply(a);\n\nexport default multiplyTwice;\n"
8888
},
8989
"errors": [],
9090
"returns": [
@@ -153,7 +153,7 @@
153153
{
154154
"title": "param",
155155
"name": "a",
156-
"lineNumber": 8
156+
"lineNumber": 9
157157
}
158158
],
159159
"members": {
@@ -168,6 +168,96 @@
168168
],
169169
"namespace": "multiplyTwice"
170170
},
171+
{
172+
"description": {
173+
"type": "root",
174+
"children": [
175+
{
176+
"type": "paragraph",
177+
"children": [
178+
{
179+
"type": "text",
180+
"value": "This is the default export frogs!",
181+
"position": {
182+
"start": {
183+
"line": 1,
184+
"column": 1,
185+
"offset": 0
186+
},
187+
"end": {
188+
"line": 1,
189+
"column": 34,
190+
"offset": 33
191+
},
192+
"indent": []
193+
}
194+
}
195+
],
196+
"position": {
197+
"start": {
198+
"line": 1,
199+
"column": 1,
200+
"offset": 0
201+
},
202+
"end": {
203+
"line": 1,
204+
"column": 34,
205+
"offset": 33
206+
},
207+
"indent": []
208+
}
209+
}
210+
],
211+
"position": {
212+
"start": {
213+
"line": 1,
214+
"column": 1,
215+
"offset": 0
216+
},
217+
"end": {
218+
"line": 1,
219+
"column": 34,
220+
"offset": 33
221+
}
222+
}
223+
},
224+
"tags": [],
225+
"loc": {
226+
"start": {
227+
"line": 1,
228+
"column": 0
229+
},
230+
"end": {
231+
"line": 3,
232+
"column": 3
233+
}
234+
},
235+
"context": {
236+
"loc": {
237+
"start": {
238+
"line": 4,
239+
"column": 0
240+
},
241+
"end": {
242+
"line": 4,
243+
"column": 18
244+
}
245+
},
246+
"code": "/**\n * This is the default export frogs!\n */\nexport default 10;\n"
247+
},
248+
"errors": [],
249+
"name": "es6-ext",
250+
"members": {
251+
"instance": [],
252+
"static": []
253+
},
254+
"path": [
255+
{
256+
"name": "es6-ext"
257+
}
258+
],
259+
"namespace": "es6-ext"
260+
},
171261
{
172262
"description": {
173263
"type": "root",

test/fixture/es6-import.output.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This function returns the number one.
88

99
Returns **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** numberone
1010

11+
# es6-ext
12+
13+
This is the default export frogs!
14+
1115
# destructure
1216

1317
This function destructures with defaults.

test/fixture/es6-import.output.md.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,51 @@
147147
}
148148
]
149149
},
150+
{
151+
"depth": 1,
152+
"type": "heading",
153+
"children": [
154+
{
155+
"type": "text",
156+
"value": "es6-ext"
157+
}
158+
]
159+
},
160+
{
161+
"type": "paragraph",
162+
"children": [
163+
{
164+
"type": "text",
165+
"value": "This is the default export frogs!",
166+
"position": {
167+
"start": {
168+
"line": 1,
169+
"column": 1,
170+
"offset": 0
171+
},
172+
"end": {
173+
"line": 1,
174+
"column": 34,
175+
"offset": 33
176+
},
177+
"indent": []
178+
}
179+
}
180+
],
181+
"position": {
182+
"start": {
183+
"line": 1,
184+
"column": 1,
185+
"offset": 0
186+
},
187+
"end": {
188+
"line": 1,
189+
"column": 34,
190+
"offset": 33
191+
},
192+
"indent": []
193+
}
194+
},
150195
{
151196
"depth": 1,
152197
"type": "heading",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var data = require('./require-json');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy