-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
257 additions
and
45 deletions.
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
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) | ||
`, | ||
}, | ||
], | ||
}); |