0.6.3 • Published 2 months ago

typescript-plugin-safescript v0.6.3

Weekly downloads
162
License
MIT
Repository
-
Last release
2 months ago

TypeScript Plugin for SafeScript Runtime

This package intent manipulate AST to enforce type checking at runtime

How it is different from Babel Plugin for SafeScript Runtime

Consider the following example:

const str0 = "3124122";
const num0 = 9;
const str1 = "3124122";
const num1 = str1 - num0;
const num2 = 55 + num1;
const num3 = 5 + num0;
const num4 = (num3 + 1) + 8;
let num5 = 3;
num5 += "3";

let str2 = "24256";
let numstr2 = +str2;

++numstr2;

function something(num) {
    if (typeof num === "number") {
        return num + 88;
    }
    return num + 88;
}
something(2);

Babel Plugin will generate the following file:

const str0 = "3124122";
const num0 = 9;
const str1 = "3124122";
const num1 = SafeScript.sub(str1, num0);
const num2 = SafeScript.add(55, num1);
const num3 = SafeScript.add(5, num0);
const num4 = SafeScript.add(SafeScript.add(num3, 1), 8);
let num5 = 3;
num5 = SafeScript.add(num5, "3");

let str2 = "24256";
let numstr2 = SafeScript.plus(str2);

(numstr2 = SafeScript.inc(numstr2));

function something(num) {
  if (typeof num === "number") {
    return SafeScript.add(num, 88);
  }
  return SafeScript.add(num, 88);
}
something(2);

As you can see Babel Plugin changed each operator, because it do not have information about types for expressions and variables

Lets check what will generate TypeScript Plugin:

const str0 = "3124122";
const num0 = 9;
const str1 = "3124122";
const num1 = SafeScript.sub(str1, num0);
const num2 = 55 + num1;
const num3 = 5 + num0;
const num4 = (num3 + 1) + 8;
let num5 = 3;
num5 = SafeScript.add(num5, "3");

let str2 = "24256";
let numstr2 = SafeScript.plus(str2);

++numstr2;

function something(num) {
    if (typeof num === "number") {
        return num + 88;
    }
    return SafeScript.add(num, 88);
}
something(2);

As you can see TypeScript Plugin inserted only 4 checks at runtime instead of 10 for Babel Plugin. It is because TypeScript Plugin knows type of each expression and variable at any place in code and it could decide if runtime check is needed

0.6.3

2 months ago

0.6.2

2 months ago

0.6.1

2 years ago

0.6.0

3 years ago

0.5.3

3 years ago

0.5.0

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.4.5

3 years ago

0.4.1

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.3.0

3 years ago

0.2.1

3 years ago

0.4.0

3 years ago

0.1.2

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago