Skip to content

Fixed crash when using vue/no-empty-component-block and vue/padding-line-between-blocks rules in .js file #1232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/rules/no-empty-component-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/padding-line-between-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down
31 changes: 31 additions & 0 deletions tests/lib/rules-without-vue-sfc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @author Yosuke Ota <https://github.com/ota-meshi>
* 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')
})
}
})
3 changes: 2 additions & 1 deletion tests/lib/rules/no-empty-component-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ tester.run('no-empty-component-block', rule, {
`<template src="./template.html"></template><script src="./script.js"></script>`,
`<template src="./template.html" /><script src="./script.js" />`,
`<template src="./template.html"></template><script src="./script.js"></script><style src="./style.css"></style>`,
`<template src="./template.html" /><script src="./script.js" /><style src="./style.css" />`
`<template src="./template.html" /><script src="./script.js" /><style src="./style.css" />`,
`var a = 1`
],
invalid: [
{
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/rules/padding-line-between-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ tester.run('padding-line-between-blocks', rule, {
<style></style>
`,
options: ['never']
}
},
`var a = 1`
],
invalid: [
{
Expand Down
2 changes: 1 addition & 1 deletion typings/eslint-plugin-vue/util-types/parser-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface ParserServices {
templateBodyVisitor: TemplateListener,
scriptVisitor?: eslint.Rule.RuleListener
) => eslint.Rule.RuleListener
getDocumentFragment?: () => VAST.VDocumentFragment
getDocumentFragment?: () => VAST.VDocumentFragment | null
}
export namespace ParserServices {
export interface TokenStore {
Expand Down