-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sm/simplify-message-imports #358
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4249b80
docs: note about arguing with dev-scripts
mshanemc e749190
feat: simplify ESM message imports
mshanemc 0d1cefd
test: show both the --fix and not --fix versions
mshanemc 4f4f8c1
chore: bump core
mshanemc fcb690a
refactor: ts-eslint6
mshanemc daa0c5c
ci: deduplicate in case dependencies need to go up
mshanemc 4480486
Merge remote-tracking branch 'origin/main' into sm/simplify-message-i…
mshanemc ad0b52a
refactor: handle removing one of multiple import specifiers
mshanemc e886ec9
refactor: handle named imports
mshanemc 058b380
refactor: deprecated method
mshanemc f790043
refactor: handle 1 remaining default import
mshanemc 9c48cb9
refactor: fewer consts
mshanemc 4ac2e33
chore(release): 1.16.16-qa.0 [skip ci]
svc-cli-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Looks for the verbose `Messages.importMessagesDirectory(dirname(fileURLToPath(import.meta.url)))` to offer a simpler alternative (`sf-plugin/esm-message-import`) | ||
|
||
💼 This rule is enabled in the following configs: 📚 `library`, ✈️ `migration`, ✅ `recommended`. | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# Use loaded messages and separate files for messages (`sf-plugin/no-hardcoded-messages-flags`) | ||
# Use loaded messages and separate files for messages. Follow the message naming guidelines (`sf-plugin/no-hardcoded-messages-flags`) | ||
|
||
⚠️ This rule _warns_ in the following configs: ✈️ `migration`, ✅ `recommended`. | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
/* eslint-disable complexity */ | ||
import { RuleCreator } from '@typescript-eslint/utils/eslint-utils'; | ||
import { ASTUtils, AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
|
||
export const esmMessageImport = RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: | ||
'Looks for the verbose `Messages.importMessagesDirectory(dirname(fileURLToPath(import.meta.url)))` to offer a simpler alternative', | ||
recommended: 'strict', | ||
}, | ||
messages: { | ||
changeImport: 'use Messages.importMessagesDirectoryFromMetaUrl(import.meta.url) instead', | ||
unnecessaryImport: 'the only code using this import was removed by this rule', | ||
}, | ||
fixable: 'code', | ||
type: 'problem', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
MemberExpression(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.sourceCode.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)'), | ||
}); | ||
} | ||
}, | ||
ImportDeclaration(node): void { | ||
const allCode = context.sourceCode.getText(); | ||
// true for all cases | ||
if ( | ||
((node.source.value === 'node:url' && | ||
node.specifiers.some((s) => s.local.name === 'fileURLToPath') && | ||
// it's the only reference to fileURLToPath | ||
Array.from(allCode.matchAll(/fileURLToPath/g)).length === 1) || | ||
(node.source.value === 'node:path' && | ||
node.specifiers.some((s) => s.local.name === 'dirname') && | ||
// it's the only reference to dirname | ||
Array.from(allCode.matchAll(/dirname/g)).length === 1)) && | ||
// we've already removed the old way of doing it | ||
allCode.includes('Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)') | ||
) { | ||
if ( | ||
// case 1: single specifier removes the entire import line | ||
node.specifiers.length === 1 && | ||
node.specifiers[0].type === AST_NODE_TYPES.ImportSpecifier && | ||
node.specifiers[0].local.type === AST_NODE_TYPES.Identifier | ||
) { | ||
return context.report({ | ||
node, | ||
messageId: 'unnecessaryImport', | ||
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' | ||
); | ||
|
||
return context.report({ | ||
node, | ||
messageId: 'unnecessaryImport', | ||
fix: (fixer) => | ||
replacementSpecifiers.every(ASTUtils.isNodeOfType(AST_NODE_TYPES.ImportDefaultSpecifier)) | ||
? fixer.replaceText( | ||
node, | ||
`import ${replacementSpecifiers[0].local.name} from '${node.source.value}'` | ||
) | ||
: fixer.replaceTextRange( | ||
[node.specifiers[0].range[0], node.specifiers[node.specifiers.length - 1].range[1]], | ||
replacementSpecifiers.map((s) => context.sourceCode.getText(s)).join(', ') | ||
), | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh, the actual rule that led to this PR