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: base addition for context handling #8

Merged
merged 8 commits into from
Sep 4, 2021

Conversation

barelyhuman
Copy link
Collaborator

@barelyhuman barelyhuman commented Aug 21, 2021

pmndrs/valtio#204 (comment)

Note: Needs additional test cases and checks , this is just a base commit for the feature, not the completed version.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 21, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit baf1af6:

Sandbox Source
Vanilla Configuration
React Configuration

@dai-shi
Copy link
Member

dai-shi commented Aug 27, 2021

some ideas for the complete version.

// invalid
const state = proxy({
  count: 0,
  inc() {
    ++this.count
  },
})

// valid
const state = proxy({
  count: 0,
  inc() {
    ++state.count
  },
})

// valid
const state = proxy({
  count: 0,
  inc: function () {
    ++state.count
  },
})

// valid
const state = proxy({
  count: 0,
  inc: () => {
    ++state.count
  },
})

// invalid
const state = proxy({
  count: 0,
  inc: function () {
    ++this.count
  },
})

// invalid (technically possible though)
const state = proxy({
  count: 0,
  inc: () => {
    ++this.count
  },
})

// valid
const state = proxy({
  arr: [],
  inc: () => {
    state.arr = [1, 2, 3].map(function(x) { return x / this }, 10)
  },
})

// invalid
const state = proxy({
  count: [],
  inc() {
    (() => { ++this.count })()
  },
})

Basically, if this is pointing to state or developers mean it, we detect it as invalid. Otherwise, valid.

@dai-shi
Copy link
Member

dai-shi commented Aug 27, 2021

On second thought, we don't need to take care of those edge cases. We can just warn this usage in proxy regardless of the usage. After all, it's just a warning for beginners.

@barelyhuman
Copy link
Collaborator Author

we don't need to take care of those edge cases

don't plan to either, but these are just for the tests so that we can be assured that beginners are warned that something is going to be wrong aka, more for our confirmation than for the users

Also,
Avoid using 'this' in valtio.proxy context. It might lead to unexpected results
Is this okay for a message? I think we'd need a little more clarity added to the message, let me know

@dai-shi
Copy link
Member

dai-shi commented Aug 27, 2021

If possible, we want to detect this, but maybe it's hard and not possible across modules?

// invalid
const initialObj = {
  count: 0,
  inc() {
    ++this.count
  },
}

const state = proxy(initialObj)

@dai-shi
Copy link
Member

dai-shi commented Aug 27, 2021

Avoid using 'this' in valtio.proxy context. It might lead to unexpected results
Is this okay for a message? I think we'd need a little more clarity added to the message, let me know

I think it's good. To add a note, we can make sure that the rule is optional.
Something like "using this is valid, but often a pitfall for beginners"

@barelyhuman
Copy link
Collaborator Author

If possible, we want to detect this, but maybe it's hard and not possible across modules?

// invalid
const initialObj = {
  count: 0,
  inc() {
    ++this.count
  },
}

const state = proxy(initialObj)

Hmm, I'll have to add in a few more handlers to see how I can handle that, it should be possible but then I guess the eslint error would show on the proxy(initialObj) line, which can be confusing, I'll try to replicate that

To add a note, we can make sure that the rule is optional.

They can turn it off from the rules , and yes i'll add "using this is valid, but often a pitfall for beginners" to the message

Though, I think people turning the rule off will probably know what it's going to do so makes sense to keep it in the recommended rules

handles if the this context is used inside another call context
instead of proxy

extra: also adds comments and `nearestCalleeName` in utils
@barelyhuman
Copy link
Collaborator Author

@dai-shi I have a version that detects this

// invalid
const initialObj = {
  count: 0,
  inc() {
    ++this.count
  },
}

const state = proxy(initialObj)

though it's the hackiest solution I've ever written, so I'm testing it with other common setups so it doesn't collide with them.
I'll let you know either way.

@barelyhuman
Copy link
Collaborator Author

@dai-shi want me to set it as ready for review or is there another case we need to check, let me know

@dai-shi
Copy link
Member

dai-shi commented Sep 3, 2021

If you don't have any concerns, it means ready!

@barelyhuman barelyhuman marked this pull request as ready for review September 4, 2021 05:49
@barelyhuman
Copy link
Collaborator Author

Where the warning will be shown in both cases.

Screenshot 2021-09-04 at 11 20 31 AM

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

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

LGTM. Nice work.

src/AvoidThisInProxy.js Outdated Show resolved Hide resolved
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
Copy link
Member

@Aslemammad Aslemammad left a comment

Choose a reason for hiding this comment

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

That's a clean & valid code! Thank you for your efforts.

@Aslemammad Aslemammad merged commit 70426a0 into pmndrs:master Sep 4, 2021
@dai-shi dai-shi mentioned this pull request Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants