4
4
*/
5
5
"use strict"
6
6
7
- const DEFAULT_VALUE = Object . freeze ( [ ".js" , ".json" , ".node" , ".mjs" , ".cjs" ] )
7
+ const { getTSConfigForContext } = require ( "./get-tsconfig" )
8
+ const isTypescript = require ( "./is-typescript" )
9
+
10
+ const DEFAULT_JS_VALUE = Object . freeze ( [
11
+ ".js" ,
12
+ ".json" ,
13
+ ".node" ,
14
+ ".mjs" ,
15
+ ".cjs" ,
16
+ ] )
17
+ const DEFAULT_TS_VALUE = Object . freeze ( [
18
+ ".js" ,
19
+ ".ts" ,
20
+ ".mjs" ,
21
+ ".mts" ,
22
+ ".cjs" ,
23
+ ".cts" ,
24
+ ".json" ,
25
+ ".node" ,
26
+ ] )
8
27
9
28
/**
10
29
* Gets `tryExtensions` property from a given option object.
@@ -13,7 +32,7 @@ const DEFAULT_VALUE = Object.freeze([".js", ".json", ".node", ".mjs", ".cjs"])
13
32
* @returns {string[]|null } The `tryExtensions` value, or `null`.
14
33
*/
15
34
function get ( option ) {
16
- if ( option && option . tryExtensions && Array . isArray ( option . tryExtensions ) ) {
35
+ if ( Array . isArray ( option ? .tryExtensions ) ) {
17
36
return option . tryExtensions . map ( String )
18
37
}
19
38
return null
@@ -24,19 +43,29 @@ function get(option) {
24
43
*
25
44
* 1. This checks `options` property, then returns it if exists.
26
45
* 2. This checks `settings.n` | `settings.node` property, then returns it if exists.
27
- * 3. This returns `[".js", ".json", ".node"]`.
46
+ * 3. This returns `[".js", ".json", ".node", ".mjs", ".cjs" ]`.
28
47
*
29
48
* @param {RuleContext } context - The rule context.
30
49
* @returns {string[] } A list of extensions.
31
50
*/
32
51
module . exports = function getTryExtensions ( context , optionIndex = 0 ) {
33
- return (
34
- get ( context . options && context . options [ optionIndex ] ) ||
35
- get (
36
- context . settings && ( context . settings . n || context . settings . node )
37
- ) ||
38
- DEFAULT_VALUE
39
- )
52
+ const configured =
53
+ get ( context . options ?. [ optionIndex ] ) ??
54
+ get ( context . settings ?. n ) ??
55
+ get ( context . settings ?. node )
56
+
57
+ if ( configured != null ) {
58
+ return configured
59
+ }
60
+
61
+ if ( isTypescript ( context ) ) {
62
+ const tsconfig = getTSConfigForContext ( context )
63
+ if ( tsconfig ?. config ?. compilerOptions ?. allowImportingTsExtensions ) {
64
+ return DEFAULT_TS_VALUE
65
+ }
66
+ }
67
+
68
+ return DEFAULT_JS_VALUE
40
69
}
41
70
42
71
module . exports . schema = {
0 commit comments