Skip to content

coderaiser/goldstein

Repository files navigation

🤫Goldstein License NPM version Build Status Coverage Status

image

"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 :).

Install

npm i goldstein esbuild -g

CLI

$ 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');
}

API

compile(source)

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.

parse(source, {type, keywords})

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);

print(ast)

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);

convert(source)

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')`;

Keywords

Goldstein is absolutely compatible with JavaScript, and it has extensions. Here is the list.

fn

You can use fn to declare a function:

fn hello() {
    return 'world';
}

This is the same as:

function hello() {
    return 'world';
}

append array

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]);

guard

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

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

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.

freeze

You can use freeze instead of Object.freeze() like that:

freeze {
    'example': true
}

Is the same as:

Object.freeze({
    example: true,
});

if

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);
}

throw expression

You can use throw as expression, just like that:

const a = () => throw 'hello';

Curry

Similar to partial application:

const sum = (a, b) => a + b;
const inc = sum~(1);

inc(5);
// returns
6

Import

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';

FunctionDeclaration with Arrow

If you mistakenly put => in function declaration:

function hello() => {
}

That absolutely fine, it will be converted to:

function hello() {}

Broken String

When you accidentally broke string:

const a = 'hello
const b = 'world';

Goldstein will fix it to:

const a = 'hello';
const b = 'world';

Missing Initializer

Forget to add assignment (=), not problem!

-const {code, places} await samadhi(source);
+const {code, places} = await samadhi(source);

Useless comma

Added useless comma (,)? no problem!

const a = {
-    b,,
+    b,
};

Useless semicolon

Added useless semicolon (;)? no problem!

const a = {
-    b;
+    b,
};

const a = {
-    b(){},
+    b(){}
};

Assign from

const a = from 'a';

The same as:

const a = require('a');

How to contribute?

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 - update fixtures;
  • AST=1 npm test - log AST;
  • npm run coverage;
  • npm run fix:lint;

Update docs and make PR, that's it!

License

MIT