Skip to content

Commit

Permalink
feat: add report-interpolation-error support and bump @wxml/parser (#…
Browse files Browse the repository at this point in the history
…44)

* feat: support report-interpolation-error

* chore: code format

* chore: change example and fix typo

* test: add more unit test
  • Loading branch information
iChenLei authored Mar 24, 2022
1 parent ebecd33 commit 6c92f62
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 15 deletions.
27 changes: 20 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with: {node-version: '${{ env.NODE_VERSION }}'}
- run: npm install
- run: npm run lint
Expand All @@ -28,13 +28,26 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with: {node-version: '${{ env.NODE_VERSION }}'}
- run: npm install
- run: npm run lint
- run: npm run test

eslint8_test:
name: Test eslint-plugin-wxml with eslint8
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with: {node-version: '${{ env.NODE_VERSION }}'}
- run: npm install
- run: npm install eslint@8
- run: npm run lint
- run: npm run test

node_tests:
name: 'Test eslint-plugin-wxml on ${{matrix.os}} with node${{matrix.node}}'
strategy:
Expand All @@ -44,9 +57,9 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
# Pull repo to test machine
- uses: actions/checkout@v2
- uses: actions/checkout@v3
# Configures the node version used on GitHub-hosted runners
- uses: actions/setup-node@v2
- uses: actions/setup-node@v3
with:
# The Node.js version to configure
node-version: ${{ matrix.node }}
Expand All @@ -55,7 +68,7 @@ jobs:
id: npm-cache-dir
run: |
echo "::set-output name=dir::$(npm config get cache)"
- uses: actions/cache@v2
- uses: actions/cache@v3
id: npm-cache # use this to check for `cache-hit` ==> if: steps.npm-cache.outputs.cache-hit != 'true'
with:
path: ${{ steps.npm-cache-dir.outputs.dir }}
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

<!-- ## [0.7.0] - 2022-03-24 -->
### Changed
- Bump `@wxml/parser` version from `v0.4.0`
### Added
- Add ESLint 8 test suits
- Add rule `report-interpolation-error`

## [0.6.3] - 2022-02-09
### Added
- Add rule `required-root-tag`
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = {
"no-wx-for-with-wx-if": require("./rules/no-wx-for-with-wx-if"),
"no-wx-if-string": require("./rules/no-wx-if-string"),
quotes: require("./rules/quotes"),
"report-interpolation-error": require("./rules/report-interpolation-error"),
"report-wxs-syntax-error": require("./rules/report-wxs-syntax-error"),
"report-wxml-syntax-error": require("./rules/report-wxml-syntax-error"),
"required-attributes": require("./rules/required-attributes"),
Expand Down
61 changes: 61 additions & 0 deletions lib/rules/report-interpolation-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module.exports = {
/** @type {import('eslint').Rule.RuleMetaData} */
meta: {
type: "problem",
docs: {
description: "report interpolation error",
categories: [],
url: "https://eslint-plugin-wxml.js.org/rules/report-interpolation-error.html",
},
fixable: null,
messages: {
interpolationError: "{{error}}",
},
schema: [],
},

/** @param {import('eslint').Rule.RuleContext} context */
create(context) {
return {
WXInterpolation(node) {
let espreeParser;
if (!(node && node.value)) {
return;
}
try {
// eslint-disable-next-line node/no-extraneous-require
espreeParser = require("espree");
} catch (_) {
// ...
}
if (espreeParser) {
try {
espreeParser.parse(node.value, {
ecmaVersion: espreeParser.latestEcmaVersion,
});
} catch (error) {
try {
espreeParser.parse(`({${node.value}})`, {
ecmaVersion: espreeParser.latestEcmaVersion,
});
} catch (_) {
try {
espreeParser.parse(`(${node.value})`, {
ecmaVersion: espreeParser.latestEcmaVersion,
});
} catch (_) {
context.report({
node,
messageId: "interpolationError",
data: {
error: error.message,
},
});
}
}
}
}
},
};
},
};
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
"prettier": "^2.5.1"
},
"dependencies": {
"@wxml/parser": "^0.3.2"
"@wxml/parser": "^0.4.0"
}
}
147 changes: 147 additions & 0 deletions tests/rules/report-interpolation-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
const RuleTester = require("eslint").RuleTester;
const rule = require("../../lib/rules/report-interpolation-error");

const tester = new RuleTester({
parser: require.resolve("@wxml/parser"),
});

