From a3be151a6b05c58915910aa92f15a76b6561fdf0 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Fri, 8 May 2020 09:52:47 -0400 Subject: [PATCH] chore: rename `registerAlias` to `registerAliases` Plural form is clearly better as it's not surprising to the reader to see it being passed an array - where as the singular form might have been. Meanwhile it's also easy to assume that it also supports arrays of any size - including an array with a singular alias. The fact that it can magically accept a string as the first argument might not be obvious, but we document it and even if one didn't know this they could still use the array form of the API without any issue by passing a one item array. --- CHANGES.md | 2 +- docs/api.rst | 4 ++-- src/highlight.js | 13 ++++++------- test/api/index.js | 1 + test/api/registerAlias.js | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index e6348fdfe3..2b74d3d100 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,7 +4,7 @@ Parser Engine: - (parser) Adds `keywords.$pattern` key to grammar definitions (#2519) [Josh Goebel][] - (parser) Adds SHEBANG utility mode [Josh Goebel][] -- (parser) Adds `registerAlias` method (#2540) [Taufik Nurrohman][] +- (parser) Adds `registerAliases` method (#2540) [Taufik Nurrohman][] - (enh) Added `on:begin` callback for modes (#2261) [Josh Goebel][] - (enh) Added `on:end` callback for modes (#2261) [Josh Goebel][] - (enh) Added ability to programatically ignore begin and end matches (#2261) [Josh Goebel][] diff --git a/docs/api.rst b/docs/api.rst index a219bfa9ea..41f7014ad6 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -112,8 +112,8 @@ Adds new language to the library under the specified name. Used mostly internall to use common regular expressions defined within it. -``registerAlias(alias|aliases, {languageName})`` ------------------------------------------------- +``registerAliases(alias|aliases, {languageName})`` +-------------------------------------------------- Adds new language alias or aliases to the library for the specified language name defined under ``languageName`` key. diff --git a/src/highlight.js b/src/highlight.js index bc34fb4fc3..2b3f776429 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -658,7 +658,7 @@ const HLJS = function(hljs) { lang.rawDefinition = language.bind(null, hljs); if (lang.aliases) { - lang.aliases.forEach(function(alias) { aliases[alias] = name; }); + registerAliases(lang.aliases, { languageName: name }); } } @@ -685,12 +685,11 @@ const HLJS = function(hljs) { return languages[name] || languages[aliases[name]]; } - function registerAlias(alias, {languageName}) { - let list = alias; - if (typeof list === 'string') { - list = [alias] + function registerAliases(aliasList, {languageName}) { + if (typeof aliasList === 'string') { + aliasList = [aliasList] } - list.forEach(alias => aliases[alias] = languageName); + aliasList.forEach(alias => aliases[alias] = languageName); } function autoDetection(name) { @@ -724,7 +723,7 @@ const HLJS = function(hljs) { registerLanguage, listLanguages, getLanguage, - registerAlias, + registerAliases, requireLanguage, autoDetection, inherit, diff --git a/test/api/index.js b/test/api/index.js index 043bb65b4d..0e4b596dcb 100644 --- a/test/api/index.js +++ b/test/api/index.js @@ -12,4 +12,5 @@ describe('hljs', function() { require('./highlight'); require('./fixmarkup'); require('./keywords'); + require('./registerAlias'); }); diff --git a/test/api/registerAlias.js b/test/api/registerAlias.js index 3e03136fe4..aede3ebb7a 100644 --- a/test/api/registerAlias.js +++ b/test/api/registerAlias.js @@ -3,9 +3,9 @@ const hljs = require('../../build'); const should = require('should'); -describe('.registerAlias()', () => { +describe('.registerAliases()', () => { it('should get an existing language by alias', () => { - hljs.registerAlias('jquery', { + hljs.registerAliases('jquery', { languageName: 'javascript' }); const result = hljs.getLanguage('jquery'); @@ -14,7 +14,7 @@ describe('.registerAlias()', () => { }); it('should get an existing language by aliases', () => { - hljs.registerAlias(['jquery', 'jqueryui'], { + hljs.registerAliases(['jquery', 'jqueryui'], { languageName: 'javascript' }); const result = hljs.getLanguage('jquery');