define-props-declaration.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @author Amorites
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. module.exports = {
  8. meta: {
  9. type: 'suggestion',
  10. docs: {
  11. description: 'enforce declaration style of `defineProps`',
  12. categories: undefined,
  13. url: 'https://eslint.vuejs.org/rules/define-props-declaration.html'
  14. },
  15. fixable: null,
  16. schema: [
  17. {
  18. enum: ['type-based', 'runtime']
  19. }
  20. ],
  21. messages: {
  22. hasArg: 'Use type-based declaration instead of runtime declaration.',
  23. hasTypeArg: 'Use runtime declaration instead of type-based declaration.'
  24. }
  25. },
  26. /** @param {RuleContext} context */
  27. create(context) {
  28. const scriptSetup = utils.getScriptSetupElement(context)
  29. if (!scriptSetup || !utils.hasAttribute(scriptSetup, 'lang', 'ts')) {
  30. return {}
  31. }
  32. const defineType = context.options[0] || 'type-based'
  33. return utils.defineScriptSetupVisitor(context, {
  34. onDefinePropsEnter(node) {
  35. switch (defineType) {
  36. case 'type-based': {
  37. if (node.arguments.length > 0) {
  38. context.report({
  39. node,
  40. messageId: 'hasArg'
  41. })
  42. }
  43. break
  44. }
  45. case 'runtime': {
  46. if (node.typeParameters && node.typeParameters.params.length > 0) {
  47. context.report({
  48. node,
  49. messageId: 'hasTypeArg'
  50. })
  51. }
  52. break
  53. }
  54. }
  55. }
  56. })
  57. }
  58. }