Skip to content

Commit

Permalink
add support for require imports (#22)
Browse files Browse the repository at this point in the history
* add support for require imports

* small ui fix, finish implementation for supporting require in javasctipt

* changelog
  • Loading branch information
florianbgt authored Oct 30, 2024
1 parent 2fe8179 commit ea0bd92
Show file tree
Hide file tree
Showing 6 changed files with 550 additions and 322 deletions.
2 changes: 1 addition & 1 deletion packages/app/src/components/ApiTree/ApiTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export default function ApiTree(props: {
>
<div className="absolute bottom-6 inset-x-4 z-10 flex justify-around">
<div className="flex gap-3 items-center">
<div className="bg-background-light dark:bg-background-dark flex gap-4 py-2 px-3 rounded-md">
<div className="bg-background-light dark:bg-background-dark flex gap-4 py-2 px-3 rounded-lg">
<Button
size="1"
variant="ghost"
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

Add support for require import in javascript

## [0.0.14] - 2024-10-30

Fix padding and size discrepancy with toolbar
Expand Down
51 changes: 13 additions & 38 deletions packages/cli/src/helper/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import fs from "fs";
import Parser from "tree-sitter";
import {
extractJavascriptFileImports,
removeInvalidJavascriptFileImports,
removeJavascriptAnnotations,
removeJavascriptDeletedImportUsage,
removeUnusedJavascriptImports,
} from "./languages/javascript";
import { cleanupJavascriptFile } from "./languages/javascript/cleanup";
import { extractJavascriptFileImports } from "./languages/javascript/imports";

import { getParserLanguageFromFile, resolveFilePath } from "./file";
import { Dependencies, Group } from "./types";

Expand All @@ -28,11 +24,10 @@ export function getDependencyTree(filePath: string): Dependencies {
parser.setLanguage(language);

const sourceCode = fs.readFileSync(currentFilePath, "utf8");
const tree = parser.parse(sourceCode);

let imports: string[] = [];
if (["javascript", "typescript"].includes(language.name)) {
imports = extractJavascriptFileImports(tree.rootNode);
imports = extractJavascriptFileImports(parser, sourceCode);
} else {
throw new Error(`Unsupported language: ${language.name}`);
}
Expand Down Expand Up @@ -61,51 +56,31 @@ export function cleanupFile(filePath: string, group: Group) {
parser.setLanguage(language);

const sourceCode = fs.readFileSync(filePath, "utf8");
let tree = parser.parse(sourceCode);

let dependencies: string[] = [];
if (["javascript", "typescript"].includes(language.name)) {
dependencies = extractJavascriptFileImports(tree.rootNode);
dependencies = extractJavascriptFileImports(parser, sourceCode);
} else {
throw new Error(`Unsupported language: ${language.language}`);
}

// Check if we can resolve the path for each dependency
// If we cannot, we need to remove it
// Check if we can resolve the path for each dependency. If we cannot, we need to remove it
const invalidDependencies = dependencies.filter(
(dep) => !resolveFilePath(dep, filePath),
);

let updatedSourceCode: string;

if (["javascript", "typescript"].includes(language.name)) {
const newSourceCode = removeJavascriptAnnotations(
tree.rootNode,
updatedSourceCode = cleanupJavascriptFile(
parser,
sourceCode,
group,
invalidDependencies,
);
tree = parser.parse(newSourceCode);

const { updatedSourceCode, removedImportsNames } =
removeInvalidJavascriptFileImports(
tree.rootNode,
newSourceCode,
invalidDependencies,
);
tree = parser.parse(updatedSourceCode);

let finalUpdatedSourceCode = removeJavascriptDeletedImportUsage(
tree.rootNode,
updatedSourceCode,
removedImportsNames,
);
tree = parser.parse(finalUpdatedSourceCode);

finalUpdatedSourceCode = removeUnusedJavascriptImports(
tree.rootNode,
finalUpdatedSourceCode,
);

fs.writeFileSync(filePath, finalUpdatedSourceCode, "utf8");
} else {
throw new Error(`Unsupported language: ${language.language}`);
}

fs.writeFileSync(filePath, updatedSourceCode, "utf8");
}
283 changes: 0 additions & 283 deletions packages/cli/src/helper/languages/javascript.ts

This file was deleted.

Loading

0 comments on commit ea0bd92

Please sign in to comment.