-
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 report-interpolation-error support and bump
@wxml/parser
(#…
…44) * feat: support report-interpolation-error * chore: code format * chore: change example and fix typo * test: add more unit test
- Loading branch information
Showing
11 changed files
with
302 additions
and
15 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
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,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, | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -34,6 +34,6 @@ | |
"prettier": "^2.5.1" | ||
}, | ||
"dependencies": { | ||
"@wxml/parser": "^0.3.2" | ||
"@wxml/parser": "^0.4.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,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", | ||
}, | ||
}, | ||
], | ||
} | ||
], | ||
}); |
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
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,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) |
Oops, something went wrong.
6c92f62
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: