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

Error: Unexpected! Inserting syntax kind of PropertyDeclaration, but Parameter was inserted. #57

Closed
gp187 opened this issue Aug 15, 2017 · 4 comments
Labels

Comments

@gp187
Copy link

gp187 commented Aug 15, 2017

The simplest

const property = classDeclaration.addProperty({ isStatic: true, name: "prop", type: "string" });

triggers this on latest version when ever a non-empty constructor is present

constructor( private serv: MyLocalService ) {}
if no constructor then it works.

ERROR >  Error: Unexpected! Inserting syntax kind of PropertyDeclaration, but Parameter was inserted.
    at Object.getRangeFromArray (/lab/o0/cli2/node_modules/ts-simple-ast/dist/manipulation/getRangeFromArray.js:11:19)
    at Object.fillAndGetChildren (/lab/o0/cli2/node_modules/ts-simple-ast/dist/manipulation/fillAndGetChildren.js:5:42)
    at Object.insertIntoBracesOrSourceFileWithFillAndGetChildren (/lab/o0/cli2/node_modules/ts-simple-ast/dist/manipulation/insertIntoBracesOrSourceFileWithFillAndGetChildren.js:16:33)
    at ClassDeclaration.insertProperties (/lab/o0/cli2/node_modules/ts-simple-ast/dist/compiler/class/ClassDeclaration.js:167:31)
    at ClassDeclaration.addProperties (/lab/o0/cli2/node_modules/ts-simple-ast/dist/compiler/class/ClassDeclaration.js:136:21)
    at as (/lab/o0/cli2/dist/index.js:283:13)
    at Object.<anonymous> (/lab/o0/cli2/dist/index.js:286:1)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3
@gp187
Copy link
Author

gp187 commented Aug 15, 2017

Also, the variables get declared after the constructor. Wouldn't it be better before it?

@dsherret
Copy link
Owner

dsherret commented Aug 15, 2017

Thanks, I'm able to reproduce this and I've fixed the problem. Sorry about that! It was related to a change in behaviour I made in #39.

When fixing this, it also made me notice an issue related to inserting. I'll fix that tonight as part of this and then do a new patch release.

#26 will deal with member ordering when calling an add method. It would be nice if people could configure the member order rules they want.

@gp187
Copy link
Author

gp187 commented Aug 15, 2017

Also, something helpful, I cannot recover properties in an existing constructor statement.
I may be wrong, but I tried everything available.

If you want to modify an existing constructor, you can't. Only remove and add a new one.
That may be problematic and I see the structures there to fix, but I'm missing something....

dsherret added a commit that referenced this issue Aug 16, 2017
… exists overloads.

Also changed Node.getAllChildren() to Node.getDescendants().
@dsherret
Copy link
Owner

dsherret commented Aug 16, 2017

@gp187 this is fixed in 0.49.0 so I'm going to close this. Let me know if you encounter any issues.

--

You should be able to get the parameters properties from a constructor by filtering the parameters for ones that are parameter properties:

const ctorProps = ctor.getParameters().filter(p => p.isParameterProperty())
// p.isParameterProperty() == p.getScope() != null || p.isReadonly()

Is that what you mean? Otherwise, from a class declaration you can get them by calling:

const ctorProps = classDeclaration.getInstanceProperties()
    .filter(p => p instanceof ParameterDeclaration) as ParameterDeclaration[];

--

Unfortunately, right now removing nodes isn't possible. Removing a constructor is a bit of an exception. I'm working on that slowly in #47 (it's the highest priority feature right now)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants