Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from OakNorthAI/improve
Browse files Browse the repository at this point in the history
Allow: [test, bob] as some_type[] and Support ignoring tag/attribute combinations
  • Loading branch information
Matthias Görgens authored Oct 11, 2019
2 parents 9bb80b5 + 7c4ac5d commit 144e8ea
Showing 1 changed file with 55 additions and 16 deletions.
71 changes: 55 additions & 16 deletions lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,30 @@ module.exports = {
type: 'string'
}
},
ignoreAttributes: {
ignoreTags: {
type: 'array',
items: {
type: 'string'
type: 'object',
properties: {
tag: {
anyOf: [{
type: 'string', // TODO: think about allowing array of strings alternatively.
}, {
type: 'array',
items: {
type: 'string'
}
}]
},
attributes: {
type: 'array',
items: {
type: 'string'
}
}
}
}
},
}
},
additionalProperties: false
}]
Expand All @@ -61,12 +79,10 @@ module.exports = {
const whitelists = ((option && option.ignore) || []).map(
item => new RegExp(item)
);
const propWhitelist = ((option && option.ignoreProperties) || []);
const attrWhitelist = [
'className', 'style', 'styleName', 'src', 'type', 'id',
// SVG related attributes.
'viewBox', 'cx', 'cy', 'r', 'rx', 'ry', 'd', 'x1', 'y1', 'x2', 'y2', 'fill', 'stroke', 'strokeWidth', 'points'
].concat((option && option.ignoreAttributes) || []);
const propWhitelist = (option && option.ignoreProperties) || [];
const tagAttrWhiteList = ((option && option.ignoreTags) || []).map(entry =>
({ tags: typeof entry.tag === 'string' ? [entry.tag] : entry.tag,
attributes: entry.attributes }));

const calleeWhitelists = generateCalleeWhitelists(option);
const translatorCalleeWhitelists = generateTranslatorWhitelists(option);
Expand Down Expand Up @@ -123,10 +139,6 @@ module.exports = {
return calleeWhitelists.simple.indexOf(calleeName) !== -1;
}

function isValidAttrName(name) {
return attrWhitelist.includes(name);
}

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
Expand Down Expand Up @@ -192,6 +204,7 @@ module.exports = {

'JSXElement > JSXOpeningElement'(node) {
const name = context.getSourceCode().getText(node.name);
// TODO: read from an optional list.
if (name === "Trans" || name === "Translation") {
translationNodes.add(node.parent);
}
Expand All @@ -202,8 +215,13 @@ module.exports = {

'JSXAttribute'(node) {
// allow <div className="active" />
if (isValidAttrName(node.name.name)) {
technicalNodes.add(node);
const element = getNearestAncestor(node, 'JSXOpeningElement');
if (element !== null) {
if (tagAttrWhiteList.some(entry =>
(entry.tags.includes('*') || entry.tags.includes(element.name.name)) && entry.attributes.includes(node.name.name)
)) {
technicalNodes.add(node);
}
}
},

Expand Down Expand Up @@ -239,12 +257,16 @@ module.exports = {
if (isUpperCase(node.parent.id.name)) visited.add(node);
},

// 'TSTypeAssertion'(node) {
'TSAsExpression > :matches(Literal, TemplateLiteral)'(node) {
// Allow: "test" as string
// Allow: "test" as some_type
visited.add(node);
},
'TSAsExpression > ArrayExpression > :matches(Literal, TemplateLiteral)'(node) {
// Allow: ["test", "bob"] as some_type[]
visited.add(node);
},

'Property :matches(Literal, TemplateLiteral)'(node) {
if (!isString(node)) return;
const parent = getNearestAncestor(node, 'Property');
Expand Down Expand Up @@ -360,6 +382,23 @@ module.exports = {
// });

// • • • • •

{
const element = getNearestAncestor(node, 'JSXOpeningElement');
const attribute = getNearestAncestor(node, 'JSXAttribute');
if (element !== null) {
complaints.set(node, {
node,
message: "Forbidden literal string {{string}} as value for attribute '{{attribute}}' for tag '{{tag}}' in {{ code }} ",
data: {
string: context.getSourceCode().getText(node),
attribute: attribute.name.name,
tag: element.name.name,
code: context.getSourceCode().getText(element),
},
});
}
}
context.report(
complaints.has(node) ?
complaints.get(node) :
Expand Down

0 comments on commit 144e8ea

Please sign in to comment.