codemix / babel-plugin-contracts Goto Github PK
View Code? Open in Web Editor NEWDesign by Contract for JavaScript via a Babel plugin.
License: MIT License
Design by Contract for JavaScript via a Babel plugin.
License: MIT License
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
Hello,
Thank you for this DBC library for JS.
I am encountering an issue with Webpack and Babel-loader plugin, where I set options to change the var names (for example "result" instead of "it"), but those are not used, and the default var names are used instead.
I have included a (basic) testcase.
Regards.
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)
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.
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
babel-plugin-contracts/src/index.js
Line 50 in feb3422
e.g.
{
"pre": ""
"post": ""
"invariant": "",
"it": ""
}
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
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:
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:
violation()
method for capturing the contract exception thrown by ContractsConsider 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!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.