configDescriptor.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. const CONFIG = 'org.vue.eslintrc'
  2. const CATEGORIES = [
  3. 'essential',
  4. 'strongly-recommended',
  5. 'recommended',
  6. 'uncategorized'
  7. ]
  8. const DEFAULT_CATEGORY = 'essential'
  9. const RULE_SETTING_OFF = 'off'
  10. const RULE_SETTING_ERROR = 'error'
  11. const RULE_SETTING_WARNING = 'warn'
  12. const RULE_SETTINGS = [RULE_SETTING_OFF, RULE_SETTING_ERROR, RULE_SETTING_WARNING]
  13. const defaultChoices = [
  14. {
  15. name: 'org.vue.eslint.config.eslint.setting.off',
  16. value: JSON.stringify(RULE_SETTING_OFF)
  17. },
  18. {
  19. name: 'org.vue.eslint.config.eslint.setting.error',
  20. value: JSON.stringify(RULE_SETTING_ERROR)
  21. },
  22. {
  23. name: 'org.vue.eslint.config.eslint.setting.warning',
  24. value: JSON.stringify(RULE_SETTING_WARNING)
  25. }
  26. ]
  27. function escapeHTML (text) {
  28. return text.replace(/</g, '&lt;').replace(/>/g, '&gt;')
  29. }
  30. function getEslintConfigName (eslint) {
  31. let config = eslint.extends
  32. if (eslint.extends instanceof Array) {
  33. config = eslint.extends.find(configName => configName.startsWith('plugin:vue/'))
  34. }
  35. return config && config.startsWith('plugin:vue/') ? config : null
  36. }
  37. // Sets default value regarding selected global config
  38. function getDefaultValue (rule, data) {
  39. const { category: ruleCategory } = rule.meta.docs
  40. const currentCategory = getEslintConfigName(data.eslint)
  41. if (!currentCategory || ruleCategory === undefined) return RULE_SETTING_OFF
  42. return CATEGORIES.indexOf(ruleCategory) <= CATEGORIES.indexOf(currentCategory.split('/')[1])
  43. ? RULE_SETTING_ERROR
  44. : RULE_SETTING_OFF
  45. }
  46. function getEslintPrompts (data, rules) {
  47. const allRules = Object.keys(rules)
  48. .map(ruleKey => ({
  49. ...rules[ruleKey],
  50. name: `vue/${ruleKey}`
  51. }))
  52. return CATEGORIES
  53. .map(category =>
  54. allRules.filter(rule =>
  55. rule.meta.docs.category === category || (
  56. category === 'uncategorized' &&
  57. rule.meta.docs.category === undefined
  58. )
  59. )
  60. )
  61. .reduce((acc, rulesArr) => [...acc, ...rulesArr], [])
  62. .map(rule => {
  63. const value = data.eslint &&
  64. data.eslint.rules &&
  65. data.eslint.rules[rule.name]
  66. return {
  67. name: rule.name,
  68. type: 'list',
  69. message: rule.name,
  70. group: `org.vue.eslint.config.eslint.groups.${rule.meta.docs.category || 'uncategorized'}`,
  71. description: escapeHTML(rule.meta.docs.description),
  72. link: rule.meta.docs.url,
  73. default: JSON.stringify(getDefaultValue(rule, data)),
  74. value: JSON.stringify(value),
  75. choices: !value || RULE_SETTINGS.indexOf(value) > -1
  76. ? defaultChoices
  77. : [...defaultChoices, {
  78. name: 'org.vue.eslint.config.eslint.setting.custom',
  79. value: JSON.stringify(value)
  80. }]
  81. }
  82. })
  83. }
  84. function onRead ({ data, cwd }) {
  85. const { loadModule } = require('@vue/cli-shared-utils')
  86. const rules = loadModule('eslint-plugin-vue', cwd, true).rules
  87. return {
  88. tabs: [
  89. {
  90. id: 'general',
  91. label: 'org.vue.eslint.config.eslint.general.label',
  92. prompts: [
  93. {
  94. name: 'lintOnSave',
  95. type: 'confirm',
  96. message: 'org.vue.eslint.config.eslint.general.lintOnSave.message',
  97. description: 'org.vue.eslint.config.eslint.general.lintOnSave.description',
  98. link: 'https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint#configuration',
  99. default: true,
  100. value: data.vue && data.vue.lintOnSave
  101. },
  102. {
  103. name: 'config',
  104. type: 'list',
  105. message: 'org.vue.eslint.config.eslint.general.config.message',
  106. description: 'org.vue.eslint.config.eslint.general.config.description',
  107. link: 'https://github.com/vuejs/eslint-plugin-vue',
  108. default: `plugin:vue/${DEFAULT_CATEGORY}`,
  109. choices: CATEGORIES.filter(category => category !== 'uncategorized').map(category => ({
  110. name: `org.vue.eslint.config.eslint.groups.${category}`,
  111. value: `plugin:vue/${category}`
  112. })),
  113. value: getEslintConfigName(data.eslint)
  114. }
  115. ]
  116. },
  117. {
  118. id: 'rules',
  119. label: 'org.vue.eslint.config.eslint.rules.label',
  120. prompts: getEslintPrompts(data, rules)
  121. }
  122. ]
  123. }
  124. }
  125. async function onWrite ({ data, api, prompts }) {
  126. const eslintData = { ...data.eslint }
  127. const vueData = {}
  128. for (const prompt of prompts) {
  129. // eslintrc
  130. if (prompt.id === 'config') {
  131. if (eslintData.extends instanceof Array) {
  132. const vueEslintConfig = eslintData.extends.find(config => config.indexOf('plugin:vue/') === 0)
  133. const index = eslintData.extends.indexOf(vueEslintConfig)
  134. eslintData.extends[index] = JSON.parse(prompt.value)
  135. } else {
  136. eslintData.extends = JSON.parse(prompt.value)
  137. }
  138. } else if (prompt.id.indexOf('vue/') === 0) {
  139. eslintData[`rules.${prompt.id}`] = await api.getAnswer(prompt.id, JSON.parse)
  140. } else {
  141. // vue.config.js
  142. vueData[prompt.id] = await api.getAnswer(prompt.id)
  143. }
  144. }
  145. api.setData('eslint', eslintData)
  146. api.setData('vue', vueData)
  147. }
  148. const config = {
  149. id: CONFIG,
  150. name: 'ESLint configuration',
  151. description: 'org.vue.eslint.config.eslint.description',
  152. link: 'https://github.com/vuejs/eslint-plugin-vue',
  153. files: {
  154. eslint: {
  155. js: ['.eslintrc.js'],
  156. json: ['.eslintrc', '.eslintrc.json'],
  157. yaml: ['.eslintrc.yaml', '.eslintrc.yml'],
  158. package: 'eslintConfig'
  159. },
  160. vue: {
  161. js: ['vue.config.js']
  162. }
  163. },
  164. onRead,
  165. onWrite
  166. }
  167. module.exports = {
  168. config,
  169. getEslintConfigName,
  170. getDefaultValue,
  171. getEslintPrompts
  172. }