Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

csvn
Copy link
Contributor

@csvn csvn commented Nov 20, 2023

Angular v17 added a new Component.styleUrl property which accepts a string, and added the ability to use a string with the Component.styles property. This PR adds a linting rule to enforce & fix styles: ['...']/styleUrls: ['...'] to styles: '...'/styleUrl: '...'.

@csvn csvn force-pushed the feat/no-single-styles-array branch from a45f2aa to 17ff57a Compare November 20, 2023 16:56
Copy link
Member

@JamesHenry JamesHenry left a 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)

@csvn
Copy link
Contributor Author

csvn commented Nov 20, 2023

Thanks for taking a look James, and thanks for the feedback!
I think that's a good idea. In my mind it was very rare there ever was more than one string/URL, but I can still see someone wanting it consistently be an array. I'll update the PR shortly.

@JamesHenry JamesHenry marked this pull request as draft November 30, 2023 08:27
@JamesHenry
Copy link
Member

@csvn Any update on this one please?

@csvn
Copy link
Contributor Author

csvn commented Jan 9, 2024

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?
Copy link
Contributor

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`]
}

Copy link
Contributor

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}'`)];
Copy link
Contributor

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.

  1. It changes the quote type, which means it may end up breaking code. For example, "body { content: 'test'; }" would become 'body { content: 'test'; }'.
  2. 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)];

@csvn
Copy link
Contributor Author

csvn commented Feb 7, 2024

I think it makes sense to close this PR, since #1710 seems to fix the remaining work I had on this PR.

@csvn csvn closed this Feb 7, 2024
@csvn csvn deleted the feat/no-single-styles-array branch March 19, 2024 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants