/** * @author Yosuke Ota * See LICENSE file in root directory for full license. */ 'use strict' const utils = require('../utils') /** * @typedef { 'script-setup' | 'composition' | 'composition-vue2' | 'options' } PreferOption * * @typedef {PreferOption[]} UserPreferOption * * @typedef {object} NormalizeOptions * @property {object} allowsSFC * @property {boolean} [allowsSFC.scriptSetup] * @property {boolean} [allowsSFC.composition] * @property {boolean} [allowsSFC.compositionVue2] * @property {boolean} [allowsSFC.options] * @property {object} allowsOther * @property {boolean} [allowsOther.composition] * @property {boolean} [allowsOther.compositionVue2] * @property {boolean} [allowsOther.options] */ /** @type {PreferOption[]} */ const STYLE_OPTIONS = [ 'script-setup', 'composition', 'composition-vue2', 'options' ] /** * Normalize options. * @param {any[]} options The options user configured. * @returns {NormalizeOptions} The normalized options. */ function parseOptions(options) { /** @type {NormalizeOptions} */ const opts = { allowsSFC: {}, allowsOther: {} } /** @type {UserPreferOption} */ const preferOptions = options[0] || ['script-setup', 'composition'] for (const prefer of preferOptions) { if (prefer === 'script-setup') { opts.allowsSFC.scriptSetup = true } else if (prefer === 'composition') { opts.allowsSFC.composition = true opts.allowsOther.composition = true } else if (prefer === 'composition-vue2') { opts.allowsSFC.compositionVue2 = true opts.allowsOther.compositionVue2 = true } else if (prefer === 'options') { opts.allowsSFC.options = true opts.allowsOther.options = true } } if ( !opts.allowsOther.composition && !opts.allowsOther.compositionVue2 && !opts.allowsOther.options ) { opts.allowsOther.composition = true opts.allowsOther.compositionVue2 = true opts.allowsOther.options = true } return opts } const OPTIONS_API_OPTIONS = new Set([ 'mixins', 'extends', // state 'data', 'computed', 'methods', 'watch', 'provide', 'inject', // lifecycle 'beforeCreate', 'created', 'beforeMount', 'mounted', 'beforeUpdate', 'updated', 'activated', 'deactivated', 'beforeDestroy', 'beforeUnmount', 'destroyed', 'unmounted', 'render', 'renderTracked', 'renderTriggered', 'errorCaptured', // public API 'expose' ]) const COMPOSITION_API_OPTIONS = new Set(['setup']) const COMPOSITION_API_VUE2_OPTIONS = new Set([ 'setup', 'render', // https://github.com/vuejs/composition-api#template-refs 'renderTracked', // https://github.com/vuejs/composition-api#missing-apis 'renderTriggered' // https://github.com/vuejs/composition-api#missing-apis ]) const LIFECYCLE_HOOK_OPTIONS = new Set([ 'beforeCreate', 'created', 'beforeMount', 'mounted', 'beforeUpdate', 'updated', 'activated', 'deactivated', 'beforeDestroy', 'beforeUnmount', 'destroyed', 'unmounted', 'renderTracked', 'renderTriggered', 'errorCaptured' ]) /** * @typedef { 'script-setup' | 'composition' | 'options' } ApiStyle */ /** * @param {object} allowsOpt * @param {boolean} [allowsOpt.scriptSetup] * @param {boolean} [allowsOpt.composition] * @param {boolean} [allowsOpt.compositionVue2] * @param {boolean} [allowsOpt.options] */ function buildAllowedPhrase(allowsOpt) { const phrases = [] if (allowsOpt.scriptSetup) { phrases.push('`