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

Typescript this parameters don't work #236

Closed
ashrafsabrym opened this issue Feb 18, 2019 · 7 comments · Fixed by #237
Closed

Typescript this parameters don't work #236

ashrafsabrym opened this issue Feb 18, 2019 · 7 comments · Fixed by #237
Assignees
Labels
Milestone

Comments

@ashrafsabrym
Copy link

I'm trying to compile a function with this parameters. Part of my code is:

export function DirectiveIserviceIdPrefixController(this: any, $scope: angular.IScope, $attrs: angular.IAttributes)
{
  this.$scope = $scope;
  this.$attrs = $attrs;
}

And I get the error:
Module parse failed: Unexpected keyword 'this' (1:52)

Here's my Webpack configuration for swc:

{
          test: /\.ts$/,
          exclude: [/node_modules/, /wwwroot/, /packages/],
          use: {
            loader: 'swc-loader',
            options: {
              jsc: {
                parser: {
                  syntax: 'typescript'
                }
              }
            }
          }
        }

The code compiles successfully with Babel, but with swc, I have to remove the this parameter and add the noImplicitThis: false configuration to tsconfig for my IDE to ignore it.

@kdy1 kdy1 added this to the v1.0.19 milestone Feb 19, 2019
kdy1 added a commit to kdy1/swc-example that referenced this issue Feb 19, 2019
kdy1 added a commit to kdy1/swc that referenced this issue Feb 19, 2019
@kdy1
Copy link
Member

kdy1 commented Feb 19, 2019

@kdy1 kdy1 mentioned this issue Feb 19, 2019
@kdy1 kdy1 closed this as completed in #237 Feb 19, 2019
@ashrafsabrym
Copy link
Author

I certify that the issue occurs when using the swc-loader as part of Webpack, but the same code compiles successfully if swc-cli is used.

Please view this repository. It isolates the issue.
Running npm run build:dev will cause the mentioned issue, while npx swc ./directives/directive.ts -d ./ compiles the file successfully.

@kdy1 kdy1 reopened this Feb 19, 2019
@kdy1 kdy1 removed this from the v1.0.20 milestone Feb 19, 2019
@kdy1
Copy link
Member

kdy1 commented Feb 19, 2019

I found the issue.
swc transcompiles it to

export function DirectiveController(this, $scope, $attrs) {
    this['$scope'] = $scope;
    this['$attrs'] = $attrs;
}

and webpack cannot understand this syntax.

So I guess swc should remove this parameter.

@kdy1 kdy1 added the C-bug label Feb 19, 2019
@kdy1 kdy1 added this to the v1.0.21 milestone Feb 19, 2019
@kdy1 kdy1 closed this as completed in 4fddea0 Feb 19, 2019
@ashrafsabrym
Copy link
Author

Yes, I see.
That's why in Typescript documentation they call them fake parameters:

this parameters are fake parameters that come first in the parameter list of a function

@kdy1
Copy link
Member

kdy1 commented Feb 19, 2019

As this is a bug fix, I'll publish 1.0.21 soon.

kdy1 added a commit to swc-project/node-swc that referenced this issue Feb 19, 2019
@kdy1
Copy link
Member

kdy1 commented Feb 19, 2019

Fixed with @swc/[email protected]

@kdy1 kdy1 self-assigned this Feb 25, 2019
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 30, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants