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

Cannot set property $inject of function SomeController which has only a getter #53

Open
kostetskyroma opened this issue Oct 23, 2019 · 6 comments

Comments

@kostetskyroma
Copy link

kostetskyroma commented Oct 23, 2019

Hi everyone, I've faced with the issue when using babel-plugin-angularjs-annotate plugin v.0.10.0.
I've received an error:

**Console**
Uncaught TypeError: Cannot set property $inject of function SomeController($scope, $state) {
...<omitted>... } which has only a getter

SomeController.js

const module = angular.module('module1', []);
export default class SomeController {
  static get $inject() {
    return ['$scope', '$state'];
  }
  constructor($scope, $state) {
    this.$scope = $scope;
    this.$state = $state;
}
module.controller('SomeController', SomeController);

.babelrc

{
  "presets": ["@babel/preset-env"],
  "plugins": ["@babel/plugin-proposal-class-properties", "angularjs-annotate"]
}

What is wrong in my config?

@kostetskyroma
Copy link
Author

kostetskyroma commented Oct 23, 2019

it seems that babel-plugin-angularjs-annotate doesn't support this syntax.

image

@schmod can you confirm it?

@zleight1
Copy link

zleight1 commented Feb 2, 2020

I was able to fix this by having babel transpile to a lower target.

@timofei-iatsenko
Copy link

The problem in your case that you have already defined $inject property in your class. And it's defined as a getter without setter and treat as read-only.
Plugin during its work annotate this class second time and try to assign variable into your getter, and this causes an error.

You have a few options here:

  1. Disable implicit mode:
  "plugins": [["angularjs-annotate", { "explicitOnly" : true}]]

This way only explicitly annotated classes will be processed check docs: https://www.npmjs.com/package/babel-plugin-angularjs-annotate#explicitonly

Because your class doesn't have explicit annotation it will not be processed.

  1. Remove $inject getter from your class. The purpose of this plugin is automatic annotating. You can safely remove this property and let the plugin do that job for you.

  2. Change $inject getter to a property. This will not fix the root cause, class will still be annotated twice, but omit the error because re-assigning property not causing an error.

I was able to fix this by having babel transpile to a lower target.

Traspiling to lower target also just "mask" the issue, during transpiling to lower target your getters/setters get transpiled in the way which not causing error during re-assigning read-only property in run-time.

@StefanVlad
Copy link

Hey @zleight1

I am having the same problems so I was wondering what was the target that you set for babel to transpile the code in?

Thanks in advance!

@zleight1
Copy link

Hey @zleight1

I am having the same problems so I was wondering what was the target that you set for babel to transpile the code in?

Thanks in advance!

I'm using preset-env and have this browserslist

[production]
> 0.5%
chrome >= 70
edge >= 90
firefox >= 70
safari >= 13
ios_saf >= 11.1
not dead
not op_mini all
ie 11

@StefanVlad
Copy link

@zleight1 Thank you for the reply. Unfortunately, it did not solve my issue but I will keep trying. Much appreciated. Cheers!

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

No branches or pull requests

4 participants