codemix / babel-plugin-typecheck

Static and runtime type checking for JavaScript in the form of a Babel plugin.
MIT License
886 stars 44 forks source link

Invalid Syntax When using Default Values for Paramters #15

Closed killercup closed 9 years ago

killercup commented 9 years ago

I guess I just found a weird corner case. Using default parameter values with types generates invalid syntax.

function lol(settings={}: Object) {}
lol({x: 1})

becomes:

"use strict";

function lol() {
  var settings = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  if (typeof settings = {} !== "object") throw new TypeError("Value of argument 'undefined' violates contract, expected object got " + (settings = {} === null ? "null" : settings = {} instanceof Object && settings = {}.constructor ? settings = {}.constructor.name : typeof settings = {}));
}
lol({ x: 1 });

which doesn't work because:

/…/stack.es5.js:5
  if (typeof settings = {} !== "object") throw new TypeError("Value of argumen
  ^
ReferenceError: Invalid left-hand side in assignment
    at lol (/…/stack.es5.js:5:3)
    at Object.<anonymous> (/…/stack.es5.js:7:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:935:3
phpnode commented 9 years ago

hmm that's weird, I've actually not seen that syntax, I've been using:

function lol (settings: Object = {}) {}
killercup commented 9 years ago

Interesting. For some reason that syntax even looks a bit better than my version. (It's still odd that they both seem to work.) I'll just use your variant, then!

Charles Pick notifications@github.com schrieb am So., 23. Aug. 2015 um 23:18:

hmm that's weird, I've actually not seen that syntax, I've been using:

function lol (settings: Object = {}) {}

— Reply to this email directly or view it on GitHub https://github.com/codemix/babel-plugin-typecheck/issues/15#issuecomment-133938835 .