codalien / operator-overloading-js

Simple Operator overloading library for JS.
160 stars 26 forks source link

Arrow functions aren't supported #24

Open roman-koshchei opened 1 year ago

roman-koshchei commented 1 year ago

condition is arrow function for comfortable usage, but it doesn't work

type Where = (over: Over) => A
function where(condition: Where) {
  const o = new Over()

  const overloadedFunction = overload(condition)
  return overloadedFunction(o, condition)
}

const data = where(p => p.a1 || p.a2)

image

kungfooman commented 1 year ago

First of all, mixing JavaScript with type comments ("TypeScript") is ugly and you are better off writing JSDoc:

/**
 * @callback Where
 * @param {Over} over
 * @returns {A}
 */

/**
 * @param {Where} condition - TODO: Explain what this is...
 */
function where(condition) {
  const o = new Over();
  const overloadedFunction = overload(condition);
  return overloadedFunction(o, condition);
}
const data = where(p => p.a1 || p.a2);

Secondly, esprima@1 doesn't support ES6, you need to install esprima@2:

npm i esprima@2

Then it should work...

aliabolhassani commented 1 month ago

Still does not work, even after installing esprima@2

` function toUpper(str) { return str.toUpperCase(); }

function reverse(str) { return str.split("").reverse().join(""); } const text = () => "ali"; // const text = "ali"; // this will work

var result = text() | toUpper | reverse; `

Error: Line 11: Unexpected token ) at throwError (C:...\node_modules\operator-overloading\node_modules\esprima\esprima.js:1846:21) at throwUnexpected (C:...\node_modules\operator-overloading\node_modules\esprima\esprima.js:1904:9) at parsePrimaryExpression (C:...\node_modules\operator-overloading\node_modules\esprima\esprima.js:2218:13) at parseLeftHandSideExpressionAllowCall (C:...\node_modules\operator-overloading\node_modules\esprima\esprima.js:2293:61) at parsePostfixExpression (C:...\node_modules\operator-overloading\node_modules\esprima\esprima.js:2341:16) at parseUnaryExpression (C:...\node_modules\operator-overloading\node_modules\esprima\esprima.js:2400:20) at parseBinaryExpression (C:...\node_modules\operator-overloading\node_modules\esprima\esprima.js:2489:16) at parseConditionalExpression (C:...\node_modules\operator-overloading\node_modules\esprima\esprima.js:2549:16) at parseAssignmentExpression (C:...\node_modules\operator-overloading\node_modules\esprima\esprima.js:2575:23) at parseExpression (C:...\node_modules\operator-overloading\node_modules\esprima\esprima.js:2601:16) { index: 234, lineNumber: 11, column: 19, description: 'Unexpected token )' }

kungfooman commented 1 month ago

@aliabolhassani It clearly does work, but something is fishy in your setup:

const esprima = require('esprima');
const program = `
function toUpper(str) {
  return str.toUpperCase();
}
function reverse(str) {
  return str.split("").reverse().join("");
}
const text = () => "ali";
// const text = "ali"; // this will work
var result = text() | toUpper | reverse;
`;
const tokens = esprima.tokenize(program);
console.log("tokens", tokens);
const ast = esprima.parse(program);
console.log("ast", ast);

Output:

// ... tokens ...
ast {
  type: 'Program',
  body: [
    {
      type: 'FunctionDeclaration',
      id: [Object],
      params: [Array],
      defaults: [],
      body: [Object],
      generator: false,
      expression: false
    },
    {
      type: 'FunctionDeclaration',
      id: [Object],
      params: [Array],
      defaults: [],
      body: [Object],
      generator: false,
      expression: false
    },
    {
      type: 'VariableDeclaration',
      declarations: [Array],
      kind: 'const'
    },
    { type: 'VariableDeclaration', declarations: [Array], kind: 'var' }
  ],
  sourceType: 'script'
}

Try running: npx esparse -v

ECMAScript Parser (using Esprima version 2.7.3 )