-
-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements proposed max-template-depth rule (#2525)
- Loading branch information
Showing
5 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,67 @@ | ||
--- | ||
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> | ||
|
||
<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 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 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: | ||
'Element is nested too deeply (depth of {{depth}}, maximum allowed is {{limit}}).' | ||
} | ||
}, | ||
/** @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 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,168 @@ | ||
/** | ||
* @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: | ||
'Element is nested too deeply (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: | ||
'Element is nested too deeply (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: | ||
'Element is nested too deeply (depth of 4, maximum allowed is 3).', | ||
line: 9, | ||
endLine: 12 | ||
}, | ||
{ | ||
message: | ||
'Element is nested too deeply (depth of 5, maximum allowed is 3).', | ||
line: 10, | ||
endLine: 10 | ||
}, | ||
{ | ||
message: | ||
'Element is nested too deeply (depth of 5, maximum allowed is 3).', | ||
line: 11, | ||
endLine: 12 | ||
} | ||
] | ||
} | ||
] | ||
}) |