gulp-i18n-update-localization-ids
Advanced tools
Comparing version 1.4.0 to 1.5.0
29
index.js
@@ -26,3 +26,3 @@ 'use strict'; | ||
const encoding = option(options.encoding, 'utf8'); | ||
const keyAttribute = option(options.keyAttribute, 't'); | ||
const keyAttribute = option(options.keyAttribute, 't'); | ||
@@ -32,4 +32,11 @@ const emitCondition = option(options.emit, 'always'); | ||
throw new TypeError('options.emit must be "always" or "onChangeOnly".'); | ||
} | ||
} | ||
const exceptions = option(options.exceptions, {}); | ||
for (const name in exceptions) { | ||
if (!['throw', 'warn', 'ignore'].includes(exceptions[name])) { | ||
throw new TypeError(`options.exceptions.${name} must be "throw", "warn" or "ignore".`); | ||
} | ||
} | ||
const LocalizationKey = option(options.LocalizationKey, DefaultLocalizationKey); | ||
@@ -96,5 +103,5 @@ | ||
} else if (hasText) { | ||
throw new DomRelatedError(inFile, node, 'Non-whitelisted tag has text content.'); | ||
new DomRelatedError(inFile, node, 'Non-whitelisted tag has text content.').emit(exceptions.illegalContent); | ||
} else if (getAttr(node, keyAttribute)) { | ||
throw new DomRelatedError(inFile, node, `Non-whitelisted tag has a ${keyAttribute} attribute.`); | ||
new DomRelatedError(inFile, node, `Non-whitelisted tag has a ${keyAttribute} attribute.`).emit(exceptions.illegalAttribute); | ||
} | ||
@@ -139,5 +146,11 @@ } | ||
} else if (originalKey.has('html') || originalKey.has('text')) { | ||
throw new DomRelatedError(inFile, node, 'Content is already localized, but not whitelisted.'); | ||
new DomRelatedError(inFile, node, 'Content is already localized, but not whitelisted.').emit(exceptions.illegalContent); | ||
if (originalKey.has('html')) { | ||
newKey.set('html', originalKey.get('html')); | ||
} | ||
if (originalKey.has('text')) { | ||
newKey.set('text', originalKey.get('text')); | ||
} | ||
} else if (hasText) { | ||
throw new DomRelatedError(inFile, node, 'Content is not whitelisted to be localized.'); | ||
new DomRelatedError(inFile, node, 'Content is not whitelisted to be localized.').emit(exceptions.illegalContent); | ||
} | ||
@@ -152,3 +165,3 @@ for (const attr of whitelisted.attrs) { | ||
if (attr !== 'text' && attr !== 'html' && !whitelisted.attrs.has(attr)) { | ||
throw new DomRelatedError(inFile, node, `Attribute "${attr}" is already localized, but not whitelisted.`); | ||
new DomRelatedError(inFile, node, `Attribute "${attr}" is already localized, but not whitelisted.`).emit(exceptions.illegalAttribute); | ||
} | ||
@@ -177,2 +190,2 @@ } | ||
module.exports.mergeOptions = mergeOptions; | ||
module.exports.prefixFilename = prefixFilename; | ||
module.exports.prefixFilename = prefixFilename; |
'use strict'; | ||
const PluginError = require('plugin-error'); | ||
const colors = require('ansi-colors'); | ||
const log = require('fancy-log'); | ||
const {name} = require('../package.json'); | ||
@@ -10,3 +12,21 @@ | ||
super(name, `${message} (${vinyl.path} at ln=${info.startLine}, col=${info.startCol})`); | ||
} | ||
} | ||
/** | ||
* Emit this error based on the specified configuration. | ||
* @param {'throw' | 'warn' | 'ignore'} behaviour The configured behaviour. Default is `'throw'` | ||
*/ | ||
emit(behaviour = 'throw') { | ||
switch (behaviour) { | ||
case 'warn': | ||
log.warn(colors.yellow(this.message)); | ||
break; | ||
case 'ignore': | ||
break; | ||
default: | ||
throw this; | ||
} | ||
} | ||
}; |
'use strict'; | ||
function mergeIgnore(a, b) { | ||
return a === undefined ? b : (b === undefined ? a : [a, b]); | ||
function mergeIgnore(defaults, overrides) { | ||
return defaults === undefined ? overrides : (overrides === undefined ? defaults : [defaults, overrides]); | ||
} | ||
function mergeExceptions(defaults, overrides) { | ||
if (defaults === undefined) { | ||
return overrides; | ||
} | ||
if (overrides === undefined) { | ||
return defaults; | ||
} | ||
return Object.assign({}, defaults, overrides); | ||
} | ||
module.exports = (defaults, overrides) => { | ||
@@ -18,4 +28,9 @@ const merged = { | ||
merged.ignore = ignore; | ||
} | ||
} | ||
const exceptions = mergeExceptions(defaults.exceptions, overrides.exceptions); | ||
if (exceptions !== undefined) { | ||
merged.exceptions = exceptions; | ||
} | ||
for (const key of ['emit', 'idTemplate', 'keyAttribute', 'encoding', 'LocalizationKey']) { | ||
@@ -22,0 +37,0 @@ if (overrides[key] !== undefined) { |
{ | ||
"name": "gulp-i18n-update-localization-ids", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Update i18n localization ids in html files.", | ||
@@ -32,2 +32,4 @@ "keywords": [ | ||
"dependencies": { | ||
"ansi-colors": "^3.2.4", | ||
"fancy-log": "^1.3.3", | ||
"parse5": "^5.1.0", | ||
@@ -34,0 +36,0 @@ "plugin-error": "^1.0.1", |
@@ -114,2 +114,18 @@ # gulp-i18n-update-localization-ids | ||
### `options.exceptions = { }` | ||
Optional. Configure, how specific exceptions are handled.<br> | ||
Currently, the following exceptions can be configured: | ||
```js | ||
exceptions: { | ||
// If a tag contains text content, and the tag or it's content is not whitelisted. | ||
// (Consider using an ignore rule instead) | ||
illegalContent: 'throw' | ||
// If a tag has a `t` attribute, and the tag or a localized attribute is not whitelisted. | ||
// (Consider whitelisting the attribute instead) | ||
illegalAttribute: 'throw' | ||
} | ||
``` | ||
The following behaviour types are available: `'throw'`, `'warn'` and `'ignore'`. | ||
### `options.LocalizationKey` | ||
@@ -116,0 +132,0 @@ Optional. Specify the class that represents a localization key.<br> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45136
605
284
7
+ Addedansi-colors@^3.2.4
+ Addedfancy-log@^1.3.3
+ Addedansi-colors@3.2.4(transitive)