Skip to content

Commit

Permalink
refactor to use if statement with early returns.
Browse files Browse the repository at this point in the history
  • Loading branch information
TaLeaMonet committed Sep 9, 2020
1 parent 411dd9a commit b5b8364
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,30 @@ module.exports = {

create(context) {
const defaults = {
noStrings: false, allowedStrings: [], ignoreProps: false, noAttributeStrings: false
noStrings: false,
allowedStrings: [],
ignoreProps: false,
noAttributeStrings: false
};
const config = Object.assign({}, defaults, context.options[0] || {});
config.allowedStrings = new Set(config.allowedStrings.map(trimIfString));

function defaultMessage() {
switch (true) {
case config.noAttributeStrings:
return 'Strings not allowed in attributes';
case config.noStrings:
return 'Strings not allowed in JSX files';
default:
return 'Missing JSX expression container around literal string';
if (config.noAttributeStrings) {
return 'Strings not allowed in attributes';
}
if (config.noStrings) {
return 'Strings not allowed in JSX files';
}
return 'Missing JSX expression container around literal string';
}

function reportLiteralNode(node, customMessage) {
const errorMessage = customMessage || defaultMessage();

context.report({
node,
message: `${errorMessage}: "${context.getSourceCode().getText(node).trim()}"`
message: `${errorMessage}: ${context.getSourceCode().getText(node).trim()}`
});
}

Expand Down

0 comments on commit b5b8364

Please sign in to comment.