Skip to content

Commit b42e07e

Browse files
buschtoensbirkskyum
authored andcommitted
fix(exported): respect parse-extension & require-extension
Fixes #1272. Fixes #1258.
1 parent 4fec3f4 commit b42e07e

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

src/extractors/exported.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ const traverse = babelTraverse.default || babelTraverse;
1515
* and inserting blank comments into documentation.js's processing stream.
1616
* Through inference steps, these comments gain more information and are automatically
1717
* documented as well as we can.
18+
* @param {Object} config
19+
* @param {Object} [config.extensions] extensions to try when resolving
1820
* @param {Object} ast the babel-parsed syntax tree
1921
* @param {Object} data the name of the file
2022
* @param {Function} addComment a method that creates a new comment if necessary
2123
* @returns {Array<Object>} comments
2224
* @private
2325
*/
2426
export default function walkExported(
27+
config /* { extensions?: string[] } */,
2528
ast,
2629
data /*: {
2730
file: string
@@ -120,7 +123,8 @@ export default function walkExported(
120123
local,
121124
exportKind,
122125
filename,
123-
source.value
126+
source.value,
127+
config.extensions
124128
);
125129
bindingPath = tmp.ast;
126130
specData = tmp.data;
@@ -181,16 +185,40 @@ function traverseExportedSubtree(path, data, addComments, overrideName) {
181185
}
182186
}
183187

184-
function getCachedData(dataCache, filePath) {
185-
let path = filePath;
186-
if (!nodePath.extname(path)) {
187-
path = require.resolve(path);
188+
function resolveFile(filePath, extensions = []) {
189+
try {
190+
// First try resolving the file with the default extensions.
191+
return require.resolve(filePath);
192+
} catch {
193+
// If that fails, try resolving the file with the extensions passed in.
188194
}
189195

196+
// Then try all other extensions in order.
197+
for (const extension of extensions) {
198+
try {
199+
return require.resolve(
200+
`${filePath}${extension.startsWith('.') ? extension : `.${extension}`}`
201+
);
202+
} catch {
203+
continue;
204+
}
205+
}
206+
207+
throw new Error(
208+
`Could not resolve \`${filePath}\` with any of the extensions: ${[
209+
...require.extensions,
210+
...extensions
211+
].join(', ')}`
212+
);
213+
}
214+
215+
function getCachedData(dataCache, filePath, extensions) {
216+
const path = resolveFile(filePath, extensions);
217+
190218
let value = dataCache.get(path);
191219
if (!value) {
192220
const input = fs.readFileSync(path, 'utf-8');
193-
const ast = parseToAst(input, filePath);
221+
const ast = parseToAst(input, path);
194222
value = {
195223
data: {
196224
file: path,
@@ -209,10 +237,11 @@ function findExportDeclaration(
209237
name,
210238
exportKind,
211239
referrer,
212-
filename
240+
filename,
241+
extensions
213242
) {
214243
const depPath = nodePath.resolve(nodePath.dirname(referrer), filename);
215-
const tmp = getCachedData(dataCache, depPath);
244+
const tmp = getCachedData(dataCache, depPath, extensions);
216245
const ast = tmp.ast;
217246
let data = tmp.data;
218247

@@ -273,7 +302,8 @@ function findExportDeclaration(
273302
local,
274303
exportKind,
275304
depPath,
276-
source.value
305+
source.value,
306+
extensions
277307
);
278308
rv = tmp.ast;
279309
data = tmp.data;

src/parsers/javascript.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ export default function parseJavaScript(data, config) {
2424
const ast = parseToAst(data.source, data.file);
2525
const addComment = _addComment.bind(null, visited, commentsByNode);
2626

27+
const extensions = []
28+
.concat(config.parseExtension, config.requireExtension)
29+
.filter(Boolean);
30+
2731
return _.flatMap(
2832
config.documentExported
29-
? [walkExported]
33+
? [walkExported.bind(null, { extensions })]
3034
: [
3135
walkComments.bind(null, 'leadingComments', true),
3236
walkComments.bind(null, 'innerComments', false),

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