Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/rules/template-no-template-lint-directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ module.exports = {
},
};

// ember-template-lint accepts quoted rule names in its directives and strips
// the quotes before matching, so `{{! template-lint-disable "no-log" }}` is a
// live directive. Mirror its `unquote` exactly: strip only when the first and
// last character are the same quote character.
function unquote(name) {
if (name.length < 3) {
return name;
}
const first = name[0];
if (first === name.at(-1) && (first === '"' || first === "'")) {
return name.slice(1, -1);
}
return name;
}

function convertComment(rawComment) {
const match = rawComment.match(DIRECTIVE_COMMENT);
if (!match) {
Expand All @@ -82,7 +97,7 @@ function convertComment(rawComment) {
.trim()
.split(/\s+/)
.filter(Boolean)
.map((r) => `ember/template-${r}`)
.map((r) => `ember/template-${unquote(r)}`)
.join(', ');
const body = rules ? `eslint-${action} ${rules}` : `eslint-${action}`;
// Emit symmetric markers regardless of what the source did.
Expand Down
31 changes: 31 additions & 0 deletions tests/lib/rules/template-no-template-lint-directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ ruleTester.run('template-no-template-lint-directives', rule, {
output: '<template>{{! eslint-enable ember/template-no-log }}{{log "x"}}</template>',
errors: [{ messageId: 'convert' }],
},
// ember-template-lint unquotes each rule name (single or double quotes),
// so quoted names are live directives in migrating codebases and must
// convert to bare eslint rule names.
{
code: '<template>{{! template-lint-disable "no-log" }}{{log "x"}}</template>',
output: '<template>{{! eslint-disable ember/template-no-log }}{{log "x"}}</template>',
errors: [{ messageId: 'convert' }],
},
{
code: '<template>{{! template-lint-disable \'no-log\' }}{{log "x"}}</template>',
output: '<template>{{! eslint-disable ember/template-no-log }}{{log "x"}}</template>',
errors: [{ messageId: 'convert' }],
},
{
code: '<template>{{! template-lint-disable "no-log" no-debugger }}{{log "x"}}</template>',
output:
'<template>{{! eslint-disable ember/template-no-log, ember/template-no-debugger }}{{log "x"}}</template>',
errors: [{ messageId: 'convert' }],
},
{
code: '<template>{{! template-lint-enable "no-log" }}{{log "x"}}</template>',
output: '<template>{{! eslint-enable ember/template-no-log }}{{log "x"}}</template>',
errors: [{ messageId: 'convert' }],
},
// Mismatched or lone quotes are not a quoted name — upstream's unquote
// only strips when the first and last character are the same quote.
{
code: '<template>{{! template-lint-disable "no-log\' }}{{log "x"}}</template>',
output: '<template>{{! eslint-disable ember/template-"no-log\' }}{{log "x"}}</template>',
errors: [{ messageId: 'convert' }],
},
// Directive inside an element's opening tag is lifted to before the
// element so the eslint-disable scope covers the line where the
// violation is reported.
Expand Down
Loading