|  | 
|  | 1 | +/** | 
|  | 2 | + * @author Yosuke Ota | 
|  | 3 | + * See LICENSE file in root directory for full license. | 
|  | 4 | + */ | 
|  | 5 | +'use strict' | 
|  | 6 | + | 
|  | 7 | +const { Range } = require('semver') | 
|  | 8 | +const utils = require('../utils') | 
|  | 9 | + | 
|  | 10 | +const FEATURES = { | 
|  | 11 | +  // Vue.js 2.5.0+ | 
|  | 12 | +  'slot-scope-attribute': require('./syntaxes/slot-scope-attribute'), | 
|  | 13 | +  // Vue.js 2.6.0+ | 
|  | 14 | +  'dynamic-directive-arguments': require('./syntaxes/dynamic-directive-arguments'), | 
|  | 15 | +  'v-slot': require('./syntaxes/v-slot'), | 
|  | 16 | + | 
|  | 17 | +  // >=2.6.0-beta.1 <=2.6.0-beta.3 | 
|  | 18 | +  'v-bind-prop-modifier-shorthand': require('./syntaxes/v-bind-prop-modifier-shorthand') | 
|  | 19 | +} | 
|  | 20 | + | 
|  | 21 | +const cache = new Map() | 
|  | 22 | +/** | 
|  | 23 | + * Get the `semver.Range` object of a given range text. | 
|  | 24 | + * @param {string} x The text expression for a semver range. | 
|  | 25 | + * @returns {Range|null} The range object of a given range text. | 
|  | 26 | + * It's null if the `x` is not a valid range text. | 
|  | 27 | + */ | 
|  | 28 | +function getSemverRange (x) { | 
|  | 29 | +  const s = String(x) | 
|  | 30 | +  let ret = cache.get(s) || null | 
|  | 31 | + | 
|  | 32 | +  if (!ret) { | 
|  | 33 | +    try { | 
|  | 34 | +      ret = new Range(s) | 
|  | 35 | +    } catch (_error) { | 
|  | 36 | +      // Ignore parsing error. | 
|  | 37 | +    } | 
|  | 38 | +    cache.set(s, ret) | 
|  | 39 | +  } | 
|  | 40 | + | 
|  | 41 | +  return ret | 
|  | 42 | +} | 
|  | 43 | + | 
|  | 44 | +/** | 
|  | 45 | + * Merge two visitors. | 
|  | 46 | + * @param {Visitor} x The visitor which is assigned. | 
|  | 47 | + * @param {Visitor} y The visitor which is assigning. | 
|  | 48 | + * @returns {Visitor} `x`. | 
|  | 49 | + */ | 
|  | 50 | +function merge (x, y) { | 
|  | 51 | +  for (const key of Object.keys(y)) { | 
|  | 52 | +    if (typeof x[key] === 'function') { | 
|  | 53 | +      if (x[key]._handlers == null) { | 
|  | 54 | +        const fs = [x[key], y[key]] | 
|  | 55 | +        x[key] = node => fs.forEach(h => h(node)) | 
|  | 56 | +        x[key]._handlers = fs | 
|  | 57 | +      } else { | 
|  | 58 | +        x[key]._handlers.push(y[key]) | 
|  | 59 | +      } | 
|  | 60 | +    } else { | 
|  | 61 | +      x[key] = y[key] | 
|  | 62 | +    } | 
|  | 63 | +  } | 
|  | 64 | +  return x | 
|  | 65 | +} | 
|  | 66 | + | 
|  | 67 | +module.exports = { | 
|  | 68 | +  meta: { | 
|  | 69 | +    type: 'suggestion', | 
|  | 70 | +    docs: { | 
|  | 71 | +      description: 'disallow unsupported Vue.js syntax on the specified version', | 
|  | 72 | +      category: undefined, | 
|  | 73 | +      url: 'https://eslint.vuejs.org/rules/no-unsupported-features.html' | 
|  | 74 | +    }, | 
|  | 75 | +    fixable: 'code', | 
|  | 76 | +    schema: [ | 
|  | 77 | +      { | 
|  | 78 | +        type: 'object', | 
|  | 79 | +        properties: { | 
|  | 80 | +          version: { | 
|  | 81 | +            type: 'string' | 
|  | 82 | +          }, | 
|  | 83 | +          ignores: { | 
|  | 84 | +            type: 'array', | 
|  | 85 | +            items: { | 
|  | 86 | +              enum: Object.keys(FEATURES) | 
|  | 87 | +            }, | 
|  | 88 | +            uniqueItems: true | 
|  | 89 | +          } | 
|  | 90 | +        }, | 
|  | 91 | +        additionalProperties: false | 
|  | 92 | +      } | 
|  | 93 | +    ], | 
|  | 94 | +    messages: { | 
|  | 95 | +      // Vue.js 2.5.0+ | 
|  | 96 | +      forbiddenSlotScopeAttribute: '`slot-scope` are not supported until Vue.js "2.5.0".', | 
|  | 97 | +      // Vue.js 2.6.0+ | 
|  | 98 | +      forbiddenDynamicDirectiveArguments: 'Dynamic arguments are not supported until Vue.js "2.6.0".', | 
|  | 99 | +      forbiddenVSlot: '`v-slot` are not supported until Vue.js "2.6.0".', | 
|  | 100 | + | 
|  | 101 | +      // >=2.6.0-beta.1 <=2.6.0-beta.3 | 
|  | 102 | +      forbiddenVBindPropModifierShorthand: '`.prop` shorthand are not supported except Vue.js ">=2.6.0-beta.1 <=2.6.0-beta.3".' | 
|  | 103 | +    } | 
|  | 104 | +  }, | 
|  | 105 | +  create (context) { | 
|  | 106 | +    const { version, ignores } = Object.assign( | 
|  | 107 | +      { | 
|  | 108 | +        version: null, | 
|  | 109 | +        ignores: [] | 
|  | 110 | +      }, | 
|  | 111 | +      context.options[0] || {} | 
|  | 112 | +    ) | 
|  | 113 | +    if (!version) { | 
|  | 114 | +      // version is not set. | 
|  | 115 | +      return {} | 
|  | 116 | +    } | 
|  | 117 | +    const versionRange = getSemverRange(version) | 
|  | 118 | + | 
|  | 119 | +    /** | 
|  | 120 | +     * Check whether a given case object is full-supported on the configured node version. | 
|  | 121 | +     * @param {{supported:string}} aCase The case object to check. | 
|  | 122 | +     * @returns {boolean} `true` if it's supporting. | 
|  | 123 | +     */ | 
|  | 124 | +    function isNotSupportingVersion (aCase) { | 
|  | 125 | +      if (typeof aCase.supported === 'function') { | 
|  | 126 | +        return !aCase.supported(versionRange) | 
|  | 127 | +      } | 
|  | 128 | +      return versionRange.intersects(getSemverRange(`<${aCase.supported}`)) | 
|  | 129 | +    } | 
|  | 130 | +    const templateBodyVisitor = Object.keys(FEATURES) | 
|  | 131 | +      .filter(syntaxName => !ignores.includes(syntaxName)) | 
|  | 132 | +      .filter(syntaxName => isNotSupportingVersion(FEATURES[syntaxName])) | 
|  | 133 | +      .reduce((result, syntaxName) => { | 
|  | 134 | +        const visitor = FEATURES[syntaxName].createTemplateBodyVisitor(context) | 
|  | 135 | +        if (visitor) { | 
|  | 136 | +          merge(result, visitor) | 
|  | 137 | +        } | 
|  | 138 | +        return result | 
|  | 139 | +      }, {}) | 
|  | 140 | + | 
|  | 141 | +    return utils.defineTemplateBodyVisitor(context, templateBodyVisitor) | 
|  | 142 | +  } | 
|  | 143 | +} | 
0 commit comments