-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(eslint): check for improper sanitize function usage
- Loading branch information
1 parent
c1d40a0
commit 1947db0
Showing
6 changed files
with
101 additions
and
3 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
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,67 @@ | ||
/** | ||
* Rule to enforce using `sanitize` function as a template literal and not as a regular function. | ||
* Check the `docs/translations.example.js` file for more details. | ||
* | ||
* Note: this rule only applies in translation files. | ||
* | ||
* Limitations: the automatic fix script only affects `sanitize()` call with a single parameter, | ||
* which must be a template literal (`TemplateLiteral` AST type). | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const { | ||
getClosestParentFromType, | ||
isTranslationFile, | ||
} = require('./i18n-shared.js'); | ||
|
||
function report (context, key, callExpressionNode) { | ||
context.report({ | ||
node: callExpressionNode, | ||
messageId: 'sanitizeAlwaysTemplateLiteral', | ||
data: { key }, | ||
fix: (fixer) => { | ||
if (callExpressionNode.arguments?.length !== 1) { | ||
return; | ||
} | ||
|
||
const argument = callExpressionNode.arguments[0]; | ||
if (argument.type === 'TemplateLiteral') { | ||
const contents = context.getSourceCode().text.substring(argument.start, argument.end); | ||
return fixer.replaceText(callExpressionNode, `sanitize${contents}`); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'enforce template literal when using the sanitize function', | ||
category: 'Translation files', | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
sanitizeAlwaysTemplateLiteral: 'Missing template literal usage with sanitize function: {{key}}', | ||
}, | ||
}, | ||
create: function (context) { | ||
|
||
// Early return for non translation files | ||
if (!isTranslationFile(context)) { | ||
return {}; | ||
} | ||
|
||
return { | ||
CallExpression (node) { | ||
if (node.callee.name === 'sanitize') { | ||
const parentProperty = getClosestParentFromType(node, 'Property'); | ||
if (parentProperty != null) { | ||
report(context, parentProperty.key.value, node); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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