Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

goldstein

coderaiser15.3kMIT5.25.0

JavaScript with no limits

JavaScript, parser, compiler

readme

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

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

operator-safe-assignment

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, Goldstein will fix it:

-const a = 'hello
+const a = 'hello';
-const a = ‘hello world’;
+const a = 'hello 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');

Export without const

export x = () => {};

The same as:

export const x = () => {};

Wrong brace )

-import a from 'a');
+import a from '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

changelog

2025.01.21, v5.25.0

feature:

  • 8d69b27 goldstein: estree-to-babel v10.1.0

2025.01.21, v5.24.0

feature:

  • 9cbc2b7 goldstein: preserve parenthesized

2025.01.12, v5.23.0

feature:

  • d4a789a goldstein: @putout/printer v12.0.0

2024.12.22, v5.22.1

feature:

  • 6e95200 goldstein: keyword-broken-string: goldstein methods

2024.12.22, v5.22.0

feature:

  • a7981c9 goldstein: broken string: improve

2024.12.19, v5.21.1

feature:

  • d949ff5 goldstein: keyword-export-no-const: no declaration.id

2024.12.19, v5.21.0

feature:

  • c0041b4 goldstein: wrong brace
  • a774470 goldstein: putout v37.0.0

2024.12.12, v5.20.0

feature:

  • 01d6d6f goldstein: keyword-export-no-const: add

2024.12.12, v5.19.2

feature:

  • 7a44206 goldstein: @putout/plugin-logical-expressions v7.0.0

2024.12.10, v5.19.1

feature:

  • dd22ac9 goldstein: @putout/printer v11.0.0

2024.10.27, v5.19.0

feature:

  • 76cb57d goldstein: @putout/printer v10.0.1
  • ee20f4e goldstein: estree-to-babel v10.0.0
  • ebd230e goldstein: esbuild v0.24.0

2024.09.06, v5.18.0

feature:

2024.07.27, v5.17.0

feature:

  • 7bf4e42 goldstein: putout v36.0.3
  • 9c03d48 goldstein: @putout/test v11.0.0
  • 5c329c3 goldstein: esbuild v0.23.0

2024.06.11, v5.16.0

feature:

  • dc62f70 keyword-useless-comma: improve support of identifiers
  • b09a906 @putout/plugin-goldstein: c8 v10.0.0
  • 7873c60 goldstein: c8 v10.0.0

2024.06.07, v5.15.1

feature:

  • 40d2966 goldstein: @putout/printer v9.0.1

2024.06.03, v5.15.0

feature:

  • 5d11929 goldstein: keyword-useless-comma: class methods

2024.06.03, v5.14.0

feature:

  • 7e81e0e goldstein: keyword-assign-from: add

2024.06.03, v5.13.2

fix:

  • 775a6fc goldstein: keyword: broken-string: infinite loop

feature:

  • 4eb6237 goldstein: @putout/test v10.0.0

2024.05.31, v5.13.1

fix:

  • 1d705cf goldstein: keyword-useless-comma: rename-unnamed-identifier: StringLiteral

2024.05.31, v5.13.0

feature:

  • c16a034 golstein: useless-semicolon

2024.05.30, v5.12.0

feature:

  • ade2799 goldstein: useless comma

2024.05.28, v5.11.0

feature:

  • c84aaa4 goldstein: add support of jsx/typescript

2024.05.27, v5.10.0

feature:

  • 05d0b92 goldstein: @putout/plugin-logical-expressions v6.0.0
  • 1554718 goldstein: Import: identifier

2024.05.23, v5.9.0

feature:

  • 1a9585b goldstein: missing-initializer: add

2024.05.22, v5.8.0

feature:

  • 2bd6e8c test: do not fail when update tests
  • f4fb1d6 goldstein: broken strings
  • e80d132 goldstein: check-dts v0.8.0

2024.05.11, v5.7.2

fix:

  • 3fddd10 goldstein: rm unused

2024.05.11, v5.7.1

feature:

  • 9856dbc goldstein: make esbuild optional

2024.05.09, v5.7.0

feature:

  • 2f2fecf goldstein: add ability to disable keyword

2024.05.09, v5.6.1

fix:

  • ecd46b4 goldstein: override keywords

2024.05.08, v5.6.0

feature:

  • 4be8b79 goldstein: keyword-fn: add constant
  • f0b7d54 goldstein: keyword-fn: add ability to use "fn" as function name

2024.05.08, v5.5.2

