Skip to content
Open
Show file tree
Hide file tree
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

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
feature: handle array as props list (#2465)
  • Loading branch information
mpiniarski committed Jul 15, 2024
commit 1d58a2bf1716a7c5dd2bad68c15a7b04ba5a720f
46 changes: 30 additions & 16 deletions lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ function* fixTypeBased(fixer, node, props, context) {
}
)

const componentPropsTypeCode = `{ ${componentPropsTypes.join(PROPS_SEPARATOR)} }`
const componentPropsTypeCode = `{ ${componentPropsTypes.join(
PROPS_SEPARATOR
)} }`

// remove defineProps function parameters
yield fixer.replaceText(node.arguments[0], '')
Expand Down Expand Up @@ -96,22 +98,34 @@ const mapNativeType = (nativeType) => {
* @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
if (prop.type === 'array') {
if (prop.node.type !== 'Identifier') {
throw new Error(`Unexpected prop type inside array: ${prop.node.type}`)
}

return {
name: prop.node.name,
type: 'string',
required: false,
defaultValue: undefined
}
} else if (prop.type === 'object') {
if (prop.propName === null) {
throw new Error('Unexpected prop with null name.')
}

const type = optionGetType(prop.value, sourceCode)
const required = optionGetRequired(prop.value)
const defaultValue = optionGetDefault(prop.value)

return {
name: prop.propName,
type,
required,
defaultValue
}
}
throw new Error(`Unexpected prop type: ${prop.type}.`)
}

/**
Expand Down
28 changes: 24 additions & 4 deletions tests/lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ tester.run('define-props-declaration', rule, {
code: `
<script setup lang="ts">
interface Kind { id: number; name: string }

const props = defineProps({
kind: {
type: Object as PropType<Kind>,
Expand All @@ -376,7 +376,7 @@ tester.run('define-props-declaration', rule, {
output: `
<script setup lang="ts">
interface Kind { id: number; name: string }

const props = defineProps<{ kind?: Kind }>()
</script>
`,
Expand All @@ -393,7 +393,7 @@ tester.run('define-props-declaration', rule, {
code: `
<script setup lang="ts">
import Kind from 'test'

const props = defineProps({
kind: {
type: Object as PropType<Kind>,
Expand All @@ -404,7 +404,7 @@ tester.run('define-props-declaration', rule, {
output: `
<script setup lang="ts">
import Kind from 'test'

const props = defineProps<{ kind?: Kind }>()
</script>
`,
Expand Down Expand Up @@ -633,6 +633,26 @@ tester.run('define-props-declaration', rule, {
line: 3
}
]
},
// array
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps([kind])
Copy link
Member

Choose a reason for hiding this comment

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

The array elements must be strings. The kind variable shouldn't auto-fix because we don't know what's actually in it.
Could you leave this test as a test case for when not to auto-fix and add a new test case using a string literal?

https://vuejs.org/guide/components/props.html#props-declaration

Copy link
Author

Choose a reason for hiding this comment

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

My bad, you're right. It does not default to a string. Leaving it unhandled then.

Copy link
Member

Choose a reason for hiding this comment

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

Could you please add the test case you requested earlier?

 <script setup lang="ts">  const props = defineProps(['kind'])  </script>

#2466 (comment)

</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind?: string }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
}
]
})