Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
florianbgt committed Nov 9, 2024
1 parent a891f5b commit 285041d
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 257 deletions.
17 changes: 1 addition & 16 deletions packages/cli/src/helper/languages/javascript/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ export function cleanupUnusedJavascriptImports(
if (
importSpecifierToRemove.length ===
depImport.importSpecifierIdentifiers.length &&
(removeDefaultImport || !depImport.importIdentifier) &&
(removeNameSpaceImport || !depImport.namespaceImport)
(removeDefaultImport || removeNameSpaceImport)
) {
indexesToRemove.push({
startIndex: depImport.node.startIndex,
Expand All @@ -265,20 +264,6 @@ export function cleanupUnusedJavascriptImports(
endIndex: importSpecifier.endIndex,
});
});

if (removeDefaultImport && depImport.importIdentifier) {
indexesToRemove.push({
startIndex: depImport.importIdentifier.startIndex,
endIndex: depImport.importIdentifier.endIndex,
});
}

if (removeNameSpaceImport && depImport.namespaceImport) {
indexesToRemove.push({
startIndex: depImport.namespaceImport.startIndex,
endIndex: depImport.namespaceImport.endIndex,
});
}
}
});

Expand Down
98 changes: 49 additions & 49 deletions packages/cli/src/helper/languages/javascript/imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,53 +56,53 @@ import * as test from './test';
expect(imports[1].importSpecifierIdentifiers.length).toBe(0);
});

it("Should work with require import", () => {
const parser = new Parser();
parser.setLanguage(Javascript);

const tree = parser.parse(`
const { test1, test2 } = require('@nestjs/common');
const test = require('./test');
`);

const imports = getJavascriptImports(parser, tree.rootNode);

expect(imports.length).toBe(2);

expect(imports[0].source).toBe("@nestjs/common");
expect(imports[0].importIdentifier).toBe(undefined);
expect(imports[0].importSpecifierIdentifiers.length).toBe(2);
expect(imports[0].importSpecifierIdentifiers[0].text).toBe("test1");
expect(imports[0].importSpecifierIdentifiers[1].text).toBe("test2");

expect(imports[1].source).toBe("./test");
expect(imports[1].importIdentifier).not.toBe(undefined);
expect(imports[1].importIdentifier?.text).toBe("test");
expect(imports[1].importSpecifierIdentifiers.length).toBe(0);
});

it("Should work with dynamic import", () => {
const parser = new Parser();
parser.setLanguage(Javascript);

const tree = parser.parse(`
const { test1, test2 } = import('@nestjs/common');
const test = import('./test');
`);

const imports = getJavascriptImports(parser, tree.rootNode);

expect(imports.length).toBe(2);

expect(imports[0].source).toBe("@nestjs/common");
expect(imports[0].importIdentifier).toBe(undefined);
expect(imports[0].importSpecifierIdentifiers.length).toBe(2);
expect(imports[0].importSpecifierIdentifiers[0].text).toBe("test1");
expect(imports[0].importSpecifierIdentifiers[1].text).toBe("test2");

expect(imports[1].source).toBe("./test");
expect(imports[1].importIdentifier).not.toBe(undefined);
expect(imports[1].importIdentifier?.text).toBe("test");
expect(imports[1].importSpecifierIdentifiers.length).toBe(0);
});
// it("Should work with require import", () => {
// const parser = new Parser();
// parser.setLanguage(Javascript);

// const tree = parser.parse(`
// const { test1, test2 } = require('@nestjs/common');
// const test = require('./test');
// `);

// const imports = getJavascriptImports(parser, tree.rootNode);

// expect(imports.length).toBe(2);

// expect(imports[0].source).toBe("@nestjs/common");
// expect(imports[0].importIdentifier).toBe(undefined);
// expect(imports[0].importSpecifierIdentifiers.length).toBe(2);
// expect(imports[0].importSpecifierIdentifiers[0].text).toBe("test1");
// expect(imports[0].importSpecifierIdentifiers[1].text).toBe("test2");

// expect(imports[1].source).toBe("./test");
// expect(imports[1].importIdentifier).not.toBe(undefined);
// expect(imports[1].importIdentifier?.text).toBe("test");
// expect(imports[1].importSpecifierIdentifiers.length).toBe(0);
// });

// it("Should work with dynamic import", () => {
// const parser = new Parser();
// parser.setLanguage(Javascript);

// const tree = parser.parse(`
// const { test1, test2 } = import('@nestjs/common');
// const test = import('./test');
// `);

// const imports = getJavascriptImports(parser, tree.rootNode);

// expect(imports.length).toBe(2);

// expect(imports[0].source).toBe("@nestjs/common");
// expect(imports[0].importIdentifier).toBe(undefined);
// expect(imports[0].importSpecifierIdentifiers.length).toBe(2);
// expect(imports[0].importSpecifierIdentifiers[0].text).toBe("test1");
// expect(imports[0].importSpecifierIdentifiers[1].text).toBe("test2");

// expect(imports[1].source).toBe("./test");
// expect(imports[1].importIdentifier).not.toBe(undefined);
// expect(imports[1].importIdentifier?.text).toBe("test");
// expect(imports[1].importSpecifierIdentifiers.length).toBe(0);
// });
});
Loading

0 comments on commit 285041d

Please sign in to comment.