Skip to content

Commit

Permalink
refactor: handle named imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 13, 2023
1 parent ad0b52a commit e886ec9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
33 changes: 2 additions & 31 deletions src/rules/esm-message-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ export const esmMessageImport = RuleCreator.withoutDocs({
fix: (fixer) => fixer.remove(node),
});
} else {
// case 2, just remove the 1 unused specifier
if (node.specifiers.length > 1) {
const replacementSpecifiers = node.specifiers
.filter((s) => s.local.name !== 'dirname' && s.local.name !== 'fileURLToPath')
.map((s) => s.local.name)
.map((s) => context.sourceCode.getText(s))
.join(', ');
const replacementRange = [
node.specifiers[0].range[0],
Expand All @@ -90,36 +91,6 @@ export const esmMessageImport = RuleCreator.withoutDocs({
}
}
},
// now we're going to clean up unused imports if they're left over
// ImportDeclaration(node): void {
// // verify it extends SfCommand
// if (node.source.value === '@salesforce/command') {
// context.report({
// node,
// messageId: 'import',
// fix: (fixer) =>
// fixer.replaceText(node, "import {Flags, SfCommand} from '@salesforce/sf-plugins-core';"),
// });
// }
// },(node): void {
// if (
// node.object.type === AST_NODE_TYPES.Identifier &&
// node.object.name === 'Messages' &&
// node.property.type === AST_NODE_TYPES.Identifier &&
// node.property.name === 'importMessagesDirectory' &&
// node.parent?.parent &&
// context.getSourceCode().getText(node.parent.parent).includes('dirname(fileURLToPath(import.meta.url))')
// ) {
// const toReplace = node.parent.parent;
// // we never found the message at all, we can report and exit
// return context.report({
// node,
// messageId: 'changeImport',
// fix: (fixer) =>
// fixer.replaceText(toReplace, 'Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)'),
// });
// }
// },
};
},
});
17 changes: 17 additions & 0 deletions test/rules/esm-message-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
output: `
import { join } from 'node:path'
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
`,
},
{
name: 'new loader with extra imports removes only the dirname import (when last)',
errors: [
{
messageId: 'unnecessaryImport',
},
],
code: `
import { dirname, resolve as pathResolve, join } from 'node:path'
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
`,
// other code (ex: prettier) can handle the extra whitespaces
output: `
import { resolve as pathResolve, join } from 'node:path'
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
`,
},
Expand Down

0 comments on commit e886ec9

Please sign in to comment.