Code Monkey home page Code Monkey logo

babel-plugin-contracts's People

Contributors

emiltholin avatar phpnode avatar sheepsteak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

babel-plugin-contracts's Issues

precondition failed but not sure why

Hey there, I'm trying out this lib, and I have the following function:

export const fib = (n) => {
  pre: {
    Number.isInteger(n);
    n > 0;
  }

  if (n === 0) {
    return 1;
  } else {
    return n + fib(n - 1);
  }

  post: {
    Number.isInteger(it);
    it > 0;
  }
};

when I run fib(5) I get Error: Function precondition failed: n > 0

This shouldn't happen right?

Whole testbed is here: https://github.com/sdellysse/contract-test-n-zero

Suggestion: Note migration requirements from contractual to babel-plugin-contracts in readme

After some head scratching over an error about length being undefined, I realized you added a body around the pre label and the main label was gone. It might not be a bad idea to add a migration section to your readme about these changes as they don't jump out that it changed.

The error I was getting was the following:

Module build failed: TypeError: /electron/src/main.js: Cannot read property 'length' of undefined
    at LabeledStatement (/electron/node_modules/babel-plugin-contracts/lib/index.js:775:41)

Unexpected token (2:9)

Hello,

I used to use contracts in Racket of all languages, so glad to see something similar exists for JS!

Anyway, every time I try to compile anything with the contracts plugin, I get Unexpected token (2:9). It doesn't matter what is at position 2:9, even comments will trigger the same error.

.babelrc

{
  "plugins": [
    ["contracts", {
      "names": {
        "assert": "assert",
        "precondition": "pre",
        "postcondition": "post",
        "invariant": "invariant",
        "return": "it",
        "old": "old"
      }
    }]
  ]
}

test.js

const add1 = n => {
	pre: typeof n === 'number';
	return n + 1
}

babel --version

6.26.0 (babel-core 6.26.3)

Do I need babel 7 for this? 6.26.3 is the latest version in npm.

https://www.npmjs.com/package/babel-core

special variable name

Post-conditions introduce a special variable, it which refers to the result of the function.

Could you add a property—like env—that would let us use a custom variable name of our choosing?
cf

const RETURN_NAME = 'it';

e.g.

{
  "pre": ""
  "post": ""
  "invariant": "",
  "it": ""
}

Error: Unknown substitution "condition" given

Is this plugin compatible with babel 7?

relevant excerpt of the stack strace:

…node_modules/babel-plugin-contracts/lib/index.js:37:15
…node_modules/@babel/template/lib/populate.js:35:15

Class invariants and rescue clauses

First, thanks so much for a fantastic library!

I was an Eiffel programmer once upon a time and miss DbC. Babel Contracts is the closest I've seen to Eiffel's implementation, and the smoothest. I have already started adding contracts to my code.

Two additions to the library would be most welcome:

  • Class invariants
  • rescue (as Eiffel would call it)

For the former, imagine this: We place an invariant structure on the constructor of a class, and that invariant is then automatically applied to the beginning and end of each method call (although I admit that I'm not sure what to do about getters and setters). The invariant would not be applied to the constructor, of course, as the instance is still developing.

As for the latter, a rescue clause would allow us to do the following:

  • Restore the method to a stable state
  • Intercept the exception Contracts throws when the contract is violated, and then re-throw it, most likely with a new Error (and the violation as an inner error)
  • Introduce a violation() method for capturing the contract exception thrown by Contracts

Consider the following trivial example:

setStatus(status) {
   require: {
      status;
      typeof status === 'string';
   }

   this.status = status;

   ensure: {
      this.status === 'status';
   }

   rescue: {
      throw new Error('Status was not set', violation());
   }
}

Do you think these things are doable?

Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.