|
| 1 | +/** |
| 2 | + * @author Toru Nagashima <https://github.com/mysticatea> |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ----------------------------------------------------------------------------- |
| 8 | +// Helpers |
| 9 | +// ----------------------------------------------------------------------------- |
| 10 | + |
| 11 | +const COMMENT_DIRECTIVE_B = /^\s*(eslint-(?:en|dis)able)\s*(?:(\S|\S[\s\S]*\S)\s*)?$/ |
| 12 | +const COMMENT_DIRECTIVE_L = /^\s*(eslint-disable(?:-next)?-line)\s*(?:(\S|\S[\s\S]*\S)\s*)?$/ |
| 13 | + |
| 14 | +/** |
| 15 | + * Parse a given comment. |
| 16 | + * @param {RegExp} pattern The RegExp pattern to parse. |
| 17 | + * @param {string} comment The comment value to parse. |
| 18 | + * @returns {({type:string,rules:string[]})|null} The parsing result. |
| 19 | + */ |
| 20 | +function parse (pattern, comment) { |
| 21 | + const match = pattern.exec(comment) |
| 22 | + if (match == null) { |
| 23 | + return null |
| 24 | + } |
| 25 | + |
| 26 | + const type = match[1] |
| 27 | + const rules = (match[2] || '') |
| 28 | + .split(',') |
| 29 | + .map(s => s.trim()) |
| 30 | + .filter(Boolean) |
| 31 | + |
| 32 | + return { type, rules } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Enable rules. |
| 37 | + * @param {RuleContext} context The rule context. |
| 38 | + * @param {{line:number,column:number}} loc The location information to enable. |
| 39 | + * @param {string} group The group to enable. |
| 40 | + * @param {string[]} rules The rule IDs to enable. |
| 41 | + * @returns {void} |
| 42 | + */ |
| 43 | +function enable (context, loc, group, rules) { |
| 44 | + if (rules.length === 0) { |
| 45 | + context.report({ loc, message: '++ {{group}}', data: { group }}) |
| 46 | + } else { |
| 47 | + context.report({ loc, message: '+ {{group}} {{rules}}', data: { group, rules: rules.join(' ') }}) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Disable rules. |
| 53 | + * @param {RuleContext} context The rule context. |
| 54 | + * @param {{line:number,column:number}} loc The location information to disable. |
| 55 | + * @param {string} group The group to disable. |
| 56 | + * @param {string[]} rules The rule IDs to disable. |
| 57 | + * @returns {void} |
| 58 | + */ |
| 59 | +function disable (context, loc, group, rules) { |
| 60 | + if (rules.length === 0) { |
| 61 | + context.report({ loc, message: '-- {{group}}', data: { group }}) |
| 62 | + } else { |
| 63 | + context.report({ loc, message: '- {{group}} {{rules}}', data: { group, rules: rules.join(' ') }}) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Process a given comment token. |
| 69 | + * If the comment is `eslint-disable` or `eslint-enable` then it reports the comment. |
| 70 | + * @param {RuleContext} context The rule context. |
| 71 | + * @param {Token} comment The comment token to process. |
| 72 | + * @returns {void} |
| 73 | + */ |
| 74 | +function processBlock (context, comment) { |
| 75 | + const parsed = parse(COMMENT_DIRECTIVE_B, comment.value) |
| 76 | + if (parsed != null) { |
| 77 | + if (parsed.type === 'eslint-disable') { |
| 78 | + disable(context, comment.loc.start, 'block', parsed.rules) |
| 79 | + } else { |
| 80 | + enable(context, comment.loc.start, 'block', parsed.rules) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Process a given comment token. |
| 87 | + * If the comment is `eslint-disable-line` or `eslint-disable-next-line` then it reports the comment. |
| 88 | + * @param {RuleContext} context The rule context. |
| 89 | + * @param {Token} comment The comment token to process. |
| 90 | + * @returns {void} |
| 91 | + */ |
| 92 | +function processLine (context, comment) { |
| 93 | + const parsed = parse(COMMENT_DIRECTIVE_L, comment.value) |
| 94 | + if (parsed != null && comment.loc.start.line === comment.loc.end.line) { |
| 95 | + const line = comment.loc.start.line + (parsed.type === 'eslint-disable-line' ? 0 : 1) |
| 96 | + const column = -1 |
| 97 | + disable(context, { line, column }, 'line', parsed.rules) |
| 98 | + enable(context, { line: line + 1, column }, 'line', parsed.rules) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * The implementation of `vue/comment-directive` rule. |
| 104 | + * @param {Program} node The program node to parse. |
| 105 | + * @returns {Object} The visitor of this rule. |
| 106 | + */ |
| 107 | +function create (context) { |
| 108 | + return { |
| 109 | + Program (node) { |
| 110 | + const comments = (node.templateBody && node.templateBody.comments) || [] |
| 111 | + for (const comment of comments) { |
| 112 | + processBlock(context, comment) |
| 113 | + processLine(context, comment) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// ----------------------------------------------------------------------------- |
| 120 | +// Rule Definition |
| 121 | +// ----------------------------------------------------------------------------- |
| 122 | + |
| 123 | +module.exports = { |
| 124 | + meta: { |
| 125 | + docs: { |
| 126 | + description: 'support comment-directives in `<template>`', // eslint-disable-line consistent-docs-description |
| 127 | + category: 'base' |
| 128 | + }, |
| 129 | + schema: [] |
| 130 | + }, |
| 131 | + create |
| 132 | +} |
0 commit comments