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)

handle PropTypes (e.g. String as PropType<'test'>)
  • Loading branch information
mpiniarski committed May 27, 2024
commit b770232a69f3039af2ddf189261b9fc29e92a15c
18 changes: 18 additions & 0 deletions lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
@@ -61,6 +61,7 @@ module.exports = {
},
/** @param {RuleContext} context */
create(context) {
const sourceCode = context.getSourceCode()
/**
* @param {Expression} node
* @returns {string | null}
@@ -76,6 +77,23 @@ module.exports = {
if (typeProperty == null) {
return null
}
if (typeProperty.value.type === 'TSAsExpression') {
if (
typeProperty.value.typeAnnotation.typeName.name !== 'PropType'
) {
return null
}

const typeArgument =
typeProperty.value.typeAnnotation.typeArguments.params[0]
if (typeArgument === undefined) {
return null
}

const text = sourceCode.getText(typeArgument)

return text
}
return optionGetType(typeProperty.value)
}
case 'ArrayExpression': {
98 changes: 98 additions & 0 deletions tests/lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
@@ -288,6 +288,104 @@ tester.run('define-props-declaration', rule, {
line: 3
}
]
},
// Native Type with PropType
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps({
kind: {
type: String as PropType<'a' | 'b'>,
}
})
</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind: 'a' | 'b' }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
},
// Object with PropType
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps({
kind: {
type: Object as PropType<{ id: number; name: string }>,
}
})
</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind: { id: number; name: string } }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
},
// Array with PropType
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps({
kind: {
type: Array as PropType<string[]>,
default: () => []
}
})
</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind: string[] }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
},
// Function with PropType
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps({
kind: {
type: Function as PropType<(a: number, b: string) => boolean>,
required: true
}
})
</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind: (a: number, b: string) => boolean }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
}
]
})