Skip to content

Commit

Permalink
feat: improvements to attribute-validate-style
Browse files Browse the repository at this point in the history
  • Loading branch information
revelt committed Feb 1, 2021
1 parent ed082b8 commit 7a2e9e2
Show file tree
Hide file tree
Showing 9 changed files with 948 additions and 70 deletions.
2 changes: 1 addition & 1 deletion packages/emlint/coverage/coverage-summary.json
@@ -1 +1 @@
{"total":{"lines":{"total":2426,"covered":2369,"skipped":0,"pct":97.65},"statements":{"total":2706,"covered":2649,"skipped":0,"pct":97.89},"functions":{"total":990,"covered":984,"skipped":0,"pct":99.39},"branches":{"total":1983,"covered":1874,"skipped":0,"pct":94.5}}}
{"total":{"lines":{"total":2456,"covered":2399,"skipped":0,"pct":97.68},"statements":{"total":2737,"covered":2680,"skipped":0,"pct":97.92},"functions":{"total":990,"covered":984,"skipped":0,"pct":99.39},"branches":{"total":2031,"covered":1922,"skipped":0,"pct":94.63}}}
134 changes: 120 additions & 14 deletions packages/emlint/dist/emlint.cjs.js
Expand Up @@ -9116,30 +9116,134 @@ function attributeValidateStart(context) {
};
}

