Skip to content

Commit dad5cd6

Browse files
committed
Implement support for line comments
1 parent b2655ae commit dad5cd6

File tree

2 files changed

+71
-37
lines changed

2 files changed

+71
-37
lines changed

lib/utils/index.js

+34-13
Original file line numberDiff line numberDiff line change
@@ -341,16 +341,6 @@ module.exports = {
341341
})
342342
},
343343

344-
/**
345-
* Check whether the given node is a Vue component based
346-
* @param {RuleContext} context The ESLint rule context object.
347-
* @returns {boolean}
348-
*/
349-
hasVueComponentComment (context) {
350-
const sourceCode = context.getSourceCode()
351-
return Boolean(sourceCode.getAllComments().find(item => /@vue\/component/g.test(item.value)))
352-
},
353-
354344
/**
355345
* Check whether the given node is a Vue component based
356346
* on the filename and default export type
@@ -442,21 +432,52 @@ module.exports = {
442432
executeOnVueComponent (context, cb) {
443433
const filePath = context.getFilename()
444434
const _this = this
435+
const componentComments = []
436+
const foundNodes = []
437+
438+
const hasNode = (node) => {
439+
return foundNodes.some(el => el.loc.start.line === node.loc.start.line)
440+
}
445441

446442
return {
443+
LineComment (node) {
444+
// line comment with @vue/component
445+
if (!_this.isVueComponentComment(node.value)) return
446+
componentComments.push(node)
447+
},
448+
BlockComment (node) {
449+
// block comment with @vue/component
450+
if (!_this.isVueComponentComment(node.value)) return
451+
componentComments.push(node)
452+
},
453+
ObjectExpression (node) {
454+
// comment with @vue/component
455+
if (!componentComments.some(el => el.loc.end.line === node.loc.start.line - 1) || hasNode(node)) return
456+
foundNodes.push(node)
457+
cb(node)
458+
},
447459
'ExportDefaultDeclaration:exit' (node) {
448-
// export default {} in .vue || .jsx or comment with @vue/component
449-
if (!_this.isVueComponentFile(node, filePath, context) && !_this.hasVueComponentComment(context)) return
460+
// export default {} in .vue || .jsx
461+
if (!_this.isVueComponentFile(node, filePath, context) || hasNode(node)) return
450462
cb(node.declaration)
451463
},
452464
'CallExpression:exit' (node) {
453465
// Vue.component('xxx', {}) || component('xxx', {})
454-
if (!_this.isVueComponent(node)) return
466+
if (!_this.isVueComponent(node) || foundNodes.some(el => el.loc.start.line === node.loc.start.line)) return
455467
cb(node.arguments.slice(-1)[0])
456468
}
457469
}
458470
},
459471

472+
/**
473+
* Check whether the given comment is a Vue component based
474+
* @param {string} value Conent of comment.
475+
* @return {boolean}
476+
*/
477+
isVueComponentComment (value) {
478+
return /@vue\/component/g.test(value)
479+
},
480+
460481
/**
461482
* Return generator with all properties
462483
* @param {ASTNode} node Node to check

tests/lib/utils/vue-component.js

+37-24
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,13 @@ function foundTests (ext) {
105105
return [
106106
{
107107
filename: `test.${ext}`,
108-
code: `Vue.component('async-example', function (resolve, reject) {
108+
code: `// ${ext}
109+
Vue.component('async-example', function (resolve, reject) {
109110
// @vue/component
110111
resolve({})
111112
})`,
112113
parserOptions,
113-
errors: [makeError(2)]
114+
errors: [makeError(4)]
114115
},
115116
{
116117
filename: `test.${ext}`,
@@ -120,82 +121,93 @@ function foundTests (ext) {
120121
},
121122
{
122123
filename: `test.${ext}`,
123-
code: `// @vue/component
124+
code: `// ${ext}
125+
// @vue/component
124126
export default { }`,
125127
parserOptions,
126-
errors: [makeError(2)]
128+
errors: [makeError(3)]
127129
},
128130
{
129131
filename: `test.${ext}`,
130-
code: `/* @vue/component */
131-
export default { }`,
132+
code: `// ${ext}
133+
/* @vue/component */
134+
export default { }
135+
// ${ext}`,
132136
parserOptions,
133-
errors: [makeError(2)]
137+
errors: [makeError(3)]
134138
},
135139
{
136140
filename: `test.${ext}`,
137141
code: `
138142
/*
143+
* ext: ${ext}
139144
* @vue/component
140145
*/
141-
export default { }`,
146+
export default { }
147+
// ${ext}`,
142148
parserOptions,
143-
errors: [makeError(5)]
149+
errors: [makeError(6)]
144150
},
145151
{
146152
filename: `test.${ext}`,
147153
code: `// @vue/component
148154
export default { }
149155
// @vue/component
150-
export var a = { }`,
156+
export var a = { }
157+
// ${ext}`,
151158
parserOptions,
152159
errors: [makeError(2), makeError(4)]
153160
},
154161
{
155162
filename: `test.${ext}`,
156163
code: `/* @vue/component */
157-
export const a = { }
164+
export const foo = { }
158165
/* @vue/component */
159-
export default { }`,
166+
export default { }
167+
// ${ext}`,
160168
parserOptions,
161169
errors: [makeError(2), makeError(4)]
162170
},
163171
{
164172
filename: `test.${ext}`,
165173
code: `export default { }
166174
// @vue/component
167-
export let b = { }`,
175+
export let foo = { }
176+
// ${ext}`,
168177
parserOptions,
169-
errors: [makeError(3)]
178+
errors: (ext === 'js' ? [] : [makeError(1)]).concat([makeError(3)])
170179
},
171180
{
172181
filename: `test.${ext}`,
173-
code: `let b = { }
182+
code: `let foo = { }
174183
// @vue/component
175-
export let b = { }`,
184+
export let bar = { }
185+
// ${ext}`,
176186
parserOptions,
177187
errors: [makeError(3)]
178188
},
179189
{
180190
filename: `test.${ext}`,
181-
code: `export var b = { }
191+
code: `export var dar = { }
182192
// @vue/component
183193
foo({ })
184-
bar({ })`,
194+
bar({ })
195+
// ${ext}`,
185196
parserOptions,
186197
errors: [makeError(3)]
187198
},
188199
{
189200
filename: `test.${ext}`,
190201
code: `foo({ })
191202
export default {
192-
test: {}
203+
test: {},
193204
// @vue/component
194205
foo: { }
195206
}
196-
bar({ })`,
207+
bar({ })
208+
// ${ext}`,
197209
parserOptions,
198-
errors: [makeError(5)]
210+
errors: (ext === 'js' ? [] : [makeError(2)]).concat([makeError(5)])
199211
},
200212
{
201213
filename: `test.${ext}`,
@@ -207,9 +219,10 @@ function foundTests (ext) {
207219
// @vue/component
208220
return {}
209221
}
210-
}`,
222+
}
223+
// ${ext}`,
211224
parserOptions,
212-
errors: [makeError(7)]
225+
errors: (ext === 'js' ? [] : [makeError(1)]).concat([makeError(7)])
213226
}
214227
]
215228
}
@@ -219,7 +232,7 @@ function foundTests (ext) {
219232
// ------------------------------------------------------------------------------
220233

221234
const ruleTester = new RuleTester()
222-
ruleTester.run('return-in-computed-property', rule, {
235+
ruleTester.run('vue-component', rule, {
223236

224237
valid: [
225238
{

0 commit comments

Comments
 (0)