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

fix(eslint-plugin): fix max inline declarations rule for styles string #1630

Merged
merged 1 commit into from
Nov 30, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,42 @@ class Test {}

#### ❌ Invalid Code

```ts
@Component({
styles: `
~
div {
display: block;
height: 40px;
}
`
~
})
class Test {}
```

<br>

---

<br>

#### Default Config

```json
{
"rules": {
"@angular-eslint/component-max-inline-declarations": [
"error"
]
}
}
```

<br>

#### ❌ Invalid Code

```ts
@Component({
styles: [
Expand Down Expand Up @@ -455,6 +491,35 @@ class Test {}

#### ✅ Valid Code

```ts
@Component({
styles: 'div { display: none; }'
})
class Test {}
```

<br>

---

<br>

#### Default Config

```json
{
"rules": {
"@angular-eslint/component-max-inline-declarations": [
"error"
]
}
}
```

<br>

#### ✅ Valid Code

```ts
@Component({
animations: [state('void', style({opacity: 0, transform: 'scale(1, 0)'}))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,15 @@ export default createESLintRule<Options, MessageIds>({
[`${Selectors.COMPONENT_CLASS_DECORATOR} Property[key.name='styles']`]({
value,
}: TSESTree.Property) {
if (!ASTUtils.isArrayExpression(value)) return;

const lineCount = value.elements.reduce(
(lines, element) => lines + (element ? getLinesCount(element) : 0),
0,
);
let lineCount: number;
if (!ASTUtils.isArrayExpression(value)) {
lineCount = getLinesCount(value);
} else {
lineCount = value.elements.reduce(
(lines, element) => lines + (element ? getLinesCount(element) : 0),
0,
);
}

if (lineCount <= styles) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export const valid = [
})
class Test {}
`,
// should succeed if the number of the styles lines does not exceeds the default lines limit with a string value
`
@Component({
styles: 'div { display: none; }'
})
class Test {}
`,
// should succeed if the number of the animations lines does not exceeds the default lines limit
`
@Component({
Expand Down Expand Up @@ -99,6 +106,25 @@ export const invalid = [
messageId,
data: { lineCount: 4, max: 3, propertyType: 'styles' },
}),
convertAnnotatedSourceToFailureCase({
description:
'should fail if the number of the styles lines exceeds the default lines limit with a string value',
annotatedSource: `
@Component({
styles: \`
~
div {
display: block;
height: 40px;
}
\`
~
})
class Test {}
`,
messageId,
data: { lineCount: 4, max: 3, propertyType: 'styles' },
}),
convertAnnotatedSourceToFailureCase({
description:
'should fail if the sum of lines (from separate styles) exceeds the default lines limit',
Expand Down
Loading