Skip to content

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

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4844612
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 27, 2024
b770232
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 27, 2024
99e2f3e
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 27, 2024
f0294e8
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 27, 2024
5a4a15e
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 27, 2024
583c0db
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 27, 2024
4499597
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 27, 2024
17ac982
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 27, 2024
5e1d3b1
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 27, 2024
bc506f9
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 28, 2024
f301546
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 28, 2024
207477e
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 28, 2024
d08bed1
feat: autofix in `define-props-declaration`: runtime syntax to type-b…
mpiniarski May 28, 2024
6cb7153
Update lib/rules/define-props-declaration.js
mpiniarski Jun 21, 2024
f6c205f
fix: required default value = false
mpiniarski Jun 21, 2024
536c6a1
feature: rename autoFixToSeparateInterface option and describe it in …
mpiniarski Jun 21, 2024
100065d
chore: extract fixTypeBased function
mpiniarski Jun 21, 2024
7d9e731
chore: refactor fixTypeBased function
mpiniarski Jun 21, 2024
0715943
chore: refactor componentPropsTypeCode creation
mpiniarski Jun 24, 2024
bbdc134
fix: fix tests failing
mpiniarski Jul 1, 2024
2a1c654
feature: remove autoFixToSeparateInterface option
mpiniarski Jul 1, 2024
7cdf3ff
Merge branch 'master' into feature/#2465_autofix_in_define-props-decl…
mpiniarski Jul 2, 2024
0e6ea56
Fix tests
FloEdelmann Jul 2, 2024
e9d4400
feature: code cleanup
mpiniarski Jul 3, 2024
0bd915b
Lint
FloEdelmann Jul 4, 2024
1d58a2b
feature: handle array as props list (#2465)
mpiniarski Jul 15, 2024
da17d74
feature: catch errors and ignore them (#2465)
mpiniarski Jul 26, 2024
6634c2d
feature: do not handle array prop declaration (#2465)
mpiniarski Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: autofix in define-props-declaration: runtime syntax to type-b…
…ased syntax (#2465)

refactoring
  • Loading branch information
mpiniarski committed May 28, 2024
commit f3015464b0f98fc818da6cb29bead90086d43b1f
51 changes: 27 additions & 24 deletions lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
@@ -40,12 +40,15 @@ const mapNativeType = (/** @type {string} */ nativeType) => {
* @param {SourceCode} sourceCode
*/
function getComponentPropData(prop, sourceCode) {
if (prop.propName === null) {
throw new Error('Unexpected prop with null name.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it need to throw an error here?
Can't we make it guard against those before processing the autofix?

Copy link
Author

@mpiniarski mpiniarski Jul 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?
I think it's more clear if we throw an error here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if we throw an error, the user's eslint command will fail.
Did you throw the error with that intention? If so, why is that?
If auto-fix is not possible, I think it should be handled so that auto-fix is not performed, rather than throwing an error.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Let me catch errors and ignore them.

I prefer to leave a sign of an error somewhere, maybe in console.debug(), but it is up to you.

}
if (prop.type !== 'object') {
throw new Error(`Unexpected prop type: ${prop.type}.`)
}
const type = optionGetType(prop.value, sourceCode)
if (type === null) {
throw new Error(`Unexpected prop type: ${prop.type}.`)
throw new Error(`Unable to read prop type`)
}
const required = optionGetRequired(prop.value)
const defaultValue = optionGetDefault(prop.value)
@@ -69,33 +72,10 @@ function optionGetType(node, sourceCode) {
return mapNativeType(node.name)
}
case 'ObjectExpression': {
// foo: {
const typeProperty = utils.findProperty(node, 'type')
if (typeProperty == null) {
return null
}
if (typeProperty.value.type === 'TSAsExpression') {
const typeAnnotation = typeProperty.value.typeAnnotation
if (typeAnnotation.typeName.name !== 'PropType') {
return null
}

// in some project configuration parser populates deprecated field `typeParameters` instead of `typeArguments`
const typeArguments =
'typeArguments' in typeProperty.value
? typeAnnotation.typeArguments
: typeAnnotation.typeParameters

const typeArgument = Array.isArray(typeArguments)
? typeArguments[0].params[0]
: typeArguments.params[0]

if (typeArgument === undefined) {
return null
}

return sourceCode.getText(typeArgument)
}
return optionGetType(typeProperty.value, sourceCode)
}
case 'ArrayExpression': {
@@ -111,6 +91,29 @@ function optionGetType(node, sourceCode) {
.filter(Boolean)
.join(' | ')
}
case 'TSAsExpression': {
const typeAnnotation = node.typeAnnotation
if (typeAnnotation.typeName.name !== 'PropType') {
return null
}

// 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 null
}

return sourceCode.getText(typeArgument)
}

case 'FunctionExpression':
case 'ArrowFunctionExpression': {
return null
1 change: 0 additions & 1 deletion tests/lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
@@ -566,7 +566,6 @@ tester.run('define-props-declaration', rule, {
},
// Array of types
{
only: true,
filename: 'test.vue',
code: `
<script setup lang="ts">