"You haven't a real appreciation of Newspeak, Winston," he said almost sadly. "Even when you write it you're still thinking in Oldspeak. I've read some of those pieces that you write in The Times occasionally. They're good enough, but they're translations. In your heart you'd prefer to stick to Oldspeak, with all its vagueness and its useless shades of meaning. You don't grasp the beauty of the destruction of words. Do you know that Newspeak is the only language in the world whose vocabulary gets smaller every year?"
(c) “1984”, George Orwell
JavaScript with no limits 🤫 with built-in JSX and TypeScript. Language ruled by the users, create an issue with ideas of a new language construction and what is look like in JavaScript, and most likely we implement it :).
npm i goldstein esbuild -g
$ cat > 1.gs
export fn hello() {
return 'world';
}
$ gs 1.gs
$ cat 1.js
function hello() {
return "world";
}
export {
hello,
};
Let's do a bit more!
const a = () => throw 'hello';
if a > 2 {
log('hello');
}
Will give us:
const a = () => {
throw 'hello';
};
if (a > 2) {
log('hello');
}
When you need to compile Goldstein to JavaScript use:
import {compile} from 'goldstein';
compile(`
fn hello() {
guard text !== "world" else {
return ""
}
return "Hello " + text
}
`);
// returns
`
function hello() {
if (!(text !== 'world')) {
return '';
}
return 'Hello ' + text;
}
`;
By default, all keywords mentioned in the next section used, but you can limit the list setting with keywords
option.
You can add any keywords, and even create your own:
import {compile, keywords} from 'goldstein';
const source = `
fn hello() {
return id('hello');
}
`;
const {keywordFn} = keywords;
compile(source, {
keywords: {
...keywords,
keywordFn: null,
keywordId(Parser) {
const {keywordTypes} = Parser.acorn;
return class extends Parser {};
},
},
rules: {
declare: ['on', {
declarations: {
id: 'const id = (a) => a',
},
}],
},
});
// returns
`
const id = (a) => a;
function hello() {
return id('hello');
}
`;
You can declare variables with @putout/operator-declare
.
When you need to get JavaScript Babel AST use parse
:
import {parse} from 'goldstein';
parse(`
fn hello() {
guard text !== "world" else {
return ""
}
return "Hello " + text
}
`);
// returns Babel AST
You can parse to ESTree:
const options = {
type: 'estree',
};
parse(`
fn hello() {
guard text !== "world" else {
return ""
}
return "Hello " + text
`, options);
You can make any modifications to Goldstein AST and then print
back to Goldstein:
import {parse, print} from 'goldstein';
const ast = parse(`const t = try f('hello')`);
const source = print(ast);
You can even convert JavaScript to Goldstein with:
import {convert} from 'goldstein';
const ast = convert(`const t = tryCatch(f, 'hello')`);
// returns
`const t = try f('hello')`;
Goldstein is absolutely compatible with JavaScript, and it has extensions. Here is the list.
You can use fn
to declare a function
:
fn hello() {
return 'world';
}
This is the same as:
function hello() {
return 'world';
}
Append new elements to an array just like in Swift:
let a = [1];
a += [2, 3];
Is the same as:
const a = [1];
a.push(...[2, 3]);
Applies not to IfCondition
:
fn hello() {
guard text !== "world" else {
return ""
}
return "Hello " + text
}
Is the same as:
function hello() {
if (text === 'world') {
return '';
}
return `Hello ${text}`;
}
try
can be used as an expression.
Applies tryCatch
:
const [error, result] = try hello('world');
Is the same as:
import tryCatch from 'try-catch';
const [error, result] = tryCatch(hello, 'world');
and
const [error, result] = try await hello('world');
Is the same as:
import tryToCatch from 'try-catch';
const [error, result] = await tryToCatch(hello, 'world');
You can use ?=
instead of try
:
const [error, result] ?= hello('world');
Is the same as:
import tryCatch from 'try-catch';
const [error, result] = tryCatch(hello, 'world');
and
const [error, result] ?= await hello('world');
Is the same as:
import tryToCatch from 'try-catch';
const [error, result] = await tryToCatch(hello, 'world');
should
can be used as an expression (just like try
).
This keyword is useful if you want to prevent a function call (also async) to throw an error because you don't need to have any result and the real execution is just optional (so runs if supported).
should hello()
Is the same as:
try hello();
☝️ Warning: this feature can be helpful but also dangerous especially if you're debugging your application. In fact, this is made to be used as an optional function call (ex. should load content, but not necessary and knowing this feature is optional), if you call a function in this way while debugging, no error will be printed and the application will continue run as nothing happened.
You can use freeze
instead of Object.freeze()
like that:
freeze {
'example': true
}
Is the same as:
Object.freeze({
example: true,
});
You can omit parens. But you must use braces in this case.
if a > 3 {
hello();
}
Also you can use if let
syntax:
if let x = a?.b {
print(x);
}
You can use throw as expression, just like that:
const a = () => throw 'hello';
Similar to partial application:
const sum = (a, b) => a + b;
const inc = sum~(1);
inc(5);
// returns
6
When you import .gs
files during compile step it will be replaced with .js
:
// hello.js
export const hello = () => 'world';
// index.js1
import hello from './hello.gs';
Will be converted to:
// index.js
import hello from './hello.js';
Also, also supported:
import hello from hello;
And will be converted to:
import hello from 'hello';
If you mistakenly put =>
in function declaration:
function hello() => {
}
That absolutely fine, it will be converted to:
function hello() {}
When you accidentally broke string:
const a = 'hello
const b = 'world';
Goldstein will fix it to:
const a = 'hello';
const b = 'world';
Forget to add assignment (=
), not problem!
-const {code, places} await samadhi(source);
+const {code, places} = await samadhi(source);
Added useless comma (,
)? no problem!
const a = {
- b,,
+ b,
};
Added useless semicolon (;
)? no problem!
const a = {
- b;
+ b,
};
const a = {
- b(){},
+ b(){}
};
const a = from 'a';
The same as:
const a = require('a');
Clone the registry, create a new keyword with a prefix keyword-
, then create directory fixture
and put there two files with extensions .js
and .gs
. Half way done 🥳!
Then goes test and implementation in index.js1
and index.spec.js
accordingly. Use scripts:
npm test
UPDATE=1 npm test
- updatefixtures
;AST=1 npm test
- logAST
;npm run coverage
;npm run fix:lint
;
Update docs and make PR, that's it!
MIT