-
- Notifications
You must be signed in to change notification settings - Fork 697
implements autofix in define-props-declaration (#2465) #2466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 23 commits
4844612 b770232 99e2f3e f0294e8 5a4a15e 583c0db 4499597 17ac982 5e1d3b1 bc506f9 f301546 207477e d08bed1 6cb7153 f6c205f 536c6a1 100065d 7d9e731 0715943 bbdc134 2a1c654 7cdf3ff 0e6ea56 e9d4400 0bd915b 1d58a2b da17d74 6634c2d File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -6,6 +6,215 @@ | |
| | ||
| const utils = require('../utils') | ||
| | ||
| const PROPS_SEPARATOR = ', ' | ||
| | ||
| /** | ||
| * @param {RuleFixer} fixer | ||
| * @param {CallExpression} node | ||
| * @param {ComponentProp[]} props | ||
| * @param {RuleContext} context | ||
| */ | ||
| function* fixTypeBased(fixer, node, props, context) { | ||
| const sourceCode = context.getSourceCode() | ||
| | ||
| const componentPropsData = props.map((prop) => | ||
| getComponentPropData(prop, sourceCode) | ||
| ) | ||
| | ||
| const componentPropsTypes = componentPropsData.map( | ||
| ({ name, type, required, defaultValue }) => { | ||
| const isOptional = required === false || defaultValue | ||
| return `${name}${isOptional ? '?' : ''}: ${type}` | ||
| } | ||
| ) | ||
| | ||
| const componentPropsTypeCode = `{ ${componentPropsTypes.join(PROPS_SEPARATOR)} }` | ||
| | ||
| // remove defineProps function parameters | ||
| yield fixer.replaceText(node.arguments[0], '') | ||
| | ||
| // add type annotation | ||
| yield fixer.insertTextAfter(node.callee, `<${componentPropsTypeCode}>`) | ||
| | ||
| // add defaults if needed | ||
| const propTypesDataWithDefaultValue = componentPropsData.filter( | ||
| ({ defaultValue }) => defaultValue | ||
| ) | ||
| if (propTypesDataWithDefaultValue.length > 0) { | ||
| const defaultsCode = propTypesDataWithDefaultValue | ||
| .map( | ||
| ({ name, defaultValue }) => | ||
| `${name}: ${sourceCode.getText(defaultValue)}` | ||
| ) | ||
| .join(PROPS_SEPARATOR) | ||
| | ||
| yield fixer.insertTextBefore(node, `withDefaults(`) | ||
| yield fixer.insertTextAfter(node, `, { ${defaultsCode} })`) | ||
| } | ||
| return null | ||
| } | ||
| const mapNativeType = (/** @type {string} */ nativeType) => { | ||
mpiniarski marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| switch (nativeType) { | ||
| case 'String': { | ||
| return 'string' | ||
| } | ||
| case 'Number': { | ||
| return 'number' | ||
| } | ||
| case 'Boolean': { | ||
| return 'boolean' | ||
| } | ||
| case 'Object': { | ||
| return 'Record<string, any>' | ||
| } | ||
| case 'Array': { | ||
| return 'any[]' | ||
| } | ||
| case 'Function': { | ||
| return '(...args: any[]) => any' | ||
| } | ||
| case 'Symbol': { | ||
| return 'symbol' | ||
| } | ||
| default: { | ||
| return nativeType | ||
| } | ||
| } | ||
| } | ||
| | ||
| /** | ||
| * @param {ComponentProp} prop | ||
| * @param {SourceCode} sourceCode | ||
| */ | ||
| function getComponentPropData(prop, sourceCode) { | ||
| if (prop.propName === null) { | ||
| throw new Error('Unexpected prop with null name.') | ||
| ||
| } | ||
| if (prop.type !== 'object') { | ||
| throw new Error(`Unexpected prop type: ${prop.type}.`) | ||
| } | ||
| const type = optionGetType(prop.value, sourceCode) | ||
| const required = optionGetRequired(prop.value) | ||
| const defaultValue = optionGetDefault(prop.value) | ||
| | ||
| return { | ||
| name: prop.propName, | ||
| type, | ||
| required, | ||
| defaultValue | ||
| } | ||
| } | ||
| | ||
| /** | ||
| * @param {Expression} node | ||
| * @param {SourceCode} sourceCode | ||
| * @returns {string} | ||
| */ | ||
| function optionGetType(node, sourceCode) { | ||
| switch (node.type) { | ||
| case 'Identifier': { | ||
| return mapNativeType(node.name) | ||
| } | ||
| case 'ObjectExpression': { | ||
| const typeProperty = utils.findProperty(node, 'type') | ||
| if (typeProperty == null) { | ||
| return sourceCode.getText(node) | ||
| } | ||
| return optionGetType(typeProperty.value, sourceCode) | ||
| } | ||
| case 'ArrayExpression': { | ||
| return node.elements | ||
| .map((element) => { | ||
| // TODO handle SpreadElement | ||
FloEdelmann marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| if (element === null || element.type === 'SpreadElement') { | ||
| return sourceCode.getText(node) | ||
| } | ||
| | ||
| return optionGetType(element, sourceCode) | ||
| }) | ||
| .filter(Boolean) | ||
| .join(' | ') | ||
| } | ||
| case 'TSAsExpression': { | ||
| const typeAnnotation = node.typeAnnotation | ||
| if (typeAnnotation.typeName.name !== 'PropType') { | ||
| return sourceCode.getText(node) | ||
| } | ||
| | ||
| // in some project configuration parser populates deprecated field `typeParameters` instead of `typeArguments` | ||
| const typeArguments = | ||
| 'typeArguments' in node | ||
| ? typeAnnotation.typeArguments | ||
| : typeAnnotation.typeParameters | ||
| | ||
| const typeArgument = Array.isArray(typeArguments) | ||
| ? typeArguments[0].params[0] | ||
| : typeArguments.params[0] | ||
| | ||
| if (typeArgument === undefined) { | ||
| return sourceCode.getText(node) | ||
| } | ||
| | ||
| return sourceCode.getText(typeArgument) | ||
| } | ||
| case 'LogicalExpression': { | ||
| if (node.operator === '||') { | ||
| const left = optionGetType(node.left, sourceCode) | ||
| const right = optionGetType(node.right, sourceCode) | ||
| if (left && right) { | ||
| return `${left} | ${right}` | ||
| } | ||
| } | ||
| return sourceCode.getText(node) | ||
| } | ||
| default: { | ||
| return sourceCode.getText(node) | ||
| } | ||
| } | ||
| } | ||
| | ||
| /** | ||
| * @param {Expression} node | ||
| * @returns {boolean | undefined } | ||
mpiniarski marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| */ | ||
| function optionGetRequired(node) { | ||
| if (node.type === 'ObjectExpression') { | ||
| const requiredProperty = utils.findProperty(node, 'required') | ||
| if (requiredProperty == null) { | ||
| return false | ||
| } | ||
| | ||
| if (requiredProperty.value.type === 'Literal') { | ||
| return Boolean(requiredProperty.value.value) | ||
| } | ||
| } | ||
| | ||
| // Unknown | ||
| return false | ||
| } | ||
| | ||
| /** | ||
| * @param {Expression} node | ||
| * @returns {Expression | undefined } | ||
mpiniarski marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| */ | ||
| function optionGetDefault(node) { | ||
| if (node.type === 'ObjectExpression') { | ||
| const defaultProperty = utils.findProperty(node, 'default') | ||
| if (defaultProperty == null) { | ||
| return undefined | ||
| } | ||
| | ||
| return defaultProperty.value | ||
| } | ||
| | ||
| // Unknown | ||
| return undefined | ||
| } | ||
| | ||
| /** | ||
| * @typedef {import('../utils').ComponentProp} ComponentProp | ||
FloEdelmann marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| */ | ||
| | ||
| module.exports = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| | @@ -14,7 +223,7 @@ module.exports = { | |
| categories: undefined, | ||
| url: 'https://eslint.vuejs.org/rules/define-props-declaration.html' | ||
| }, | ||
| fixable: null, | ||
| fixable: 'code', | ||
| schema: [ | ||
| { | ||
| enum: ['type-based', 'runtime'] | ||
| | @@ -33,14 +242,18 @@ module.exports = { | |
| } | ||
| | ||
| const defineType = context.options[0] || 'type-based' | ||
| | ||
| return utils.defineScriptSetupVisitor(context, { | ||
| onDefinePropsEnter(node) { | ||
| onDefinePropsEnter(node, props) { | ||
| switch (defineType) { | ||
| case 'type-based': { | ||
| if (node.arguments.length > 0) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'hasArg' | ||
| messageId: 'hasArg', | ||
| *fix(fixer) { | ||
FloEdelmann marked this conversation as resolved. Show resolved Hide resolved | ||
| yield* fixTypeBased(fixer, node, props, context) | ||
| } | ||
| }) | ||
| } | ||
| break | ||
| | ||
Uh oh!
There was an error while loading. Please reload this page.