New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(eslint-plugin): add rule to disallow single array styles
#1629
Conversation
a45f2aa
to
17ff57a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @csvn!
I think it would be more flexible and useful to the whole community if we instead refactored this to be:
consistent-component-styles
The rule would take either string
or array
as an option, so that folks could enforce always using the array form instead if that's what they prefer. By all means we could make string
be the default value, but I think having the option to enforce consistent arrays would be good for certain codebases.
Example string config:
"@angular-eslint/consistent-component-styles": "error"
(because default)
OR
"@angular-eslint/consistent-component-styles": ["error", "string"]
Example array config:
"@angular-eslint/consistent-component-styles": ["error", "array"]
(Inspired by https://eslint.org/docs/latest/rules/consistent-this)
Thanks for taking a look James, and thanks for the feedback! |
@csvn Any update on this one please? |
Sorry James, I ended up a bit busy with work and sickness during the holidays. I'll try to update this MR this week. |
messageId: 'noSingleStylesArray', | ||
fix: (fixer) => { | ||
const [el] = node.elements; | ||
// TODO: Do we need the fix to apply for template strings? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the fix will need to apply for template strings. Using backticks for quotes is perfectly valid and, as far as I can tell, the string is not considered a "literal" (it has a type of TemplateLiteral
rather than Literal
):
@Component({
styleUrls: [`./widget.component.ts`]
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if getting the element's text from the source code is the preferred way, but this works for me:
fix: (fixer) => {
const [el] = node.elements;
if (el) {
if (ASTUtils.isStringLiteral(el)) {
return [fixer.replaceText(node, el.raw)];
}
if (ASTUtils.isTemplateLiteral(el)) {
return [
fixer.replaceText(
node,
`${context.getSourceCode().getText(el)}`,
),
];
}
}
return [];
},
const [el] = node.elements; | ||
// TODO: Do we need the fix to apply for template strings? | ||
if (!el || !ASTUtils.isStringLiteral(el)) return []; | ||
return [fixer.replaceText(node, `'${el.value}'`)]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two problems that I've found with this replacement.
- It changes the quote type, which means it may end up breaking code. For example,
"body { content: 'test'; }"
would become'body { content: 'test'; }'
. - Any escaped characters are replaced. For example,
'body { content: "\t"; }'
would become'body { content: " "; }'
Thankfully the fix is simple! Use raw
instead of value
and don't wrap it in quotes.
return [fixer.replaceText(node, el.raw)];
I think it makes sense to close this PR, since #1710 seems to fix the remaining work I had on this PR. |
Angular v17 added a new
Component.styleUrl
property which accepts astring
, and added the ability to use astring
with theComponent.styles
property. This PR adds a linting rule to enforce & fixstyles: ['...']
/styleUrls: ['...']
tostyles: '...'
/styleUrl: '...'
.