Skip to content

Commit 80be205

Browse files
committed
feat: support extra options argument for resolver
1 parent 35bc850 commit 80be205

File tree

6 files changed

+29
-18
lines changed

6 files changed

+29
-18
lines changed

resolvers/README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ To the extent it is feasible, trailing versions of the resolvers will continue t
1818

1919
Currently, version 1 is assumed if no `interfaceVersion` is available. (didn't think to define it until v2, heh. 😅)
2020

21-
### `resolve(source, file, config, context) => { found: Boolean, path: String? }`
21+
### `resolve(source, file, config, options) => { found: Boolean, path: String? }`
2222

2323
Given:
2424
```js
@@ -52,12 +52,20 @@ the absolute path to the file making the import (`/some/path/to/module.js`)
5252
an object provided via the `import/resolver` setting. `my-cool-resolver` will get `["some", "stuff"]` as its `config`, while
5353
`node` will get `{ "paths": ["a", "b", "c"] }` provided as `config`.
5454

55-
##### `context`
55+
##### `options`
5656

57-
**Only available after `[email protected]`**
57+
###### `options.context`
58+
59+
**Only available after `[email protected]`**
5860

5961
Please view [ESLint Context] for more details.
6062

63+
##### `options.tsCompilerOptions`
64+
65+
**Only available after `[email protected]`**
66+
67+
Please view [TSConfig Compiler Options] for more details.
68+
6169
#### Return value
6270

6371
The first resolver to return `{found: true}` is considered the source of truth. The returned object has:
@@ -89,3 +97,4 @@ exports.resolve = function (source, file, config) {
8997
[Node resolver]: ./node/index.js
9098
[`resolve`]: https://www.npmjs.com/package/resolve
9199
[ESLint Context]: https://eslint.org/docs/latest/developer-guide/working-with-rules#the-context-object
100+
[TSConfig Compiler Options]: https://www.typescriptlang.org/tsconfig#compilerOptions

src/ExportMap.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,14 @@ ExportMap.for = function (context) {
357357

358358
ExportMap.parse = function (path, content, context) {
359359
const m = new ExportMap(path);
360-
const isEsModuleInteropTrue = isEsModuleInterop();
360+
const tsCompilerOptions = getTsCompilerOptions();
361+
362+
const isEsModuleInteropTrue = !!tsCompilerOptions.esModuleInterop;
361363

362364
let ast;
363365
let visitorKeys;
364366
try {
365-
const result = parse(path, content, context);
367+
const result = parse(path, content, context, { tsCompilerOptions });
366368
ast = result.ast;
367369
visitorKeys = result.visitorKeys;
368370
} catch (err) {
@@ -549,7 +551,7 @@ ExportMap.parse = function (path, content, context) {
549551
return null;
550552
}
551553

552-
function isEsModuleInterop() {
554+
function getTsCompilerOptions() {
553555
const cacheKey = hashObject({
554556
tsconfigRootDir: context.parserOptions && context.parserOptions.tsconfigRootDir,
555557
}).digest('hex');
@@ -559,7 +561,7 @@ ExportMap.parse = function (path, content, context) {
559561
tsConfigCache.set(cacheKey, tsConfig);
560562
}
561563

562-
return tsConfig && tsConfig.options ? tsConfig.options.esModuleInterop : false;
564+
return tsConfig && tsConfig.options || {};
563565
}
564566

565567
ast.body.forEach(function (n) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
var assert = require('assert')
22
var path = require('path')
33

4-
exports.resolveImport = function (modulePath, sourceFile, config, context) {
4+
exports.resolveImport = function (modulePath, sourceFile, config, options) {
55
var sourceFileName = path.basename(sourceFile)
66
if (sourceFileName === 'foo.js') {
77
return path.join(__dirname, 'bar.jsx')
88
}
99
if (sourceFileName === 'exception.js') {
1010
throw new Error('foo-bar-resolver-v1 resolveImport test exception')
1111
}
12-
assert.ok(context, 'the `context` must be presented')
12+
assert.ok(options.context, 'the `context` must be presented')
1313
return undefined;
1414
}

tests/files/foo-bar-resolver-v1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
var assert = require('assert')
22
var path = require('path')
33

4-
exports.resolveImport = function (modulePath, sourceFile, config, context) {
4+
exports.resolveImport = function (modulePath, sourceFile, config, options) {
55
var sourceFileName = path.basename(sourceFile)
66
if (sourceFileName === 'foo.js') {
77
return path.join(__dirname, 'bar.jsx');
88
}
99
if (sourceFileName === 'exception.js') {
1010
throw new Error('foo-bar-resolver-v1 resolveImport test exception');
1111
}
12-
assert.ok(context, 'the `context` must be presented')
12+
assert.ok(options.context, 'the `context` must be presented')
1313
return undefined;
1414
};
1515

tests/files/foo-bar-resolver-v2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
var assert = require('assert')
22
var path = require('path')
33

4-
exports.resolve = function (modulePath, sourceFile, config, context) {
4+
exports.resolve = function (modulePath, sourceFile, config, options) {
55
var sourceFileName = path.basename(sourceFile)
66
if (sourceFileName === 'foo.js') {
77
return { found: true, path: path.join(__dirname, 'bar.jsx') }
88
}
99
if (sourceFileName === 'exception.js') {
1010
throw new Error('foo-bar-resolver-v2 resolve test exception')
1111
}
12-
assert.ok(context, 'the `context` must be presented')
12+
assert.ok(options.context, 'the `context` must be presented')
1313
return { found: false };
1414
};
1515

utils/resolve.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ exports.fileExistsWithCaseSync = function fileExistsWithCaseSync(filepath, cache
7979
return result;
8080
};
8181

82-
function relative(modulePath, sourceFile, settings, context) {
83-
return fullResolve(modulePath, sourceFile, settings, context).path;
82+
function relative(modulePath, sourceFile, settings, context, extra) {
83+
return fullResolve(modulePath, sourceFile, settings, context, extra).path;
8484
}
8585

86-
function fullResolve(modulePath, sourceFile, settings, context) {
86+
function fullResolve(modulePath, sourceFile, settings, context, extra) {
8787
// check if this is a bonus core module
8888
const coreSet = new Set(settings['import/core-modules']);
8989
if (coreSet.has(modulePath)) return { found: true, path: null };
@@ -104,7 +104,7 @@ function fullResolve(modulePath, sourceFile, settings, context) {
104104

105105
function v1() {
106106
try {
107-
const resolved = resolver.resolveImport(modulePath, sourceFile, config, context);
107+
const resolved = resolver.resolveImport(modulePath, sourceFile, config, Object.assign({ context }, extra));
108108
if (resolved === undefined) return { found: false };
109109
return { found: true, path: resolved };
110110
} catch (err) {
@@ -113,7 +113,7 @@ function fullResolve(modulePath, sourceFile, settings, context) {
113113
}
114114

115115
function v2() {
116-
return resolver.resolve(modulePath, sourceFile, config, context);
116+
return resolver.resolve(modulePath, sourceFile, config, Object.assign({ context }, extra));
117117
}
118118

119119
switch (resolver.interfaceVersion) {

0 commit comments

Comments
 (0)