function validateInlineStyle(str, idxOffset // opts
) {
// console.log(
// `005 validateInlineStyle(): ${`\u001b[${33}m${`opts`}\u001b[${39}m`} = ${JSON.stringify(
// opts,
// null,
// 4
// )}`
// );
// we get trimmed string start and end positions, also an encountered errors array
var defaults$3 = {
noTrailingSemi: true
};

function validateInlineStyle(str, idxOffset, originalOpts) {

var opts = _objectSpread__default['default'](_objectSpread__default['default']({}, defaults$3), originalOpts); // we get trimmed string start and end positions, also an encountered errors array
// const { charStart, charEnd, errorArr } = checkForWhitespace(str, idxOffset);

var _checkForWhitespace = checkForWhitespace(str, idxOffset),
charStart = _checkForWhitespace.charStart,
charEnd = _checkForWhitespace.charEnd,
errorArr = _checkForWhitespace.errorArr; // now that we know where non-whitespace chars are, we can evaluate them
// if (Number.isInteger(charStart)) {
// TODO: SOMETHING MORE
// }

if (charStart !== null && charEnd) {
// 1. check inner whitespace:
// imagine original source:
// <td style="font-size: 10px;"></td>
// extracted value is passed as "str":
// font-size: 10px;
// ^^
// we flag this
//
var whitespaceStartsAt = null;
var nonSpacesMet = false;

for (var i = charStart; i < charEnd; i++) { // catch the unspaced colon
// <td style="font-size:10px;"></td>
// ^

if (str[i] === ":" && str[i + 1].trim()) {
errorArr.push({
idxFrom: i + 1 + idxOffset,
idxTo: i + 1 + idxOffset,
message: "Add a space.",
fix: {
ranges: [[i + 1 + idxOffset, i + 1 + idxOffset, " "]]
}
});
} // catch the start of a wrong whitespace chunk


if ( // it's whitespace:
!str[i].trim() && // it hasn't been recording
whitespaceStartsAt === null) {
whitespaceStartsAt = i;
} // flag up non-space whitespace characters


if ( // chunk has been recording
whitespaceStartsAt && // and it's a whitespace
!str[i].trim() && // and current char is not a space
str[i] !== " " && // and flag hasn't been flipped already
!nonSpacesMet) {
nonSpacesMet = true;
} // catch the excessive chunk or anything not-space


if ( // it exists
whitespaceStartsAt && // it's been passed
i > whitespaceStartsAt && ( // and current char doesn't exist (end reached)
!str[i] || // or it's not whitespace
str[i].trim())) {

if (nonSpacesMet || i > whitespaceStartsAt + 1) { // default is replacement of the whole string with a single space

var from = whitespaceStartsAt;
var to = i;
var replacement = " ";

if (str[whitespaceStartsAt] === " ") {
// push "from" by one and remove replacement
from += 1;
replacement = null;
} else if (str[i - 1] === " ") {
to -= 1;
replacement = null;
}

errorArr.push({
idxFrom: from + idxOffset,
idxTo: to + idxOffset,
message: (nonSpacesMet && i === whitespaceStartsAt + 1 ? "Replace" : "Remove") + " whitespace.",
fix: {
ranges: [replacement ? [from + idxOffset, to + idxOffset, replacement] : [from + idxOffset, to + idxOffset]]
}
});
} // reset


whitespaceStartsAt = null;
nonSpacesMet = false;
}
} // -----------------------------------------------------------------------------
// 2. check the trailing semi


if (opts.noTrailingSemi && str[charEnd - 1] === ";") {
errorArr.push({
idxFrom: charEnd - 1 + idxOffset,
idxTo: charEnd + idxOffset,
message: "Delete the trailing semicolon.",
fix: {
ranges: [[charEnd - 1 + idxOffset, charEnd + idxOffset]]
}
});
} else if (!opts.noTrailingSemi && str[charEnd - 1] !== ";") {
errorArr.push({
idxFrom: charEnd + idxOffset,
idxTo: charEnd + idxOffset,
message: "Add the trailing semicolon.",
fix: {
ranges: [[charEnd + idxOffset, charEnd + idxOffset, ";"]]
}
});
}
}

return errorArr;
}

// -----------------------------------------------------------------------------

function attributeValidateStyle(context) {
for (var _len = arguments.length, opts = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
opts[_key - 1] = arguments[_key];
}

return {
attribute: function attribute(node) {

Expand All @@ -9155,7 +9259,9 @@ function attributeValidateStyle(context) {
});
}

var errorArr = validateInlineStyle(node.attribValueRaw, node.attribValueStartsAt);
var errorArr = validateInlineStyle(node.attribValueRaw, node.attribValueStartsAt, {
noTrailingSemi: Array.isArray(opts) && opts.includes("noTrailingSemi")
});
errorArr.forEach(function (errorObj) {
context.report(_objectSpread__default['default'](_objectSpread__default['default']({}, errorObj), {}, {
ruleId: "attribute-validate-style"
Expand Down
134 changes: 120 additions & 14 deletions packages/emlint/dist/emlint.dev.umd.js
Expand Up @@ -36523,30 +36523,134 @@ function attributeValidateStart(context) {
};
}

function validateInlineStyle(str, idxOffset // opts
) {
// console.log(
// `005 validateInlineStyle(): ${`\u001b[${33}m${`opts`}\u001b[${39}m`} = ${JSON.stringify(
// opts,
// null,
// 4
// )}`
// );
// we get trimmed string start and end positions, also an encountered errors array
var defaults$a = {
noTrailingSemi: true
};

function validateInlineStyle(str, idxOffset, originalOpts) {

var opts = _objectSpread2(_objectSpread2({}, defaults$a), originalOpts); // we get trimmed string start and end positions, also an encountered errors array
// const { charStart, charEnd, errorArr } = checkForWhitespace(str, idxOffset);

var _checkForWhitespace = checkForWhitespace(str, idxOffset),
charStart = _checkForWhitespace.charStart,
charEnd = _checkForWhitespace.charEnd,
errorArr = _checkForWhitespace.errorArr; // now that we know where non-whitespace chars are, we can evaluate them
// if (Number.isInteger(charStart)) {
// TODO: SOMETHING MORE
// }

if (charStart !== null && charEnd) {
// 1. check inner whitespace:
// imagine original source:
// <td style="font-size: 10px;"></td>
// extracted value is passed as "str":
// font-size: 10px;
// ^^
// we flag this
//
var whitespaceStartsAt = null;
var nonSpacesMet = false;

for (var i = charStart; i < charEnd; i++) { // catch the unspaced colon
// <td style="font-size:10px;"></td>
// ^

if (str[i] === ":" && str[i + 1].trim()) {
errorArr.push({
idxFrom: i + 1 + idxOffset,
idxTo: i + 1 + idxOffset,
message: "Add a space.",
fix: {
ranges: [[i + 1 + idxOffset, i + 1 + idxOffset, " "]]
}
});
} // catch the start of a wrong whitespace chunk


if ( // it's whitespace:
!str[i].trim() && // it hasn't been recording
whitespaceStartsAt === null) {
whitespaceStartsAt = i;
} // flag up non-space whitespace characters


if ( // chunk has been recording
whitespaceStartsAt && // and it's a whitespace
!str[i].trim() && // and current char is not a space
str[i] !== " " && // and flag hasn't been flipped already
!nonSpacesMet) {
nonSpacesMet = true;
} // catch the excessive chunk or anything not-space


if ( // it exists
whitespaceStartsAt && // it's been passed
i > whitespaceStartsAt && ( // and current char doesn't exist (end reached)
!str[i] || // or it's not whitespace
str[i].trim())) {

if (nonSpacesMet || i > whitespaceStartsAt + 1) { // default is replacement of the whole string with a single space

var from = whitespaceStartsAt;
var to = i;
var replacement = " ";

if (str[whitespaceStartsAt] === " ") {
// push "from" by one and remove replacement
from += 1;
replacement = null;
} else if (str[i - 1] === " ") {
to -= 1;
replacement = null;
}

errorArr.push({
idxFrom: from + idxOffset,
idxTo: to + idxOffset,
message: (nonSpacesMet && i === whitespaceStartsAt + 1 ? "Replace" : "Remove") + " whitespace.",
fix: {
ranges: [replacement ? [from + idxOffset, to + idxOffset, replacement] : [from + idxOffset, to + idxOffset]]
}
});
} // reset


whitespaceStartsAt = null;
nonSpacesMet = false;
}
} // -----------------------------------------------------------------------------
// 2. check the trailing semi


if (opts.noTrailingSemi && str[charEnd - 1] === ";") {
errorArr.push({
idxFrom: charEnd - 1 + idxOffset,
idxTo: charEnd + idxOffset,
message: "Delete the trailing semicolon.",
fix: {
ranges: [[charEnd - 1 + idxOffset, charEnd + idxOffset]]
}
});
} else if (!opts.noTrailingSemi && str[charEnd - 1] !== ";") {
errorArr.push({
idxFrom: charEnd + idxOffset,
idxTo: charEnd + idxOffset,
message: "Add the trailing semicolon.",
fix: {
ranges: [[charEnd + idxOffset, charEnd + idxOffset, ";"]]
}
});
}
}

return errorArr;
}

// -----------------------------------------------------------------------------

function attributeValidateStyle(context) {
for (var _len = arguments.length, opts = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
opts[_key - 1] = arguments[_key];
}

return {
attribute: function attribute(node) {

Expand All @@ -36562,7 +36666,9 @@ function attributeValidateStyle(context) {
});
}

var errorArr = validateInlineStyle(node.attribValueRaw, node.attribValueStartsAt);
var errorArr = validateInlineStyle(node.attribValueRaw, node.attribValueStartsAt, {
noTrailingSemi: Array.isArray(opts) && opts.includes("noTrailingSemi")
});
errorArr.forEach(function (errorObj) {
context.report(_objectSpread2(_objectSpread2({}, errorObj), {}, {
ruleId: "attribute-validate-style"
Expand Down

0 comments on commit 7a2e9e2

Please sign in to comment.