-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ESlint plugin for checking translation issues
- Loading branch information
Showing
10 changed files
with
242 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# The ESLint Plugin | ||
|
||
This directory contains an ESLint plugin which checks that only string literals | ||
are passed to the translation functions. | ||
|
||
It is bundled here because it is closely tied to the Agama project and probably | ||
does not make sense for other projects. | ||
|
||
## Disabling the Check | ||
|
||
In some rare cases using a variable instead of a string literal is correct. In | ||
that case disable the check locally: | ||
|
||
```js | ||
const SIZES = [ N_("small"), N_("medium"), N_("large") ]; | ||
|
||
// returns one of the sizes above | ||
const sz = getSize(); | ||
|
||
// eslint-disable-next-line agama-i18n/string-literals | ||
return <span>{_(sz)}</span>; | ||
``` | ||
|
||
## Links | ||
|
||
- https://eslint.org/docs/latest/extend/custom-rule-tutorial - tutorial for | ||
writing an ESLint plugin | ||
- https://eslint.org/docs/latest/extend/custom-rules - documentation for | ||
writing an ESLint plugin | ||
- https://astexplorer.net - online tool for browsing a parsed AST tree, | ||
useful for inspecting the properties of parsed source code |
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,29 @@ | ||
/* | ||
* Copyright (c) [2023] SUSE LLC | ||
* | ||
* All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of version 2 of the GNU General Public License as published | ||
* by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, contact SUSE LLC. | ||
* | ||
* To contact SUSE LLC about this file by physical or electronic mail, you may | ||
* find current contact information at www.suse.com. | ||
*/ | ||
|
||
const stringLiteralsRule = require("./string-literals"); | ||
|
||
module.exports = { | ||
rules: { | ||
// name of the rule | ||
"string-literals": stringLiteralsRule | ||
} | ||
}; |
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,5 @@ | ||
{ | ||
"name": "eslint-plugin-agama-i18n", | ||
"version": "0.0.1", | ||
"main": "eslint-plugin-agama-i18n.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,84 @@ | ||
/* | ||
* Copyright (c) [2023] SUSE LLC | ||
* | ||
* All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of version 2 of the GNU General Public License as published | ||
* by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, contact SUSE LLC. | ||
* | ||
* To contact SUSE LLC about this file by physical or electronic mail, you may | ||
* find current contact information at www.suse.com. | ||
*/ | ||
|
||
// names of all translation functions | ||
const translations = ["_", "n_", "N_", "Nn_"]; | ||
// names of the plural translation functions | ||
const plurals = ["n_", "Nn_"]; | ||
|
||
const errorMsgLiteral = "Use a string literal argument in the translation functions"; | ||
const errorMsgMissing = "Missing argument"; | ||
|
||
/** | ||
* Check whether the AST tree node is a string literal | ||
* @param {Object} node the node | ||
* @returns {boolean} true if the node is a string literal | ||
*/ | ||
function isStringLiteral(node) { | ||
if (!node) return false; | ||
|
||
return node.type === "Literal" && (typeof node.value === "string"); | ||
} | ||
|
||
/** | ||
* Check whether the ATS node is a string literal | ||
* @param {Object} node the node to check | ||
* @param {Object} parentNode parent node for reporting error if `node` is undefined | ||
* @param {Object} context the context for reporting an error | ||
*/ | ||
function checkNode(node, parentNode, context) { | ||
if (node) { | ||
// string literal? | ||
if (!isStringLiteral(node)) { | ||
context.report(node, errorMsgLiteral); | ||
} | ||
} else { | ||
// missing argument | ||
context.report(parentNode, errorMsgMissing); | ||
} | ||
} | ||
|
||
// define the eslint rule | ||
module.exports = { | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "Check that the arguments if the translation functions are string literals.", | ||
}, | ||
}, | ||
create: function (context) { | ||
return { | ||
// callback for handling function calls | ||
CallExpression(node) { | ||
// not a translation function, skip it | ||
if (!translations.includes(node.callee.name)) return; | ||
|
||
// check the first argument | ||
checkNode(node.arguments[0], node, context); | ||
|
||
// check also the second argument for the plural forms | ||
if (plurals.includes(node.callee.name)) { | ||
checkNode(node.arguments[1], node, context); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
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,62 @@ | ||
/* | ||
* Copyright (c) [2023] SUSE LLC | ||
* | ||
* All Rights Reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of version 2 of the GNU General Public License as published | ||
* by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, contact SUSE LLC. | ||
* | ||
* To contact SUSE LLC about this file by physical or electronic mail, you may | ||
* find current contact information at www.suse.com. | ||
*/ | ||
|
||
const { RuleTester } = require("eslint"); | ||
const stringLiteralsRule = require("./string-literals"); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { ecmaVersion: 2015 } | ||
}); | ||
|
||
ruleTester.run( | ||
"string-literals", | ||
stringLiteralsRule, | ||
{ | ||
// valid code examples, these should pass | ||
valid: [ | ||
{ code: "_(\"foo\")" }, | ||
{ code: "_('foo')" }, | ||
{ code: "n_(\"one\", \"many\", count)" }, | ||
{ code: "n_('one', 'many', count)" }, | ||
], | ||
// invalid examples, these should fail | ||
invalid: [ | ||
// string literal errors | ||
{ code: "_(null)", errors: 1 }, | ||
{ code: "_(undefined)", errors: 1 }, | ||
{ code: "_(42)", errors: 1 }, | ||
{ code: "_(foo)", errors: 1 }, | ||
{ code: "_(foo())", errors: 1 }, | ||
{ code: "_(`foo`)", errors: 1 }, | ||
{ code: "_(\"foo\" + \"bar\")", errors: 1 }, | ||
{ code: "_('foo' + 'bar')", errors: 1 }, | ||
// missing argument errors | ||
{ code: "_()", errors: 1 }, | ||
{ code: "n_('foo')", errors: 1 }, | ||
{ code: "n_(\"foo\")", errors: 1 }, | ||
// string literal + missing argument errors | ||
{ code: "n_(foo)", errors: 2 }, | ||
// string literal error twice | ||
{ code: "n_(foo, bar)", errors: 2 }, | ||
{ code: "Nn_(foo, bar)", errors: 2 }, | ||
], | ||
} | ||
); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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