tester.run("report-interpolation-error", rule, {
valid: [
{
filename: "interpolation.wxml",
code: `<app> {{23}} </app>`,
},
{
filename: "interpolation.wxml",
code: `<app> {{ show ? 23 : ''}} </app>`,
},
{
filename: "",
code: `
<view wx:if="{{CartStore.useBannerV2}}"
bind:tap="clickBanner"
class="wrapper"
style="{{ banner.bgColor ? ('background-color: ' + banner.bgColor) : ''}}"
/>
`,
},
{
filename: "interpolation.wxml",
code: `<app> {{ }} </app>`,
},
{
filename: "interpolation.wxml",
code: `
<infinite-list
rootMargin="600"
bind:loadMore="loadMore"
loading="{{tabList[selectTabIndex].isLoading}}"
isUseGoTop="{{false}}"
hasMore="{{tabList[selectTabIndex].hasMore}}"
/>
`,
},
{
filename: "interpolation.wxml",
code: `<app> {{ a + b }} </app>`,
},
{
filename: "interpolation.wxml",
code: `<view class="progressBar" style="{{width ? ( 'width:' + width ) : ''}}">`,
},
{
filename: "interpolation.wxml",
code: `<view>{{"hello" + name}}</view>`,
},
{
filename: "interpolation.wxml",
code: `<view>{{object.key}} {{array[0]}}</view>`,
},
{
filename: "interpolation.wxml",
code: `<view wx:for="{{[zero, 1, 2, 3, 4]}}"> {{item}} </view>`,
},
{
filename: "interpolation.wxml",
code: `<template is="objectCombine" data="{{...obj1, ...obj2, e: 5}}"></template>`,
},
{
filename: "interpolation.wxml",
code: `
<template is="navMain"
data="{{ searchShadeWords: Store.searchShadeWords, hasSearch, gotoOrderList}}"
/>
`,
},
{
filename: "interpolation.wxml",
code: `<template is="objectCombine" data="{{foo, bar}}"></template>`,
},
{
filename: "interpolation.wxml",
code: `ext_params_click="{{ { goods_id: log.goodsId, exps: log.stringifyExps } }}"`,
},
],
invalid: [
{
filename: "interpolation.wxml",
code: `<app> {{ show ? 23 : }} </app>`,
errors: [
{
messageId: "interpolationError",
data: {
error: "Unexpected token",
},
},
],
},
{
filename: "interpolation.wxml",
code: `<app> {{ show 23 : }} </app>`,
errors: [
{
messageId: "interpolationError",
data: {
error: "Unexpected token 23",
},
},
],
},
{
filename: "interpolation.wxml",
code: `<app> {{ a + }} </app>`,
errors: [
{
messageId: "interpolationError",
data: {
error: "Unexpected token",
},
},
],
},
{
filename: "interpolation.wxml",
code: `<app> {{ ..a, a: }} </app>`,
errors: [
{
messageId: "interpolationError",
data: {
error: "Unexpected token .",
},
},
],
},
{
filename: "interpolation.wxml",
code: `<view style="idx-{{isOdd ? 'single' }}" />`,
errors: [
{
messageId: "interpolationError",
data: {
error: "Unexpected token",
},
},
],
}
],
});
11 changes: 8 additions & 3 deletions tests/rules/report-wxml-syntax-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ tester.run("report-wxml-syntax-error", rule, {
{
messageId: "wxmlError",
data: {
error:
"Expecting token of type --> EOF <-- but found --> 'var data = {}; module.exports = { data: data } ' <--",
error: "Expecting token of type --> EOF <-- but found --> '</' <--",
},
},
],
Expand All @@ -80,7 +79,13 @@ tester.run("report-wxml-syntax-error", rule, {
messageId: "wxmlError",
data: {
error:
"Expecting token of type --> EOF <-- but found --> '{{ a > ' }} {{}}' <--",
"unexpected character: ->'<- at offset: 30, skipped 1 characters.",
},
},
{
messageId: "wxmlError",
data: {
error: "Expecting token of type --> EOF <-- but found --> '</' <--",
},
},
],
Expand Down
1 change: 1 addition & 0 deletions website/docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sidebarDepth: 0
| [Deprecated] [wxml/no-wx-for-with-wx-if](./no-wx-for-with-wx-if.md) | ~~avoid [error](https://developers.weixin.qq.com/community/develop/doc/00082a556fcb0810a6b7e2eee5b800) when you use `wx:for` with `wx:if/wx:elif/wx:else` at same tag~~ | | |
| [wxml/no-wx-if-string](./no-wx-if-string.md) | avoid unexpected result when you use invalid boolean interpolation as `wx:if/wx:elif`'s value | :star: :star: :star: | |
| [wxml/quotes](./quotes.md) | force using same quotes style in project, `'` or `"` | :star: | |
| [wxml/report-interpolation-error](./report-interpolation-error.md) | check `interpolation` syntax error via [`@wxml/parser`](https://github.com/wxmlfile/wxml-parser) | :star: :star: :star: | |
| [wxml/report-wxml-syntax-error](./report-wxml-syntax-error.md) | check `wxml` syntax error via [`@wxml/parser`](https://github.com/wxmlfile/wxml-parser) | :star: | |
| [wxml/report-wxs-syntax-error](./report-wxml-syntax-error.md) | check inline `wxs` syntax error via [`@wxml/parser`](https://github.com/wxmlfile/wxml-parser) | :star: | |
| [wxml/required-attributes](./required-attributes.md) | using custom config to force team member write the required attributes in wxml | :star: :star: | |
Expand Down
52 changes: 52 additions & 0 deletions website/docs/rules/report-interpolation-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
sidebarDepth: 0
title: wxml/report-interpolation-error
---

# wxml/report-interpolation-error

### Backgroud

::: tip {{}} interpolation

> Mustache style [interpolation]([https://developers.weixin.qq.com/miniprogram/dev/reference/wxml/data.html]) in wxml
`eslint-plugin-wxml` using `@wxml/parser` as parser to parse interpolation content, it provide `interpolation` syntax error message. If you think this rules contain `bug`, please report it by [github issue](https://github.com/wxmlfile/eslint-plugin-wxml/issues).

:::

## Motivation

hint interpolation syntax error in development time, save developer's time.

<eslint-code-block :rules="{'wxml/report-interpolation-error': ['error']}" >
```wxml
<view>
<view style="idx-{{isOdd ? 'single'}}" />
{{ a + }}
</view>
```
</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/report-interpolation-error": "error" }
```

## Version

This rule was introduced in eslint-plugin-wxml `v0.7.0`

## Implementation

- [Rule Source Code](https://github.com/wxmlfile/eslint-plugin-wxml/tree/main/lib/rules/report-interpolation-error.js)
- [Test Source Code](https://github.com/wxmlfile/eslint-plugin-wxml/tree/main/tests/rules/report-interpolation-error.js)
Loading

1 comment on commit 6c92f62

@vercel
Copy link

@vercel vercel bot commented on 6c92f62 Mar 24, 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.