-
-
Notifications
You must be signed in to change notification settings - Fork 681
Implements proposed max-template-depth rule #2525
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
Merged
ota-meshi
merged 3 commits into
vuejs:master
from
kevsommer:implements-proposed-max-template-depth-rule
Sep 2, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/max-template-depth | ||
description: enforce maximum depth of template | ||
--- | ||
|
||
# vue/max-template-depth | ||
|
||
> enforce maximum depth of template | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> _**This rule has not been released yet.**_ </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforces a maximum depth of the template in a Vue SFC, in order to aid in maintainability and reduce complexity. | ||
|
||
## :wrench: Options | ||
|
||
This rule takes an object, where you can specify the maximum depth allowed in a Vue SFC template block. | ||
There is one property that can be specified for the object. | ||
|
||
- `maxDepth` ... Specify the maximum template depth `template` block. | ||
|
||
## `{ maxDepth: 3 }` | ||
|
||
<eslint-code-block :rules="{'vue/max-template-depth': ['error', { maxDepth: 3 }]}"> | ||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<template> | ||
<main-container> | ||
<child-component> | ||
<div /> | ||
</child-component> | ||
<child-component> | ||
<nested-component> | ||
<div> | ||
<div /> | ||
</div> | ||
</nested-component> | ||
</child-component> | ||
</main-container> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
### `{ maxDepth: 5 }` | ||
kevsommer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<eslint-code-block :rules="{'vue/max-template-depth': ['error', { maxDepth: 3 }]}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<template> | ||
<main-container> | ||
<child-component> | ||
<div /> | ||
</child-component> | ||
</main-container> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/max-template-depth.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/max-template-depth.js) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* @author kevsommer Kevin Sommer | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'enforce maximum depth of template', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/max-template-depth.html' | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
maxDepth: { | ||
type: 'integer', | ||
minimum: 1 | ||
} | ||
}, | ||
additionalProperties: false, | ||
minProperties: 1 | ||
} | ||
], | ||
messages: { | ||
templateTooDeep: | ||
'Template is too deep (depth of {{depth}}). Maximum allowed is {{limit}}.' | ||
kevsommer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const option = context.options[0] || {} | ||
|
||
/** | ||
* @param {VElement} element | ||
* @param {number} curDepth | ||
*/ | ||
function checkMaxDepth(element, curDepth) { | ||
if (curDepth > option.maxDepth) { | ||
context.report({ | ||
node: element, | ||
messageId: 'templateTooDeep', | ||
data: { | ||
depth: curDepth, | ||
limit: option.maxDepth | ||
} | ||
}) | ||
} | ||
|
||
if (!element.children) { | ||
return | ||
} | ||
|
||
for (const child of element.children) { | ||
if (child.type === 'VElement') { | ||
checkMaxDepth(child, curDepth + 1) | ||
} | ||
} | ||
} | ||
|
||
return { | ||
/** @param {Program} program */ | ||
Program(program) { | ||
const element = program.templateBody | ||
|
||
if (element == null) { | ||
return | ||
} | ||
|
||
if (element.type !== 'VElement') { | ||
return | ||
} | ||
|
||
checkMaxDepth(element, 0) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/** | ||
* @author kevsommer Kevin Sommer | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('../../eslint-compat').RuleTester | ||
const rule = require('../../../lib/rules/max-template-depth') | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
parser: require('vue-eslint-parser'), | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
tester.run('max-template-depth', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<main-container> | ||
<child-component> | ||
<div /> | ||
</child-component> | ||
</main-container> | ||
</template> | ||
`, | ||
options: [{ maxDepth: 3 }] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<main-container> | ||
<ul> | ||
<li>Item 1</li> | ||
</ul> | ||
</main-container> | ||
</template> | ||
`, | ||
options: [{ maxDepth: 4 }] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<main-container> | ||
<child-component> | ||
<sub-child-component> | ||
<div /> | ||
</sub-child-component> | ||
</child-component> | ||
</main-container> | ||
</template> | ||
`, | ||
options: [{ maxDepth: 4 }] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<div> | ||
</div> | ||
<main-container> | ||
<child-component> | ||
<ul> | ||
<li /> | ||
</ul> | ||
</child-component> | ||
</main-container> | ||
</template> | ||
`, | ||
options: [{ maxDepth: 4 }] | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<div> | ||
<div> | ||
<div> | ||
<div /> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
`, | ||
options: [{ maxDepth: 3 }], | ||
errors: [ | ||
{ | ||
message: 'Template is too deep (depth of 4). Maximum allowed is 3.', | ||
line: 6 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<side-container> | ||
<child-component /> | ||
</side-container> | ||
<main-container> | ||
<child-component> | ||
<nested-component> | ||
<h1 /> | ||
</nested-component> | ||
</child-component> | ||
</main-container> | ||
</template> | ||
`, | ||
options: [{ maxDepth: 3 }], | ||
errors: [ | ||
{ | ||
message: 'Template is too deep (depth of 4). Maximum allowed is 3.', | ||
line: 9 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<main-container> | ||
<nav-bar> | ||
<div /> | ||
</nav-bar> | ||
<child-component> | ||
<nested-component> | ||
<div> | ||
<div /> | ||
<div> | ||
</nested-component> | ||
</child-component> | ||
</main-container> | ||
</template> | ||
`, | ||
options: [{ maxDepth: 3 }], | ||
errors: [ | ||
{ | ||
message: 'Template is too deep (depth of 4). Maximum allowed is 3.', | ||
line: 9, | ||
endLine: 12 | ||
}, | ||
{ | ||
message: 'Template is too deep (depth of 5). Maximum allowed is 3.', | ||
line: 10, | ||
endLine: 10 | ||
}, | ||
{ | ||
message: 'Template is too deep (depth of 5). Maximum allowed is 3.', | ||
line: 11, | ||
endLine: 12 | ||
} | ||
] | ||
} | ||
] | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.