Skip to content

Commit

Permalink
feat(htmlhint): add RegExp and regex string (#346)
Browse files Browse the repository at this point in the history
RegExp and regex string can be used in whitelist now!
related to #228 #183 microsoft/vscode-htmlhint#34
  • Loading branch information
NewFuture authored and thedaviddias committed Feb 1, 2020
1 parent d7283ad commit e617a15
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/rules/attr-lowercase.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
/**
* testAgainstStringOrRegExp
* @param {string} value string to test
* @param {string|RegExp} comparison raw string or regex string
* @returns {boolean}
*/
function testAgainstStringOrRegExp(value, comparison) {
// If it's a RegExp, test directly
if (comparison instanceof RegExp) {
return comparison.test(value)
? { match: value, pattern: comparison }
: false;
}

// Check if it's RegExp in a string
const firstComparisonChar = comparison[0];
const lastComparisonChar = comparison[comparison.length - 1];
const secondToLastComparisonChar = comparison[comparison.length - 2];

const comparisonIsRegex =
firstComparisonChar === '/' &&
(lastComparisonChar === '/' ||
(secondToLastComparisonChar === '/' && lastComparisonChar === 'i'));

const hasCaseInsensitiveFlag =
comparisonIsRegex && lastComparisonChar === 'i';

// If so, create a new RegExp from it
if (comparisonIsRegex) {
const valueMatches = hasCaseInsensitiveFlag
? new RegExp(comparison.slice(1, -2), 'i').test(value)
: new RegExp(comparison.slice(1, -1)).test(value);

return valueMatches;
}

// Otherwise, it's a string. Do a strict comparison
return value === comparison;
}

export default {
id: 'attr-lowercase',
description: 'All attribute names must be in lowercase.',
Expand All @@ -12,7 +52,7 @@ export default {
attr = attrs[i];
var attrName = attr.name;
if (
exceptions.indexOf(attrName) === -1 &&
!exceptions.find(exp => testAgainstStringOrRegExp(attrName, exp)) &&
attrName !== attrName.toLowerCase()
) {
reporter.error(
Expand Down
14 changes: 14 additions & 0 deletions test/rules/attr-lowercase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,18 @@ describe(`Rules: ${ruldId}`, function() {
const messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Set to array list with RegExp should not result in an error', function() {
const code = '<p testBox="abc" bind:tapTop="ccc">';
ruleOptions[ruldId] = ['testBox', /bind:.*/];
const messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});

it('Set to array list with regex string should not result in an error', function() {
const code = '<p testBox="abc" [ngFor]="ccc">';
ruleOptions[ruldId] = ['testBox', '/\\[.*\\]/'];
const messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});
});

0 comments on commit e617a15

Please sign in to comment.