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
Update: Resolve npm installed formatters (#5900) #9464
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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,112 @@ | ||
/** | ||
* @fileoverview Common helpers for naming of plugins, formatters and configs | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const pathUtil = require("../util/path-util"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Private | ||
//------------------------------------------------------------------------------ | ||
|
||
const NAMESPACE_REGEX = /^@.*\//i; | ||
|
||
/** | ||
* Brings package name to correct format based on prefix | ||
* @param {string} name The name of the package. | ||
* @param {string} prefix Can be either "eslint-plugin", "eslint-config" or "eslint-formatter" | ||
* @returns {string} Normalized name of the package | ||
* @private | ||
*/ | ||
function normalizePackageName(name, prefix) { | ||
|
||
/** | ||
* On Windows, name can come in with Windows slashes instead of Unix slashes. | ||
* Normalize to Unix first to avoid errors later on. | ||
* https://github.com/eslint/eslint/issues/5644 | ||
*/ | ||
if (name.indexOf("\\") > -1) { | ||
name = pathUtil.convertPathToPosix(name); | ||
} | ||
|
||
if (name.charAt(0) === "@") { | ||
|
||
/** | ||
* it's a scoped package | ||
* package name is the prefix, or just a username | ||
*/ | ||
const scopedPackageShortcutRegex = new RegExp(`^(@[^/]+)(?:/(?:${prefix})?)?$`), | ||
scopedPackageNameRegex = new RegExp(`^${prefix}(-|$)`); | ||
|
||
if (scopedPackageShortcutRegex.test(name)) { | ||
name = name.replace(scopedPackageShortcutRegex, `$1/${prefix}`); | ||
} else if (!scopedPackageNameRegex.test(name.split("/")[1])) { | ||
|
||
/** | ||
* for scoped packages, insert the prefix after the first / unless | ||
* the path is already @scope/eslint or @scope/eslint-xxx-yyy | ||
*/ | ||
name = name.replace(/^@([^/]+)\/(.*)$/, `@$1/${prefix}-$2`); | ||
} | ||
} else if (name.indexOf(`${prefix}-`) !== 0) { | ||
name = `${prefix}-${name}`; | ||
} | ||
|
||
return name; | ||
} | ||
|
||
/** | ||
* Removes the prefix from a term. | ||
* @param {string} prefix The prefix to remove. | ||
* @param {string} term The term which may have the prefix. | ||
* @returns {string} The term without prefix. | ||
*/ | ||
function removePrefixFromTerm(prefix, term) { | ||
return term.startsWith(prefix) ? term.slice(prefix.length) : term; | ||
} | ||
|
||
/** | ||
* Adds a prefix to a term. | ||
* @param {string} prefix The prefix to add. | ||
* @param {string} term The term which may not have the prefix. | ||
* @returns {string} The term with prefix. | ||
*/ | ||
function addPrefixToTerm(prefix, term) { | ||
return term.startsWith(prefix) ? term : `${prefix}${term}`; | ||
} | ||
|
||
/** | ||
* Gets the scope (namespace) of a term. | ||
* @param {string} term The term which may have the namespace. | ||
* @returns {string} The namepace of the term if it has one. | ||
*/ | ||
function getNamespaceFromTerm(term) { | ||
const match = term.match(NAMESPACE_REGEX); | ||
|
||
return match ? match[0] : ""; | ||
} | ||
|
||
/** | ||
* Removes the namespace from a term. | ||
* @param {string} term The term which may have the namespace. | ||
* @returns {string} The name of the plugin without the namespace. | ||
*/ | ||
function removeNamespaceFromTerm(term) { | ||
return term.replace(NAMESPACE_REGEX, ""); | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Public Interface | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
normalizePackageName, | ||
removePrefixFromTerm, | ||
addPrefixToTerm, | ||
getNamespaceFromTerm, | ||
removeNamespaceFromTerm | ||
}; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/cli-engine/node_modules/@somenamespace/eslint-formatter-foo/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/fixtures/cli-engine/node_modules/eslint-formatter-bar/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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.
Nice! Good refactoring. However, I think
config/config-file.js
can also be updated to use this.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.
normalizePackageName
?It seems after looking more closely that
normalizePackageName
can probably be reused in some way to make the logic in getFormatter a little nicer. So movingnormalizePackageName
in some way to the naming util looks like a good option.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.
Ok I moved the normalizePackageName to the naming util altogether. It simplified the logic in getFormatter quite a bit.