fix:

  • 811815f exports

2024.05.07, v5.5.1

fix:

  • b229122 goldstein: export

2024.05.07, v5.5.0

feature:

  • 44dee08 goldstein: get rid of ability to parse no async code with await

2024.05.07, v5.4.0

feature:

  • 0758d6a rm build

2024.05.06, v5.3.0

feature:

  • 98f3048 goldstein: add ability to parse no async code with await
  • 45c7363 @putout/plugin-goldstein: eslint-plugin-putout v22.7.0
  • 3964832 @putout/plugin-goldstein: @putout/test v9.1.0
  • aa689c7 @putout/plugin-goldstein: eslint v9.2.0
  • 1399854 @putout/plugin-goldstein: c8 v9.1.0
  • a728422 goldstein: redlint v3.14.1
  • 6f03127 goldstein: eslint v9.2.0
  • 23c8eb3 goldstein: @putout/plugin-declare v4.0.0

2024.02.20, v5.2.0

feature:

  • 5728421 goldstein: improve support of comments
  • 0d997c3 goldstein: printer: visitors: await-expression: maybeVisitor

2024.02.19, v5.1.0

feature:

  • c329ff0 goldstein: printer: visitors: try-statememnt: improve support

2024.02.19, v5.0.2

feature:

  • 7b1e60e goldstein: supertape v10.1.0
  • 448fd5b goldstein: @putout/test v9.0.1
  • 9e4433d goldstein: esbuild v0.20.1
  • 302b0c4 goldstein: @putout/plugin-declare v3.0.0

2024.01.29, v5.0.1

feature:

  • ea5ffdd goldstein: @putout/printer v8.0.1
  • 74221ae ifStatement: VariableDeclaration: drop semicolons
  • fa700a3 goldstein: c8 v9.1.0
  • 28b77bb goldstein: putout v35.0.0

2023.12.12, v5.0.0

feature:

  • 3e54d9c goldstein: escover v4.0.1
  • 9c35606 goldstein: drop support of node < 18
  • fb7a96d goldstein: @putout/plugin-logical-expressions v5.0.0
  • 51f1ff6 goldstein: @putout/printer v7.1.0
  • 27106cb goldstein: @putout/test v8.0.0
  • 04d7d2c goldstein: madrun v10.0.0
  • f477bb7 goldstein: eslint-plugin-putout v22.0.0
  • c8391d9 goldstein: putout v34.0.7
  • 0c81207 goldstein: supertape v9.0.0
  • fa384a5 goldstein: estree-to-babel v9.0.0

2023.10.28, v4.12.0

feature:

  • 9cd55bd package: @putout/printer v6.0.0
  • 3ff2ebe package: eslint-plugin-putout v21.0.1

2023.10.19, v4.11.0

feature:

  • 005959d goldstein: convert: if let

2023.10.19, v4.10.0

feature:

  • 0b4756a goldstein: add if let

2023.10.19, v4.9.1

fix:

  • aff82e3 goldstein: simplify

2023.10.19, v4.9.0

feature:

  • 2346464 goldstein: convert: add-array

2023.10.19, v4.8.0

feature:

  • 924f182 goldstein: add-array: add

2023.10.04, v4.7.0

feature:

  • 136f4a9 goldstein: convert: rm useless import of try-catch
  • fcb8679 package: estree-to-babel v8.0.0

2023.10.03, v4.6.0

feature:

  • 04ca141 goldstein: export print, convert

2023.10.03, v4.5.0

feature:

  • c55a3d1 goldstein: parse: type
  • db6bcbe goldstein: types

2023.10.03, v4.4.0

feature:

  • 02f16ef goldstein: add convert js to gs

2023.10.03, v4.3.0

feature:

  • 77cd780 goldstein: print: if

2023.10.03, v4.2.0

feature:

  • e5fb5c9 goldstein: printer: add ability to print goldstein: try

2023.09.18, v4.1.2

feature:

  • 99cf10f package: @putout/printer v5.0.0
  • 5471f17 package: eslint-plugin-putout v20.0.0

2023.09.14, v4.1.1

feature:

  • 003e2ce package: putout v32.0.1

2023.09.13, v4.1.0

feature:

  • a7b3eae package: @putout/printer v4.2.0
  • 5c23933 package: estree-to-babel v7.0.0

2023.08.30, v4.0.2

feature:

  • 6f46703 parser options

2023.08.29, v4.0.1

