ember-template-lint
Version:
Linter for Ember or Handlebars templates.
86 lines (75 loc) • 1.68 kB
JavaScript
const { builders: b } = require('ember-template-recast');
const Rule = require('./base');
const BUILT_INS = new Set([
'action',
'array',
'component',
'concat',
'debugger',
'each',
'each-in',
'fn',
'get',
'hasBlock',
'has-block',
'has-block-params',
'hash',
'if',
'input',
'let',
'link-to',
'loc',
'log',
'mount',
'mut',
'on',
'outlet',
'partial',
'query-params',
'textarea',
'unbound',
'unless',
'with',
'-in-element',
'in-element',
]);
module.exports = class NoPositionalDataTestSelectors extends Rule {
visitor() {
return {
MustacheStatement(node) {
this.process(node);
},
BlockStatement(node) {
this.process(node);
},
};
}
process(node) {
if (BUILT_INS.has(node.path.original)) {
return;
}
let testSelectorParamIndex = node.params.findIndex(
(n) => n.type === 'PathExpression' && n.original.startsWith('data-test-')
);
if (testSelectorParamIndex === -1) {
return;
}
if (this.mode === 'fix') {
let selectorName = node.params[testSelectorParamIndex].original;
// remove the item from `params`
node.params.splice(testSelectorParamIndex, 1);
// add it as a HashPair
node.hash.pairs.unshift(b.pair(selectorName, b.boolean(true)));
} else {
this.log({
message:
'Passing a `data-test-*` positional param to a curly invocation should be avoided.',
line: node.loc && node.loc.start.line,
column: node.loc && node.loc.start.column,
source: this.sourceForNode(node),
isFixable: true,
});
}
}
};
;