z-pattern-matching / z

Pattern Matching for Javascript
https://z-pattern-matching.github.io/
Apache License 2.0
1.72k stars 50 forks source link

Function reflector does not handle the case of using object-based value #44

Closed ssut closed 6 years ago

ssut commented 6 years ago

I have code like:

types.js

const Types = Object.freeze({
  A: 'a',
  B: 'b',
});

module.exports = Object.freeze({
  ...Types,
  Types: Object.values(Types),
});

index.js

const Types = require('./types');

const { matches } = require('z');

const match = matches('a')(
  (x = Types.A) => 'matched A',
  (x = Types.B) => 'matched B',
);

console.info(match);

and the expected behavior is to just work as the x is assigned by constant, but it seems not so just throwing an TypeError which looks like:

TypeError: Cannot read property 'A' of undefined
    at eval (eval at Parser.pushBuffer (C:\Users\suhun\Downloads\z-test\node_modules\js-function-reflector\argument_parser.js:96:22), <anonymous>:1:13)
    at Parser.pushBuffer (C:\Users\suhun\Downloads\z-test\node_modules\js-function-reflector\argument_parser.js:96:22)
    at Parser.parse (C:\Users\suhun\Downloads\z-test\node_modules\js-function-reflector\argument_parser.js:67:14)
    at module.exports (C:\Users\suhun\Downloads\z-test\node_modules\js-function-reflector\header_parser.js:10:21)
    at reflector (C:\Users\suhun\Downloads\z-test\node_modules\js-function-reflector\index.js:34:30)
    at module.exports (C:\Users\suhun\Downloads\z-test\node_modules\z\src\getMatchDetails.js:4:29)
    at resolveMatchFunctions (C:\Users\suhun\Downloads\z-test\node_modules\z\src\z.js:9:26)
    at functions (C:\Users\suhun\Downloads\z-test\node_modules\z\src\z.js:44:3)
    at Object.<anonymous> (C:\Users\suhun\Downloads\z-test\index.js:5:27)
    at Module._compile (internal/modules/cjs/loader.js:654:30)

I believe this should be filed in the js-function-reflector but I do it here because z has a dependency on it, which appears to be maintained by you.

leonardiwagner commented 6 years ago

Hi @ssut , it's there 2 problems:

const match = matches('a').call({ Types },
  (x = Types.A) => 'matched A',
  (x = Types.B) => 'matched B',
);
leonardiwagner commented 6 years ago

Hey @ssut , fixed that on z@1.0.8, please ensure you are using this newer version and passing Types on .call method:

const match = matches('a').call({ Types },
  (x = Types.A) => 'matched A',
  (x = Types.B) => 'matched B',
);

Please report if it's everything working now, thanks!