fix:

  • 999445f goldstein: get back sync

2023.08.28, v4.0.0

feature:

  • 37f9e82 goldstein: async
  • 901a4fa rules: convert-t-raise-to-raise
  • cb5d441 rules: add convert-t-raise-to-raise
  • 86c5230 rules: add
  • 8a8dd67 package: esbuild v0.19.2
  • 8501a14 package: @putout/printer v3.6.0

2023.08.09, v3.3.2

fix:

  • 456c500 should: typo

2023.08.07, v3.3.1

feature:

  • af6f474 package: eslint-plugin-putout v19.0.3
  • 2ecbcd5 package: putout v31.0.3

2023.07.09, v3.3.0

feature:

  • bbe6942 goldstein: update fixtures
  • 34e8be9 package: @putout/printer v2.61.0
  • 483a094 package: esbuild v0.18.11
  • 2ccfc8b package: escover v3.4.0
  • c49f5b6 package: eslint-plugin-putout v18.0.0
  • 86e8467 package: estree-to-babel v6.0.0
  • eee1ae4 package: c8 v8.0.0
  • 92dce62 package: putout v30.2.0
  • d7f423b package: eslint-plugin-n v16.0.1
  • 430bceb package: nodemon v3.0.1

2023.04.19, v3.2.4

fix:

  • dc9866d parser: comment

2023.04.19, v3.2.3

fix:

  • bf330c5 goldstein: add support of tokens

2023.04.19, v3.2.2

fix:

  • 1ec307f madrun: prepublish -> wisdom

2023.04.19, v3.2.1

fix:

  • fd137e5 parser: options

2023.04.18, v3.2.0

feature:

  • 76a336d golstein: add support of CommonJS

2023.04.18, v3.1.0

feature:

  • 4236346 goldstein: add ability to use arrow in FunctionDeclaration
  • 8e2197f goldstein: add ability tot pass keywords and 🐊Putout optionts

2023.04.02, v3.0.0

feature:

  • 3eeeab7 goldstein: add ability to simplify logical expressions during compilation
  • 7959e57 goldstein: add ability to compile import gs to js
  • c61e500 goldstein: improve compiler, disable bundling
  • 6cd279e goldstein: enable support of ifStatments with no round braces
  • 0ba6f9c major: goldstein: compile: use @putout/pinter instead of recast, parse: provide Babel AST

2023.04.01, v2.6.0

feature:

  • ccdb0f1 package: esbuild v0.17.14
  • ecf9023 package: eslint-plugin-putout v17.2.1
  • 15a95e5 package: typescript v5.0.3
  • 84a6049 package: putout v29.1.11
  • d4f22c6 package: check-dts v0.7.1

2023.01.06, v2.5.1

feature:

  • package: esbuild v0.16.14
  • package: putout v28.5.0

2022.11.14, v2.5.0

feature:

  • goldsten: export parse

2022.10.20, v2.4.2

feature:

  • package: putout v28.0.0
  • package: esbuild v0.15.7
  • package: @types/acorn v6.0.0
  • package: supertape v8.0.1

2022.07.20, v2.4.1

feature:

  • package: eslint-plugin-n v15.2.4
  • package: eslint-plugin-putout v16.0.0
  • package: putout v27.0.1

2022.06.30, v2.4.0

feature:

  • goldstein: freeze: simplify: MemberExpression -> Identifier
  • goldstein: add freeze keyword (#6)

2022.06.27, v2.3.0

feature:

  • goldstein: add curry

2022.06.27, v2.2.0

feature:

  • goldstein: add support of throw expressions

2022.06.25, v2.1.0

feature:

  • goldstein: integrate 'should'
  • add should keyword (#5)

2022.06.22, v2.0.0

feature:

  • goldstein: keyword: safe -> try

2022.06.22, v1.6.0

feature:

  • goldstein: guard keyword: add ability to omit parens

2022.06.22, v1.5.0

feature:

  • goldstein: keyword if: add ability to omit parens

2022.06.22, v1.4.0

feature:

  • goldstein: keyword safe: add support of VariableDeclaration

2022.06.22, v1.3.0

feature:

  • goldstein: add support of safe await

2022.06.22, v1.2.2

fix:

  • goldstein: cli: add mainFields

2022.06.22, v1.2.1

fix:

  • package: repository url

2022.06.22, v1.2.0

feature:

  • goldstein: add CLI

2022.06.21, v1.1.0

feature:

  • goldstein: add keyword-safe