Skip to content

Commit

Permalink
feat: add new rule no-inconsistent-tagname (#51)
Browse files Browse the repository at this point in the history
* feat: add new rule no-inconsistent-tagname

* fix: docs error

* fix: ci fail

* fix: typo
  • Loading branch information
iChenLei authored May 22, 2022
1 parent 553a893 commit 8c8db68
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
"no-duplicate-attributes": require("./rules/no-duplicate-attributes"),
"no-index-in-wx-key": require("./rules/no-index-in-wx-key"),
"no-dynamic-wx-key": require("./rules/no-dynamic-wx-key"),
"no-inconsistent-tagname": require("./rules/no-inconsistent-tagname"),
"no-inline-wxs": require("./rules/no-inline-wxs"),
"no-unexpected-string-bool": require("./rules/no-unexpected-string-bool"),
"no-unnecessary-block": require("./rules/no-unnecessary-block"),
Expand Down
39 changes: 39 additions & 0 deletions lib/rules/no-inconsistent-tagname.js
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,
},
});
}
},
};
},
};
44 changes: 44 additions & 0 deletions tests/rules/no-inconsistent-tagname.js
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`],
},
],
});
1 change: 1 addition & 0 deletions website/docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sidebarDepth: 0
| [wxml/no-dot-this-in-wx-key](./no-dot-this-in-wx-key.md) | disable using [`*this`](https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/list.html) as `wx:key`'s value | :star: | |
| [wxml/no-duplicate-attributes](./no-duplicate-attributes.md) | not allow exist duplicate attributes in a single tag | :star: :star: | |
| [wxml/no-dynamic-wx-key](./no-dynamic-wx-key.md) | enforce using static `wx:key` | :star: | |
| [wxml/no-inconsistent-tagname](./no-inconsistent-tagname.md) | Found startTag name and endTag name not equal at development stage | :star: :star: :star: | |
| [wxml/no-index-in-wx-key](./no-index-in-wx-key.md) | disable using [`index`](https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/list.html) as `wx:key`'s value | :star: | |
| [wxml/no-inline-wxs](./no-inline-wxs.md) | force using separate `.wxs` file | :star: | |
| [wxml/no-unexpected-string-bool](./no-unexpected-string-bool.md) | not allow using `"true"` or `"false"` as attribute's boolean value, [official documentation](https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/data.html) | :star: :star: :star: | |
Expand Down
57 changes: 57 additions & 0 deletions website/docs/rules/no-inconsistent-tagname.md
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)

1 comment on commit 8c8db68

@vercel
Copy link

@vercel vercel bot commented on 8c8db68 May 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.