UNPKG

eslint-plugin-playlyfe

Version:
82 lines (68 loc) 1.74 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _isLocaleFile = _interopRequireDefault(require("./utils/isLocaleFile")); const Rule = { meta: { docs: { description: 'find keys in i18n files which are not in code' } }, create(context) { if (!(0, _isLocaleFile.default)(context.getFilename())) { return {}; } const { reactIntlFilePath } = context.options[0] || {}; let reactIntlJson = null; const fileStartLoc = { line: 1, column: 0 }; if (!reactIntlFilePath) { // report if reactIntl missing context.report({ loc: fileStartLoc, message: 'Missing reactIntlFilePath option.' }); return {}; } try { reactIntlJson = require(reactIntlFilePath); // eslint-disable-line global-require // maybe file path wrong } catch (error) { context.report({ loc: fileStartLoc, message: error.message }); return {}; } function getKeyText(keyNode) { if (keyNode.type === 'Literal') { // key of form { "a": 5, 'b': 5 } return keyNode.value; } if (keyNode.type === 'Identifier') { // key of form key eg { a: 5 } return keyNode.name; } context.report({ node: keyNode, message: 'Unsupported keyType' }); return null; } return { Property(node) { const id = getKeyText(node.key); if (id && !reactIntlJson[id]) { context.report({ node: node.key, message: `id '${id}' not present in reactIntlJson` }); } } }; } }; module.exports = Rule;