-
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 3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* 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 { AST_NODE_TYPES, ESLintUtils } from '@typescript-eslint/utils'; | ||
|
||
export const esmMessageImport = ESLintUtils.RuleCreator.withoutDocs({ | ||
meta: { | ||
docs: { | ||
description: | ||
'Looks for the verbose `Messages.importMessagesDirectory(dirname(fileURLToPath(import.meta.url)))` to offer a simpler alternative', | ||
recommended: 'error', | ||
}, | ||
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.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)'), | ||
}); | ||
} | ||
}, | ||
ImportDeclaration(node): void { | ||
if ( | ||
node.specifiers.length === 1 && | ||
node.specifiers[0].type === AST_NODE_TYPES.ImportSpecifier && | ||
node.specifiers[0].local.type === AST_NODE_TYPES.Identifier && | ||
((node.source.value === 'node:url' && | ||
node.specifiers[0].local.name === 'fileURLToPath' && | ||
Array.from( | ||
context | ||
.getSourceCode() | ||
.getText() | ||
.matchAll(/fileURLToPath/g) | ||
).length === 1) || | ||
(node.source.value === 'node:path' && | ||
node.specifiers[0].local.name === 'dirname' && | ||
Array.from( | ||
context | ||
.getSourceCode() | ||
.getText() | ||
.matchAll(/dirname/g) | ||
).length === 1)) && | ||
// we've already removed the old way of doing it | ||
context.getSourceCode().getText().includes('Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)') | ||
) { | ||
return context.report({ | ||
node, | ||
messageId: 'unnecessaryImport', | ||
fix: (fixer) => fixer.remove(node), | ||
}); | ||
} | ||
}, | ||
// 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)'), | ||
// }); | ||
// } | ||
// }, | ||
}; | ||
}, | ||
}); |
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,87 @@ | ||
/* | ||
* 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 | ||
*/ | ||
import { ESLintUtils } from '@typescript-eslint/utils'; | ||
import { esmMessageImport } from '../../src/rules/esm-message-import'; | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('esmMessageImport', esmMessageImport, { | ||
valid: [ | ||
{ | ||
name: 'new loader method', | ||
code: 'Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)', | ||
}, | ||
{ | ||
name: 'old loader method but not ESM', | ||
code: 'Messages.importMessagesDirectory(__dirname)', | ||
}, | ||
{ | ||
name: 'leave the unrelated import alone', | ||
code: ` | ||
import { dirname } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
`, | ||
}, | ||
{ | ||
name: 'imports used by other code', | ||
code: ` | ||
import { dirname } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url) | ||
|
||
const foo = dirname('foo/bar') | ||
const foo = fileURLToPath('file:///foo/bar') | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
name: 'new loader with extra imports updates the import', | ||
errors: [ | ||
{ | ||
messageId: 'changeImport', | ||
}, | ||
], | ||
code: ` | ||
import { dirname } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
Messages.importMessagesDirectory(dirname(fileURLToPath(import.meta.url))) | ||
`, | ||
// other code (ex: prettier) can handle the extra whitespaces | ||
output: ` | ||
import { dirname } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url) | ||
`, | ||
}, | ||
{ | ||
name: 'new loader with extra imports updates the import', | ||
errors: [ | ||
{ | ||
messageId: 'unnecessaryImport', | ||
}, | ||
{ | ||
messageId: 'unnecessaryImport', | ||
}, | ||
], | ||
code: ` | ||
import { dirname } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url) | ||
`, | ||
// other code (ex: prettier) can handle the extra whitespaces | ||
output: ` | ||
|
||
|
||
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url) | ||
`, | ||
}, | ||
], | ||
}); |
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