Skip to content

Conversation

@Z3rio
Copy link
Contributor

@Z3rio Z3rio commented Oct 21, 2023

This PR aims to resolve the issue addressed in issue #1387

Also went ahead and made the isContextualCallExpression function more readable, as the previous one was a bit "hacky" in my opinion

Code to reproduce the issue:

/** @noSelfInFile **/

const funcByValue: Record<string, Function> = {
  hello: () => 1
};

const func = funcByValue["hello"];
func(1);

const funcByValue2: Record<string, (...args: any[]) => any> = {
  hello: () => 1
};

const func2 = funcByValue2["hello"];
func2(1);

You will notice that the first function in the outputted code will include the self parameter, and the second one won't include it. Whereas neither one should have the self parameter, as the file has noSelfInFile

Example output before changes

--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
---
-- @noSelfInFile *
funcByValue = {hello = function() return 1 end}
func = funcByValue.hello
func(_G, 1)
funcByValue2 = {hello = function() return 1 end}
func2 = funcByValue2.hello
func2(1)

https://typescripttolua.github.io/play/#code/PQKhAIAEDsHsGUCmAbAZgSWgMQJbMeGMAFDEDGs0AzgC7ioCu0ZAQgJ4BqAhsg4gFzgASogoAnACYAeWmJzQA5gBpwWJmRo5KAPnABecAG9i4U+ABEACxTJY5wQAoAlPt0BGJcQC+AblIVqOkZmfXp1dm5eRABtKxs7AF0-YLIHNyd-Slow5giePgAmQRFxaVl5ZXAHADparjEFKkEuaDZohJc9XRa2XQNjMwtrZFt7Ks73Ty9MwJyyAtCUvKiC2OHRpOIUgrSMoA

@Z3rio
Copy link
Contributor Author

Z3rio commented Oct 21, 2023

Yeah, this perhaps does not seem to be working as it should (considering the failed tests)

Copy link
Member

@Perryvw Perryvw left a comment

Choose a reason for hiding this comment

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

Missing a test verifying this change actually does what is advertised

@Z3rio
Copy link
Contributor Author

Z3rio commented Nov 19, 2023

I've added the requested test, although there currently seems to be some issues with the current solution, when importing functions from other files with noSelfInFile, as it uses the noSelfInFile from the calling file, and not the declaration file.

@Perryvw
Copy link
Member

Perryvw commented Nov 19, 2023

I've added the requested test, although there currently seems to be some issues with the current solution, when importing functions from other files with noSelfInFile, as it uses the noSelfInFile from the calling file, and not the declaration file.

Oh I was just expecting a more complicated version of the test you already had with testFunction and addExtraFile, we try to avoid actual file system operations if possible. If you need help getting that setup contact me on discord.

@Z3rio
Copy link
Contributor Author

Z3rio commented Nov 19, 2023

I've added the requested test, although there currently seems to be some issues with the current solution, when importing functions from other files with noSelfInFile, as it uses the noSelfInFile from the calling file, and not the declaration file.

Oh I was just expecting a more complicated version of the test you already had with testFunction and addExtraFile, we try to avoid actual file system operations if possible. If you need help getting that setup contact me on discord.

Ah, yeah, I could do that, didn't know that was possible actually.
I've written to you on discord concerning a bigger issue though :)

Perryvw added a commit that referenced this pull request Nov 20, 2023
* Cleanup code / make more readable

* Further readability fix

* Fix noSelfInFile check for call visitor

* Fix/further rewrite of `isContextualCallExpression`

* Further readability improvements

* Change back returns in `isContextualCallExpression`

* Add testcase for noSelfInFile over noImplicitSelf

* Simplify testcase

* Improve NoSelfInFile check

* Further hasnoselfinfile fix

* wrap declaration statement again

* Add noSelfInFile check for func declared in other file

* Remove accidentally commited debug logs

* Rework function call context checking

* Add back some caching that was removed in last commit

---------

Co-authored-by: Zerio <neoboij@gmail.com>
@Perryvw
Copy link
Member

Perryvw commented Nov 20, 2023

Closing this as superseded by #1519

@Perryvw Perryvw closed this Nov 20, 2023
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.

2 participants