Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update: Resolve npm installed formatters (#5900)
- Loading branch information
Tom Erik Støwer
committed
Oct 28, 2017
1 parent
9cf4ebe
commit aaa484f
Showing
9 changed files
with
223 additions
and
70 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
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,70 @@ | ||
/** | ||
* @fileoverview Common helpers naming of plugins and formatters | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Private | ||
//------------------------------------------------------------------------------ | ||
|
||
const NAMESPACE_REGEX = /^@.*\//i; | ||
|
||
/** | ||
* 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}`; | ||
} | ||
|
||
/** | ||
* Adds a namespace before a term | ||
* @param {string} namespace The namespace to add before the term | ||
* @param {string} term The term which may have a namespace | ||
* @returns {string} The term with namespace | ||
*/ | ||
function addNamespaceToTerm(namespace, term) { | ||
return term.match(NAMESPACE_REGEX) ? term : `${namespace}${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) { | ||
return term.match(NAMESPACE_REGEX) ? term.match(NAMESPACE_REGEX)[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 = { | ||
removePrefixFromTerm, | ||
addPrefixToTerm, | ||
addNamespaceToTerm, | ||
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
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,75 @@ | ||
/** | ||
* @fileoverview Tests for naming util | ||
*/ | ||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const assert = require("chai").assert, | ||
naming = require("../../../lib/util/naming"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
describe("naming", () => { | ||
describe("removePrefixFromTerm()", () => { | ||
it("should remove prefix when passed a term with a prefix", () => { | ||
const pluginName = naming.removePrefixFromTerm("eslint-plugin-", "eslint-plugin-test"); | ||
|
||
assert.strictEqual(pluginName, "test"); | ||
}); | ||
|
||
it("should not modify term when passed a term without a prefix", () => { | ||
const pluginName = naming.removePrefixFromTerm("eslint-plugin-", "test"); | ||
|
||
assert.strictEqual(pluginName, "test"); | ||
}); | ||
}); | ||
|
||
describe("addPrefixToTerm()", () => { | ||
it("should add prefix when passed a term without a prefix", () => { | ||
const pluginName = naming.addPrefixToTerm("eslint-plugin-", "test"); | ||
|
||
assert.strictEqual(pluginName, "eslint-plugin-test"); | ||
}); | ||
|
||
it("should not modify term when passed a term with a prefix", () => { | ||
const pluginName = naming.addPrefixToTerm("eslint-plugin-", "eslint-plugin-test"); | ||
|
||
assert.strictEqual(pluginName, "eslint-plugin-test"); | ||
}); | ||
}); | ||
|
||
describe("addNamespaceToTerm", () => { | ||
it("should add namespace when passed a term without a namespace", () => { | ||
const pluginName = naming.addNamespaceToTerm("@namespace/", "eslint-plugin-test"); | ||
|
||
assert.strictEqual(pluginName, "@namespace/eslint-plugin-test"); | ||
}); | ||
|
||
it("should not modify term when passed a term with a namespace", () => { | ||
const pluginName = naming.addNamespaceToTerm("@othernamespace/", "@namespace/eslint-plugin-test"); | ||
|
||
assert.strictEqual(pluginName, "@namespace/eslint-plugin-test"); | ||
}); | ||
}); | ||
|
||
describe("getNamespace()", () => { | ||
it("should remove namepace when passed with namepace", () => { | ||
const namespace = naming.getNamespaceFromTerm("@namepace/eslint-plugin-test"); | ||
|
||
assert.strictEqual(namespace, "@namepace/"); | ||
}); | ||
}); | ||
|
||
describe("removeNamespace()", () => { | ||
it("should remove namepace when passed with namepace", () => { | ||
const namespace = naming.removeNamespaceFromTerm("@namepace/eslint-plugin-test"); | ||
|
||
assert.strictEqual(namespace, "eslint-plugin-test"); | ||
}); | ||
}); | ||
}); |