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 22, 2017
1 parent
0f71fef
commit 6e47a26
Showing
9 changed files
with
197 additions
and
69 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,59 @@ | ||
/** | ||
* @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}`; | ||
} | ||
|
||
/** | ||
* 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, | ||
getNamespaceFromTerm, | ||
removeNamespaceFromTerm | ||
}; |
1 change: 1 addition & 0 deletions
1
tests/fixtures/cli-engine/@somenamespace/eslint-formatter-foo/index.js
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 @@ | ||
module.exports = function() {}; |
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 @@ | ||
module.exports = function() {}; |
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,61 @@ | ||
/** | ||
* @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("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"); | ||
}); | ||
}); | ||
}); |