From 8dcf67ed07c67e6b2e0c3000e871a314ad527f78 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sat, 4 Jul 2020 07:04:03 +0900 Subject: [PATCH] Fixed crash when using `vue/no-empty-component-block` and `vue/padding-line-between-blocks` rules in `.js` file --- lib/rules/no-empty-component-block.js | 8 +++-- lib/rules/padding-line-between-blocks.js | 8 +++-- tests/lib/rules-without-vue-sfc.js | 31 +++++++++++++++++++ tests/lib/rules/no-empty-component-block.js | 3 +- .../lib/rules/padding-line-between-blocks.js | 3 +- .../util-types/parser-services.ts | 2 +- 6 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 tests/lib/rules-without-vue-sfc.js diff --git a/lib/rules/no-empty-component-block.js b/lib/rules/no-empty-component-block.js index dc776705a..6378dcd83 100644 --- a/lib/rules/no-empty-component-block.js +++ b/lib/rules/no-empty-component-block.js @@ -69,10 +69,12 @@ module.exports = { if (!context.parserServices.getDocumentFragment) { return {} } + const documentFragment = context.parserServices.getDocumentFragment() + if (!documentFragment) { + return {} + } - const componentBlocks = context.parserServices - .getDocumentFragment() - .children.filter(isVElement) + const componentBlocks = documentFragment.children.filter(isVElement) return { Program() { diff --git a/lib/rules/padding-line-between-blocks.js b/lib/rules/padding-line-between-blocks.js index b9215e2c9..cc4773703 100644 --- a/lib/rules/padding-line-between-blocks.js +++ b/lib/rules/padding-line-between-blocks.js @@ -139,12 +139,16 @@ module.exports = { if (!context.parserServices.getDocumentFragment) { return {} } + const df = context.parserServices.getDocumentFragment() + if (!df) { + return {} + } + const documentFragment = df + /** @type {'always' | 'never'} */ const option = context.options[0] || 'always' const paddingType = PaddingTypes[option] - const documentFragment = context.parserServices.getDocumentFragment() - /** @type {Token[]} */ let tokens /** diff --git a/tests/lib/rules-without-vue-sfc.js b/tests/lib/rules-without-vue-sfc.js new file mode 100644 index 000000000..c1d5eb2be --- /dev/null +++ b/tests/lib/rules-without-vue-sfc.js @@ -0,0 +1,31 @@ +/** + * @author Yosuke Ota + * See LICENSE file in root directory for full license. + */ +'use strict' + +const Linter = require('eslint').Linter +const parser = require('vue-eslint-parser') +const rules = require('../..').rules + +describe("Don't crash even if without vue SFC.", () => { + const code = 'var a = 1' + + for (const key of Object.keys(rules)) { + const ruleId = `vue/${key}` + + it(ruleId, () => { + const linter = new Linter() + const config = { + parser: 'vue-eslint-parser', + parserOptions: { ecmaVersion: 2015 }, + rules: { + [ruleId]: 'error' + } + } + linter.defineParser('vue-eslint-parser', parser) + linter.defineRule(ruleId, rules[key]) + linter.verifyAndFix(code, config, 'test.js') + }) + } +}) diff --git a/tests/lib/rules/no-empty-component-block.js b/tests/lib/rules/no-empty-component-block.js index 92813aa28..8ba6a2f2c 100644 --- a/tests/lib/rules/no-empty-component-block.js +++ b/tests/lib/rules/no-empty-component-block.js @@ -22,7 +22,8 @@ tester.run('no-empty-component-block', rule, { ``, `