-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): add interpolation rule (#2)
- Loading branch information
1 parent
e3201a2
commit d088dc6
Showing
20 changed files
with
3,956 additions
and
179 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
node_modules/ | ||
yarn.lock | ||
|
||
lib/ |
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,4 @@ | ||
yarn.lock | ||
.babelrc | ||
.eslintrc | ||
tests/ |
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,10 @@ | ||
language: node_js | ||
node_js: | ||
- "7" | ||
|
||
cache: yarn | ||
|
||
script: | ||
- yarn run lint | ||
- yarn run test | ||
- yarn run build |
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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
'use strict'; | ||
|
||
var noUnknownKey = require('./rules/no-unknown-key'); | ||
var noTextAsChildren = require('./rules/no-text-as-children'); | ||
var interpolationData = require('./rules/interpolation-data'); | ||
|
||
module.exports = { | ||
rules: { | ||
'no-unknown-key': require('./rules/no-unknown-key')('principalLangs'), | ||
'no-unknown-key-secondary-langs': require('./rules/no-unknown-key')('secondaryLangs'), | ||
'no-text-as-children': require('./rules/no-text-as-children') | ||
'no-unknown-key': noUnknownKey('principalLangs'), | ||
'no-unknown-key-secondary-langs': noUnknownKey('secondaryLangs'), | ||
'no-text-as-children': noTextAsChildren, | ||
'interpolation-data': interpolationData | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,24 +1,33 @@ | ||
'use strict'; | ||
|
||
module.exports = function (context) { | ||
var config = context.settings.i18n; | ||
var minimatch = require('minimatch'); | ||
|
||
if (!config || config.ignoreFiles && new RegExp(config.ignoreFiles).test(context.getFilename())) { | ||
return {}; | ||
} | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'ensures that no plain text is used in JSX components', | ||
category: 'Possible errors' | ||
}, | ||
schema: [] | ||
}, | ||
create: function create(context) { | ||
var config = context.settings.i18n; | ||
|
||
return { | ||
JSXElement: function JSXElement(node) { | ||
node.children.forEach(function (child) { | ||
if (child.type === 'Literal') { | ||
var text = child.raw.trim().replace('\\n', ''); | ||
if (text.length) { | ||
context.report({ node: child, message: 'Untranslated text \'' + text + '\'' }); | ||
} | ||
} | ||
}); | ||
if (config && config.ignoreFiles && minimatch(context.getFilename(), config.ignoreFiles)) { | ||
return {}; | ||
} | ||
}; | ||
}; | ||
|
||
module.exports.schema = []; | ||
return { | ||
JSXElement: function JSXElement(node) { | ||
node.children.forEach(function (child) { | ||
if (child.type === 'Literal') { | ||
var text = child.raw.trim().replace('\\n', ''); | ||
if (text.length) { | ||
context.report({ node: child, message: 'Untranslated text \'' + text + '\'' }); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,26 +1,59 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _has = function _has(object, keys, index) { | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var appRootPath = require('app-root-path'); | ||
|
||
var recursiveGet = function recursiveGet(object, keys, index) { | ||
if (keys.length - index === 1) { | ||
return !!object[keys[index]]; | ||
return object[keys[index]]; | ||
} | ||
|
||
return object[keys[index]] ? _has(object[keys[index]], keys, index + 1) : false; | ||
return object[keys[index]] ? recursiveGet(object[keys[index]], keys, index + 1) : undefined; | ||
}; | ||
|
||
exports.has = function (object, key) { | ||
return !!recursiveGet(object, key.split('.'), 0); | ||
}; | ||
|
||
var has = exports.has = function has(object, key) { | ||
return _has(object, key.split('.'), 0); | ||
exports.get = function (object, key) { | ||
return recursiveGet(object, key.split('.'), 0); | ||
}; | ||
|
||
var getKeyValue = exports.getKeyValue = function getKeyValue(key) { | ||
exports.getKeyValue = function (key) { | ||
if (key.type === 'Literal') { | ||
return key.value; | ||
} else if (key.type === 'TemplateLiteral' && key.quasis.length === 1) { | ||
return key.quasis[0].value.cooked; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
var langConfig = void 0; | ||
var expireAt = 0; | ||
exports.getLangConfig = function (config, languagesKey) { | ||
if (expireAt <= Date.now() || config.disableCache) { | ||
langConfig = config[languagesKey].map(function (_ref) { | ||
var name = _ref.name, | ||
translationPath = _ref.translationPath; | ||
|
||
try { | ||
var langFile = JSON.parse(fs.readFileSync(path.resolve(appRootPath + '/' + translationPath)).toString()); | ||
|
||
return { | ||
name: name, | ||
translation: langFile | ||
}; | ||
} catch (e) { | ||
return { | ||
name: name, | ||
translation: null | ||
}; | ||
} | ||
}); | ||
expireAt = Date.now() + (config.translationsCacheTTL || 500); | ||
} | ||
|
||
return langConfig; | ||
}; |
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.