Skip to content

Commit bbaabac

Browse files
author
uchida_t
committed
Argument is read-only when used default parameters.
jsx#327
1 parent a51cc85 commit bbaabac

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

src/analysis.jsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,13 @@ class ArgumentDeclaration extends LocalVariable {
324324
this._defaultValue = defaultValue;
325325
}
326326

327+
function constructor (name : Token, type : Type, isConst : boolean, defaultValue : Expression) {
328+
super(name, type, isConst);
329+
this._defaultValue = defaultValue;
330+
}
331+
327332
function clone () : ArgumentDeclaration {
328-
return new ArgumentDeclaration(this._name, this._type, Util.cloneNullable(this._defaultValue));
333+
return new ArgumentDeclaration(this._name, this._type, this._isConstant, Util.cloneNullable(this._defaultValue));
329334
}
330335

331336
function getDefaultValue() : Expression {
@@ -334,7 +339,7 @@ class ArgumentDeclaration extends LocalVariable {
334339

335340
override function _instantiate (instantiationContext : InstantiationContext) : ArgumentDeclaration {
336341
var type = this._type != null ? this._type.instantiate(instantiationContext, false) : null;
337-
return new ArgumentDeclaration(this._name, type, this._defaultValue);
342+
return new ArgumentDeclaration(this._name, type, this._isConstant, this._defaultValue);
338343
}
339344

340345
override function instantiateAndPush (instantiationContext : InstantiationContext) : ArgumentDeclaration {

src/classdef.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ class MemberFunctionDefinition extends MemberDefinition implements Block {
16981698
for (; origArgIndex != this.getArguments().length; ++origArgIndex) {
16991699
// build list of formal args (of the generated function)
17001700
var formalArgs = this.getArguments().slice(0, origArgIndex).map.<ArgumentDeclaration>((arg) -> {
1701-
return new ArgumentDeclaration(arg.getName(), arg.getType());
1701+
return new ArgumentDeclaration(arg.getName(), arg.getType(), true, null);
17021702
});
17031703
// build function body
17041704
var argExprs = formalArgs.map.<Expression>((arg) -> {

src/parser.jsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -3409,6 +3409,7 @@ class Parser {
34093409

34103410
function _functionArgumentsExpr (allowVarArgs : boolean, requireTypeDeclaration : boolean, allowDefaultValues : boolean) : ArgumentDeclaration[] {
34113411
var args = new ArgumentDeclaration[];
3412+
var hasDefaultValue = false;
34123413
if (this._expectOpt(")") == null) {
34133414
var token = null : Token;
34143415
do {
@@ -3438,7 +3439,7 @@ class Parser {
34383439
// vararg is the last argument
34393440
if (argType == null && isVarArg)
34403441
throw new Error("not yet implemented!");
3441-
args.push(new ArgumentDeclaration(argName, new VariableLengthArgumentType(argType)));
3442+
args.push(new ArgumentDeclaration(argName, new VariableLengthArgumentType(argType), hasDefaultValue, null));
34423443
if (this._expect(")") == null)
34433444
return null;
34443445
break;
@@ -3447,6 +3448,12 @@ class Parser {
34473448
var assignToken = this._expectOpt("=");
34483449
if (assignToken != null) {
34493450
var state = this._preserveState();
3451+
if (!hasDefaultValue) {
3452+
args = args.map.<ArgumentDeclaration>((arg) -> {
3453+
return new ArgumentDeclaration(arg.getName(), arg.getType(), true, arg.getDefaultValue());
3454+
});
3455+
hasDefaultValue = true;
3456+
}
34503457
this._pushScope(null, args);
34513458
try {
34523459
if ((defaultValue = this._assignExpr(true)) == null) {
@@ -3469,7 +3476,7 @@ class Parser {
34693476
return null;
34703477
}
34713478
}
3472-
args.push(new ArgumentDeclaration(argName, argType, defaultValue));
3479+
args.push(new ArgumentDeclaration(argName, argType, hasDefaultValue, defaultValue));
34733480
token = this._expect([ ")", "," ]);
34743481
if (token == null)
34753482
return null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class C {
2+
3+
function fn(a : number, b : number = 10) {
4+
a = 11;
5+
}
6+
7+
}

0 commit comments

Comments
 (0)