Skip to content

Commit

Permalink
fix: providing file name when parsing code
Browse files Browse the repository at this point in the history
  • Loading branch information
Akronae committed Nov 24, 2024
1 parent 9cbb97a commit a7e9e53
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@types/eslint": "^9.6.1",
"@types/espree": "^10.1.0",
"@types/node": "^20.12.12",
"@types/react": "^18.3.12",
"@typescript-eslint/parser": "^7.9.0",
"@typescript-eslint/rule-tester": "^7.9.0",
"@typescript-eslint/types": "^7.9.0",
Expand Down
2 changes: 2 additions & 0 deletions src/rules/might-throw/might-throw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ await testFile(
[rule.name],
[]
);
await testFile("src/rules/might-throw/tests/generic-ok.ts", [rule.name], []);
await testFile("src/rules/might-throw/tests/tsx-ok.tsx", [rule.name], []);
5 changes: 5 additions & 0 deletions src/rules/might-throw/tests/generic-impt-ok.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { genericFunc } from "./generic-ok";

export const imptGenericFunc = () => {
return genericFunc<boolean>([false]);
};
3 changes: 3 additions & 0 deletions src/rules/might-throw/tests/generic-ok.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const genericFunc = <T>(val: Array<T>) => {
return val;
};
8 changes: 8 additions & 0 deletions src/rules/might-throw/tests/tsx-ok.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { imptGenericFunc } from "./generic-impt-ok";
import { genericFunc } from "./generic-ok";

export function MyComp() {
genericFunc<boolean>([false]);
imptGenericFunc();
return <div>Hello World</div>;
}
17 changes: 16 additions & 1 deletion src/utils/get-import-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,24 @@ export function getImportDeclaration(

if (!existsSync(res)) {
if (res.endsWith(".js")) {
res = res.replace(".js", ".ts");
res = findFileExtension(res.replace(".js", ""), [".jsx", ".ts", ".tsx"]);
} else if (res.endsWith(".jsx")) {
res = findFileExtension(res.replace(".jsx", ""), [".js", ".tsx", ".ts"]);
} else if (res.endsWith(".ts")) {
res = findFileExtension(res.replace(".ts", ""), [".tsx", ".js", ".jsx"]);
} else if (res.endsWith(".tsx")) {
res = findFileExtension(res.replace(".tsx", ""), [".ts", ".jsx", ".js"]);
}
}

return { path: res, module: to };
}

function findFileExtension(extless: string, tries: string[]) {
for (const ext of tries) {
if (extless + ext) {
return extless + ext;
}
}
return null;
}
9 changes: 5 additions & 4 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { RuleContext, SourceCode } from "@typescript-eslint/utils/ts-eslint";
import { exploreChildren } from "./explore-children";
import { SourceFile } from "typescript";

function omitNullish<T extends {}>(obj: T): T {
function omitNullish<T extends object>(obj: T): T {
return Object.fromEntries(
Object.entries(obj).filter(([, value]) => value != null)
) as T;
}

export function parse(
code: string,
source: SourceFile,
context: RuleContext<string, unknown[]>
): SourceCode.Program {
const opts = context.languageOptions;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const parsed = (opts.parser as any).parseForESLint(
code,
source.text,
omitNullish({
parser: opts.parserOptions,
filePath: source.fileName,
loc: true,
range: true,
tokens: true,
Expand Down
16 changes: 14 additions & 2 deletions src/utils/resolve-imported-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { findIdentifiersInChildren } from "@/src/utils/find-identifiers-in-child
import { readFileSync } from "fs";
import { getImportDeclaration } from "@/src/utils/get-import-declaration";
import { findInChildren } from "./find-in-children";
import ts from "typescript";
const { createSourceFile, ScriptTarget } = ts;

export function resolveImportedId(
context: RuleContext<string, unknown[]>,
Expand All @@ -17,7 +19,14 @@ export function resolveImportedId(

return {
id: findInChildren(
parse(`export function ${id.name}() {}`, context),
parse(
createSourceFile(
"untitled.js",
`export function ${id.name}() {}`,
ScriptTarget.Latest
),
context
),
isIdentifier
),
module: imp.module,
Expand All @@ -33,7 +42,10 @@ export function resolveImportedId(
console.error(e);
return;
}
const parsed = parse(content, context);
const parsed = parse(
createSourceFile(imp.path, content, ScriptTarget.Latest),
context
);
const identifierInParsed = findIdentifiersInChildren(
impt.specifiers[0].local.name,
parsed.body
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"declaration": true,
"outDir": "./dist",
"allowJs": false,
"jsx": "preserve",
"strict": true,
"baseUrl": "./",
"paths": {
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,19 @@
dependencies:
undici-types "~5.26.4"

"@types/prop-types@*":
version "15.7.13"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.13.tgz#2af91918ee12d9d32914feb13f5326658461b451"
integrity sha512-hCZTSvwbzWGvhqxp/RqVqwU999pBf2vp7hzIjiYOsl8wqOmUxkQ6ddw1cV3l8811+kdUFus/q4d1Y3E3SyEifA==

"@types/react@^18.3.12":
version "18.3.12"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.3.12.tgz#99419f182ccd69151813b7ee24b792fe08774f60"
integrity sha512-D2wOSq/d6Agt28q7rSI3jhU7G6aiuzljDGZ2hTZHIkrTLUI+AF3WMeKkEZ9nN2fkBAlcktT6vcZjDFiIhMYEQw==
dependencies:
"@types/prop-types" "*"
csstype "^3.0.2"

"@types/semver@^7.3.12":
version "7.5.8"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.8.tgz#8268a8c57a3e4abd25c165ecd36237db7948a55e"
Expand Down Expand Up @@ -819,6 +832,11 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

csstype@^3.0.2:
version "3.1.3"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.3.tgz#d80ff294d114fb0e6ac500fbf85b60137d7eff81"
integrity sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==

debug@^4.1.1:
version "4.3.5"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.5.tgz#e83444eceb9fedd4a1da56d671ae2446a01a6e1e"
Expand Down

0 comments on commit a7e9e53

Please sign in to comment.