Skip to content

Commit

Permalink
feat: add onlyAttribute option
Browse files Browse the repository at this point in the history
fix #25
  • Loading branch information
edvardchen committed Jul 19, 2020
1 parent 6974bf7 commit 7ebf98b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
13 changes: 13 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,18 @@ function isAllowedDOMAttr(tag, attr) {
return false;
}

function generateFullMatchRegExp(source) {
if (source instanceof RegExp) {
return source;
}
if (typeof source !== 'string') {
console.error('generateFullMatchRegExp: expect string but get', source);
return new RegExp();
}
// allow dot ahead
return new RegExp(`(^|\\.)${source}${source.endsWith('$') ? '' : '$'}`);
}

exports.isUpperCase = isUpperCase;
exports.isAllowedDOMAttr = isAllowedDOMAttr;
exports.generateFullMatchRegExp = generateFullMatchRegExp;
37 changes: 26 additions & 11 deletions lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
*/
'use strict';

const { isUpperCase, isAllowedDOMAttr } = require('../helper');
const {
isUpperCase,
generateFullMatchRegExp,
isAllowedDOMAttr
} = require('../helper');
const { createOptimisticUniqueName } = require('typescript');
// const { TypeFlags, SyntaxKind } = require('typescript');

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -50,6 +55,12 @@ module.exports = {
},
markupOnly: {
type: 'boolean'
},
onlyAttribute: {
type: 'array',
items: {
type: 'string'
}
}
},
additionalProperties: false
Expand Down Expand Up @@ -100,13 +111,7 @@ module.exports = {
const validCalleeList = [
...popularCallee,
...((option && option.ignoreCallee) || [])
].map(item => {
if (item instanceof RegExp) {
return item;
}
// allow dot ahead
return new RegExp(`(^|\\.)${item}${item.endsWith('$') ? '' : '$'}`);
});
].map(generateFullMatchRegExp);

function isValidFunctionCall({ callee }) {
let calleeName = callee.name;
Expand Down Expand Up @@ -137,6 +142,10 @@ module.exports = {
...ignoredAttributes
];
function isValidAttrName(name) {
if (option && option.onlyAttribute) {
// only validate those attributes in onlyAttribute option
return !option.onlyAttribute.includes(name);
}
return userJSXAttrs.includes(name);
}

Expand Down Expand Up @@ -231,6 +240,9 @@ module.exports = {
context.report({ node, message });
}

// onlyAttribute would turn on markOnly
const markupOnly = option && (option.markupOnly || !!option.onlyAttribute);

const scriptVisitor = {
//
// ─── EXPORT AND IMPORT ───────────────────────────────────────────
Expand Down Expand Up @@ -266,13 +278,16 @@ module.exports = {
},

'JSXExpressionContainer > Literal:exit'(node) {
if (option && option.markupOnly) {
if (markupOnly) {
validateLiteral(node);
}
},

'JSXAttribute > Literal:exit'(node) {
if (option && option.markupOnly) {
if (markupOnly) {
const {
name: { name: attrName }
} = getNearestAncestor(node, 'JSXAttribute');
validateLiteral(node);
}
},
Expand Down Expand Up @@ -374,7 +389,7 @@ module.exports = {
},

'Literal:exit'(node) {
if (option && option.markupOnly) {
if (markupOnly) {
return;
}
validateLiteral(node);
Expand Down
5 changes: 4 additions & 1 deletion tests/lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ ruleTester.run('no-literal-string', rule, {
{
code: '<DIV foo="bar" />',
options: [{ markupOnly: true, ignoreAttribute: ['foo'] }]
}
},
// when onlyAttribute was configured, the markOnly would be treated as true
{ code: 'const a = "foo";', options: [{ onlyAttribute: ['bar'] }] },
{ code: '<DIV foo="bar" />', options: [{ onlyAttribute: ['bar'] }] }
],

invalid: [
Expand Down

0 comments on commit 7ebf98b

Please sign in to comment.