@coffeelint/cli
Version:
Lint your CoffeeScript
77 lines (67 loc) • 2.13 kB
JavaScript
(function() {
var NoNestedStringInterpolation;
module.exports = NoNestedStringInterpolation = (function() {
class NoNestedStringInterpolation {
constructor() {
this.blocks = [];
}
lintToken(token, tokenApi) {
var block, ref, tag, tagname, tagtype;
[tag] = token;
if (!this.blocks.length) {
this.blocks.push([]);
}
block = this.blocks[this.blocks.length - 1];
if (tag === 'JSX_TAG') {
this.blocks.push([]);
return;
}
[tagname, tagtype] = tag.split('_');
if (tagtype === 'END') {
block.pop();
if (tagname === 'STRING') {
block.strCount -= 1;
if (block.strCount <= 1) {
block.error = false;
}
} else {
this.blocks.pop();
}
if (!block.length) {
this.blocks.pop();
}
if (!this.blocks.length) {
this.blocks.push([]);
}
} else {
block.push(tagname);
if (tagname === 'STRING') {
block.strCount = ((ref = block.strCount) != null ? ref : 0) + 1;
// Don't make multiple errors for deeply nested interpolation
if (block.strCount > 1 && !block.error) {
block.error = true;
return {token};
}
}
}
}
};
NoNestedStringInterpolation.prototype.rule = {
type: 'problem',
name: 'no_nested_string_interpolation',
level: 'warn',
message: 'Nested string interpolation is forbidden',
description: `This rule warns about nested string interpolation,
as it tends to make code harder to read and understand.
<pre>
<code># Good!
str = "Book by #{firstName.toUpperCase()} #{lastName.toUpperCase()}"
# Bad!
str = "Book by #{"#{firstName} #{lastName}".toUpperCase()}"
</code>
</pre>`
};
NoNestedStringInterpolation.prototype.tokens = ['JSX_TAG', 'CALL_START', 'CALL_END', 'STRING_START', 'STRING_END'];
return NoNestedStringInterpolation;
}).call(this);
}).call(this);