1
1
import { TSESTree } from "@typescript-eslint/utils" ;
2
- import { isExportNamedDeclaration } from "@/src/utils/ast-guards" ;
2
+ import { isExportNamedDeclaration , isIdentifier } from "@/src/utils/ast-guards" ;
3
3
import { RuleContext , SourceCode } from "@typescript-eslint/utils/ts-eslint" ;
4
4
import { parse } from "@/src/utils/parse" ;
5
5
import { findIdentifiersInChildren } from "@/src/utils/find-identifiers-in-children" ;
6
6
import { readFileSync } from "fs" ;
7
- import { getImportDeclarationPath } from "@/src/utils/get-import-declaration-path" ;
7
+ import { getImportDeclaration } from "@/src/utils/get-import-declaration" ;
8
+ import { findInChildren } from "./find-in-children" ;
8
9
9
10
export function resolveImportedId (
10
11
context : RuleContext < string , unknown [ ] > ,
11
12
impt : TSESTree . ImportDeclaration
12
13
) {
13
- const importPath = getImportDeclarationPath ( context , impt ) ;
14
- if ( importPath . startsWith ( "./node_modules" ) ) return ;
14
+ const imp = getImportDeclaration ( context , impt ) ;
15
+ if ( ! imp . path || imp . path . startsWith ( "./node_modules" ) ) {
16
+ const id = impt . specifiers [ 0 ] . local ;
17
+
18
+ return {
19
+ id : findInChildren (
20
+ parse ( `export function ${ id . name } () {}` , context ) ,
21
+ isIdentifier
22
+ ) ,
23
+ module : imp . module ,
24
+ protocol : imp . protocol ,
25
+ context,
26
+ } ;
27
+ }
15
28
let content = "" ;
16
29
try {
17
- content = readFileSync ( importPath , "utf-8" ) ;
30
+ content = readFileSync ( imp . path , "utf-8" ) ;
18
31
} catch ( e ) {
19
- console . error ( `Could not read file ${ importPath } ` ) ;
32
+ console . error ( `Could not read file ${ imp } ` ) ;
20
33
console . error ( e ) ;
21
34
return ;
22
35
}
@@ -29,7 +42,7 @@ export function resolveImportedId(
29
42
30
43
const ctxParsed = {
31
44
...context ,
32
- physicalFilename : importPath ,
45
+ physicalFilename : imp . path ,
33
46
sourceCode : new SourceCode ( content , parsed ) ,
34
47
parser : ( context as any ) . parser ,
35
48
parserOptions : context . parserOptions ,
@@ -41,5 +54,10 @@ export function resolveImportedId(
41
54
settings : context . settings ,
42
55
} ;
43
56
44
- return { id : identifierInParsed , context : ctxParsed } ;
57
+ return {
58
+ id : identifierInParsed ,
59
+ module : imp . module ,
60
+ protocol : imp . protocol ,
61
+ context : ctxParsed ,
62
+ } ;
45
63
}
0 commit comments