Skip to content
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

feat(reactivity-transform): allow custom shorthands #5856

Closed
wants to merge 7 commits into from

Conversation

antfu
Copy link
Member

@antfu antfu commented May 4, 2022

Related issue in Nuxt 3:

Reactivity transform provides the awesome DX to work on reactive data at ease. While the built-in shorthands like $ref and $computed are quite convenient, when it comes to custom composable functions, the usage becomes a bit verbose like $(useMouse({ ... })). In high-level integrations like Nuxt or with vite plugins, we could provide auto import for common APIs and libraries with proper types, having custom shorthands $useMouse({ ... }) would improve the DX even further.

This PR enables the options to provide custom shorthands for the transformer with minimal features. It essential handles $foo() -> $(foo()) while leaving the complexity of helper imports and type generation to the uplevel integrations.

@netlify
Copy link

netlify bot commented May 4, 2022

Deploy Preview for vue-sfc-playground ready!

Name Link
🔨 Latest commit af488c2
🔍 Latest deploy log https://app.netlify.com/sites/vue-sfc-playground/deploys/6271d1f1b1f7fd000981876e
😎 Deploy Preview https://deploy-preview-5856--vue-sfc-playground.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 4, 2022

Deploy Preview for vue-next-template-explorer ready!

Name Link
🔨 Latest commit af488c2
🔍 Latest deploy log https://app.netlify.com/sites/vue-next-template-explorer/deploys/6271d1f1016f620008da4fe4
😎 Deploy Preview https://deploy-preview-5856--vue-next-template-explorer.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented May 4, 2022

Deploy Preview for vuejs-coverage failed.

Name Link
🔨 Latest commit 3a8cc1f
🔍 Latest deploy log https://app.netlify.com/sites/vuejs-coverage/deploys/627e14cc22423a00083d703f

Comment on lines +59 to +62
const shorthands = [...builtInShorthands, ...(options.shorthands || [])]
const transformCheckRE = new RegExp(
`[^\\w]\\$(?:\$|${shorthands.map(escapeRegExp).join('|')})?\\s*(\\(|\\<)`
)
Copy link
Member Author

Choose a reason for hiding this comment

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

change 1

Comment on lines +707 to +709
const { transform, transformAST, shouldTransform } =
/*#__PURE__*/ createReactivityTransformer()
export { transform, transformAST, shouldTransform }
Copy link
Member Author

Choose a reason for hiding this comment

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

change 3

To be compatible with the original top-level APIs

Comment on lines +742 to +744
function escapeRegExp(str: string) {
return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
}
Copy link
Member Author

Choose a reason for hiding this comment

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

change 4

Utils

? options.reactivityTransform
: undefined
)
: undefined
Copy link
Member Author

Choose a reason for hiding this comment

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

change 6

use the transformer instead of top-level apis, and allows reactivityTransform to be an option.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think the createReactivityTransformer implementation is necessary. I think the whole feature can simply be implemented by adding an extra argument to transformAST, (and as an extra option to the transform options object).

Comment on lines +317 to +337
// shorthands
const name = method.slice(1)
const isBuiltIn = builtInShorthands.includes(name)
if (id.type !== 'Identifier' && isBuiltIn) {
error(`${method}() cannot be used with destructure patterns.`, call)
} else {
// replace call
s.overwrite(
call.start! + offset,
call.start! + method.length + offset,
isBuiltIn ? helper(name) : name
)
if (id.type === 'Identifier') {
// single variable
registerRefBinding(id)
} else if (id.type === 'ObjectPattern') {
processRefObjectPattern(id, call)
} else if (id.type === 'ArrayPattern') {
processRefArrayPattern(id, call)
}
}
Copy link
Member Author

@antfu antfu May 4, 2022

Choose a reason for hiding this comment

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

change 2

treat differently for builtin shorthands and custom shorthands

? options.reactivityTransform
: undefined
)
: undefined
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the createReactivityTransformer implementation is necessary. I think the whole feature can simply be implemented by adding an extra argument to transformAST, (and as an extra option to the transform options object).

@antfu
Copy link
Member Author

antfu commented May 13, 2022

We built the regex here using the options

const shorthands = [...builtInShorthands, ...(options.shorthands || [])]
const transformCheckRE = new RegExp(
`[^\\w]\\$(?:\$|${shorthands.map(escapeRegExp).join('|')})?\\s*(\\(|\\<)`
)

which also affects how shouldTransform works.

I also exposed the top-level APIs to be compatible

const { transform, transformAST, shouldTransform } =
/*#__PURE__*/ createReactivityTransformer()
export { transform, transformAST, shouldTransform }

In the long term, if we want to have more options, I still think the create factory function is better for extensibility and performance.

@yyx990803
Copy link
Member

Ah I see, didn't notice the check regex is being dynamically generated now.

@yyx990803
Copy link
Member

There are a few conflicts with main, LGTM after resolve

@antfu
Copy link
Member Author

antfu commented May 13, 2022

Done

@antfu antfu requested a review from yyx990803 May 13, 2022 08:30
Comment on lines +321 to +322
error(`${method}() cannot be used with destructure patterns.`, call)
} else {
Copy link
Member

Choose a reason for hiding this comment

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

error will never return, we could remove the else branch.

@sxzz sxzz closed this Jan 25, 2023
@edison1105 edison1105 deleted the feat/reactiveity-transform-shorthands branch October 21, 2024 07:36
@edison1105 edison1105 restored the feat/reactiveity-transform-shorthands branch October 21, 2024 08:32
@edison1105 edison1105 reopened this Oct 21, 2024
@edison1105 edison1105 closed this Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants