-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new rule no-inconsistent-tagname (#51)
* feat: add new rule no-inconsistent-tagname * fix: docs error * fix: ci fail * fix: typo
- Loading branch information
Showing
5 changed files
with
142 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,39 @@ | ||
module.exports = { | ||
/** @type {import('eslint').Rule.RuleMetaData} */ | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "wxml tag's startTag name and endTag name must be equal", | ||
categories: [], | ||
url: "https://eslint-plugin-wxml.js.org/rules/no-inconsistent-tagname.html", | ||
}, | ||
fixable: null, | ||
messages: { | ||
tagInconsistent: | ||
"startTag's name '{{startTagName}}' and endTag's name '{{endTagName}}' not equal", | ||
}, | ||
schema: [], | ||
}, | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
create(context) { | ||
return { | ||
WXElement(node) { | ||
if ( | ||
node.startTag && | ||
node.endTag && | ||
node.startTag.name !== node.endTag.name | ||
) { | ||
context.report({ | ||
node, | ||
messageId: "tagInconsistent", | ||
data: { | ||
startTagName: node.startTag.name, | ||
endTagName: node.endTag.name, | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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,44 @@ | ||
const RuleTester = require("eslint").RuleTester; | ||
const rule = require("../../lib/rules/no-inconsistent-tagname"); | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve("@wxml/parser"), | ||
}); | ||
|
||
tester.run("no-inconsistent-tagname", rule, { | ||
valid: [ | ||
{ | ||
filename: "test.wxml", | ||
code: `<view wx:for="{{titles}}" wx:key="title" ></view>`, | ||
}, | ||
{ | ||
filename: "test.wxml", | ||
code: `<app />`, | ||
}, | ||
{ | ||
filename: "test.wxml", | ||
code: `<app></app>`, | ||
}, | ||
{ | ||
filename: "wxs.wxml", | ||
code: `<wxs module="mo" src="../../show.wxs" />`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
filename: "test.wxml", | ||
code: `<view wx:for="{{titles}}" wx:key="index" ></viw>`, | ||
errors: [`startTag's name 'view' and endTag's name 'viw' not equal`], | ||
}, | ||
{ | ||
filename: "test.wxml", | ||
code: `<view wx:for="{{titles}}" wx:key="index" ></>`, | ||
errors: [`startTag's name 'view' and endTag's name '' not equal`], | ||
}, | ||
{ | ||
filename: "test.wxml", | ||
code: `<view wx:for="{{titles}}" wx:key="index" ></ >`, | ||
errors: [`startTag's name 'view' and endTag's name '' not equal`], | ||
}, | ||
], | ||
}); |
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,57 @@ | ||
--- | ||
sidebarDepth: 0 | ||
title: wxml/no-inconsistent-tagname | ||
--- | ||
|
||
# wxml/no-inconsistent-tagname | ||
|
||
## Motivation | ||
|
||
Found startTag name and endTag name not equal at development stage. | ||
|
||
<eslint-code-block :rules="{'wxml/no-inconsistent-tagname': ['error']}" > | ||
|
||
```wxml | ||
<!-- ✓ GOOD --> | ||
<view | ||
wx:for="{{goodsList}}" | ||
wx:key="goodsId" | ||
> | ||
{{item.name}} | ||
</view> | ||
<same-tag-name> | ||
{{"tag name must be equal"}} | ||
</same-tag-name> | ||
<!-- ✗ BAD --> | ||
<view | ||
wx:for="{{goodsList}}" | ||
wx:key="id-{{goodsId}}" | ||
> | ||
{{item.name}} | ||
</viw> | ||
``` | ||
</eslint-code-block> | ||
|
||
::: tip 💡 tips | ||
|
||
You can edit code via online editor, it's online REPL, try to fix eslint problem ! | ||
|
||
::: | ||
|
||
## Config | ||
|
||
No special options, normal config is ok | ||
|
||
```json | ||
{ "wxml/no-inconsistent-tagname": "error" } | ||
``` | ||
|
||
## Version | ||
|
||
This rule was introduced in eslint-plugin-wxml `v0.7.2` | ||
|
||
## Implementation | ||
|
||
- [Rule Source Code](https://github.com/wxmlfile/eslint-plugin-wxml/tree/main/lib/rules/no-inconsistent-tagname.js) | ||
- [Test Source Code](https://github.com/wxmlfile/eslint-plugin-wxml/tree/main/tests/rules/no-inconsistent-tagname.js) |
8c8db68
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.
Successfully deployed to the following URLs:
eslint-plugin-wxml – ./
eslint-plugin-wxml-git-main-mobxjs.vercel.app
eslint-plugin-wxml.js.org
eslint-plugin-wxml-mobxjs.vercel.app
eslint-plugin-wxml.vercel.app