Skip to content

Commit 8dcf67e

Browse files
committed
Fixed crash when using vue/no-empty-component-block and vue/padding-line-between-blocks rules in .js file
1 parent a2614ef commit 8dcf67e

File tree

6 files changed

+47
-8
lines changed

6 files changed

+47
-8
lines changed

lib/rules/no-empty-component-block.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ module.exports = {
6969
if (!context.parserServices.getDocumentFragment) {
7070
return {}
7171
}
72+
const documentFragment = context.parserServices.getDocumentFragment()
73+
if (!documentFragment) {
74+
return {}
75+
}
7276

73-
const componentBlocks = context.parserServices
74-
.getDocumentFragment()
75-
.children.filter(isVElement)
77+
const componentBlocks = documentFragment.children.filter(isVElement)
7678

7779
return {
7880
Program() {

lib/rules/padding-line-between-blocks.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,16 @@ module.exports = {
139139
if (!context.parserServices.getDocumentFragment) {
140140
return {}
141141
}
142+
const df = context.parserServices.getDocumentFragment()
143+
if (!df) {
144+
return {}
145+
}
146+
const documentFragment = df
147+
142148
/** @type {'always' | 'never'} */
143149
const option = context.options[0] || 'always'
144150
const paddingType = PaddingTypes[option]
145151

146-
const documentFragment = context.parserServices.getDocumentFragment()
147-
148152
/** @type {Token[]} */
149153
let tokens
150154
/**

tests/lib/rules-without-vue-sfc.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @author Yosuke Ota <https://github.com/ota-meshi>
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
const Linter = require('eslint').Linter
8+
const parser = require('vue-eslint-parser')
9+
const rules = require('../..').rules
10+
11+
describe("Don't crash even if without vue SFC.", () => {
12+
const code = 'var a = 1'
13+
14+
for (const key of Object.keys(rules)) {
15+
const ruleId = `vue/${key}`
16+
17+
it(ruleId, () => {
18+
const linter = new Linter()
19+
const config = {
20+
parser: 'vue-eslint-parser',
21+
parserOptions: { ecmaVersion: 2015 },
22+
rules: {
23+
[ruleId]: 'error'
24+
}
25+
}
26+
linter.defineParser('vue-eslint-parser', parser)
27+
linter.defineRule(ruleId, rules[key])
28+
linter.verifyAndFix(code, config, 'test.js')
29+
})
30+
}
31+
})

tests/lib/rules/no-empty-component-block.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ tester.run('no-empty-component-block', rule, {
2222
`<template src="./template.html"></template><script src="./script.js"></script>`,
2323
`<template src="./template.html" /><script src="./script.js" />`,
2424
`<template src="./template.html"></template><script src="./script.js"></script><style src="./style.css"></style>`,
25-
`<template src="./template.html" /><script src="./script.js" /><style src="./style.css" />`
25+
`<template src="./template.html" /><script src="./script.js" /><style src="./style.css" />`,
26+
`var a = 1`
2627
],
2728
invalid: [
2829
{

tests/lib/rules/padding-line-between-blocks.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ tester.run('padding-line-between-blocks', rule, {
8383
<style></style>
8484
`,
8585
options: ['never']
86-
}
86+
},
87+
`var a = 1`
8788
],
8889
invalid: [
8990
{

typings/eslint-plugin-vue/util-types/parser-services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface ParserServices {
1717
templateBodyVisitor: TemplateListener,
1818
scriptVisitor?: eslint.Rule.RuleListener
1919
) => eslint.Rule.RuleListener
20-
getDocumentFragment?: () => VAST.VDocumentFragment
20+
getDocumentFragment?: () => VAST.VDocumentFragment | null
2121
}
2222
export namespace ParserServices {
2323
export interface TokenStore {

0 commit comments

Comments
 (0)