diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 86ccc5a..86477a9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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: @@ -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 }} @@ -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 }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 14a8663..b1c25e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] + +### 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` diff --git a/lib/index.js b/lib/index.js index 9d875db..24500fc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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"), diff --git a/lib/rules/report-interpolation-error.js b/lib/rules/report-interpolation-error.js new file mode 100644 index 0000000..8cc507d --- /dev/null +++ b/lib/rules/report-interpolation-error.js @@ -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, + }, + }); + } + } + } + } + }, + }; + }, +}; diff --git a/package-lock.json b/package-lock.json index 848e9db..7f2dbb2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -90,9 +90,9 @@ "dev": true }, "@wxml/parser": { - "version": "0.3.2", - "resolved": "https://registry.npmjs.org/@wxml/parser/-/parser-0.3.2.tgz", - "integrity": "sha512-CW+So8MlXtyrt8Rb8IFDv5QZ3MkodqtypwHA4/bzRbcae5SlCXr4XhOoKJxViPvg8vJhKSlPJ+QJfc9UfP7Xvw==" + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/@wxml/parser/-/parser-0.4.0.tgz", + "integrity": "sha512-pS0OPyKzCOhCPGlcfO9IDLZbqH4485bPdGDqaa+hYnI+cY/OW+GN4J88XpfdzM8wXJcJShFZy5F6MTUr2rysXg==" }, "acorn": { "version": "7.4.1", diff --git a/package.json b/package.json index 35775f2..6a33218 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,6 @@ "prettier": "^2.5.1" }, "dependencies": { - "@wxml/parser": "^0.3.2" + "@wxml/parser": "^0.4.0" } } diff --git a/tests/rules/report-interpolation-error.js b/tests/rules/report-interpolation-error.js new file mode 100644 index 0000000..df5d20f --- /dev/null +++ b/tests/rules/report-interpolation-error.js @@ -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: ` {{23}} `, + }, + { + filename: "interpolation.wxml", + code: ` {{ show ? 23 : ''}} `, + }, + { + filename: "", + code: ` + + `, + }, + { + filename: "interpolation.wxml", + code: ` {{ }} `, + }, + { + filename: "interpolation.wxml", + code: ` + + `, + }, + { + filename: "interpolation.wxml", + code: ` {{ a + b }} `, + }, + { + filename: "interpolation.wxml", + code: ``, + }, + { + filename: "interpolation.wxml", + code: `{{"hello" + name}}`, + }, + { + filename: "interpolation.wxml", + code: `{{object.key}} {{array[0]}}`, + }, + { + filename: "interpolation.wxml", + code: ` {{item}} `, + }, + { + filename: "interpolation.wxml", + code: ``, + }, + { + filename: "interpolation.wxml", + code: ` +