abcify cli
This abcify
walks the input AST with visitors to sort commutative expressions shown here:
- object literal by keys,
- es2015 object destructing by keys,
- react jsx properties by names,
- flow type object literal by keys,
- flow union by this criteria (undefined < null < boolean < number < string),
- switch cases by (undefined < null < boolean < number < string),
note: supporting es2015, react, and flow.
SETUP
npm install abcify
note:
- if needed, add
--save-dev
xor--global
flag. - if dependencies failed, run
npm install recast yargs flow-parser
again.
EXAMPLE
abcify test/example.js
# to print abcified output to console or terminal.abcify test/example.js -s
# to save abcified output to the input file.
where test/example.js
input is shown here:
"use strict"; ;; const c b a = ; type Numeric = 10 | 2 | 1 | -1;type Strange = false | 400 | "hello" | undefined | 9 | "yes" | true | null | -5;type Sizes = "pettie" | "small" | "medium" | "large"; type Obj = "b;": boolean "a:": number e: Array<number> d: y: boolean z: number x: string c: string; const obj: Obj = e: 3 2 1 0 d: z: 0 y: false x: "hey" c: "hello" "b;": true "a:": 1; console; const e d: y x = obj; const Box = <div z="hello" y=true x=1> <div style=weight: "100%" fontSize: "10px" className="hello"> box </div> </div>; { }
where expected output is shown here:
"use strict"; ;; const a b c = ; type Numeric = -1 | 1 | 2 | 10;type Strange = undefined | null | false | true | -5 | 9 | 400 | "hello" | "yes";type Sizes = "large" | "medium" | "pettie" | "small"; type Obj = "a:": number "b;": boolean c: string d: x: string y: boolean z: number e: Array<number>; const obj: Obj = "a:": 1 "b;": true c: "hello" d: x: "hey" y: false z: 0 e: 3 2 1 0; console; const d: x y e = obj; const Box = <div x=1 y=true z="hello"> <div className="hello" style=fontSize: "10px" weight: "100%"> box </div> </div>; { }
where git diff
of test/example.js
is shown here:
"use strict"; -import {z, y, x} from "./xyz.js";-import type {Y, X} from "./xyz.js";+import {x, y, z} from "./xyz.js";+import type {X, Y} from "./xyz.js"; -const {c, b, a} = require("./abc.js");+const {a, b, c} = require("./abc.js"); -type Numeric = 10 | 2 | 1 | -1;-type Strange = false | 400 | "hello" | undefined | 9 | "yes" | true | null | -5;-type Sizes = "pettie" | "small" | "medium" | "large";+type Numeric = -1 | 1 | 2 | 10;+type Strange = undefined | null | false | true | -5 | 9 | 400 | "hello" | "yes";+type Sizes = "large" | "medium" | "pettie" | "small"; type Obj = {- "b;": boolean, "a:": number,- e: Array<number>,- d: {y: boolean, z: number, x: string},+ "b;": boolean, c: string,+ d: {x: string, y: boolean, z: number},+ e: Array<number>, }; const obj: Obj = {- e: [3, 2, 1, 0],- d: {z: 0, y: false, x: "hey"},- c: "hello",- "b;": true, "a:": 1,+ "b;": true,+ c: "hello",+ d: {x: "hey", y: false, z: 0},+ e: [3, 2, 1, 0], }; -console.log({k3: e, k2: d, k1: obj});+console.log({k1: obj, k2: d, k3: e}); -const {e, d: {y, x}} = obj;+const {d: {x, y}, e} = obj; const Box = (- <div z="hello" y={true} x={1}>- <div style={{weight: "100%", fontSize: "10px"}} className="hello">+ <div x={1} y={true} z="hello">+ <div className="hello" style={{fontSize: "10px", weight: "100%"}}> box </div> </div> const Box = ( function test(a) { switch (a) {+ case 1: case 2: case 3:- case 1: case "5": console.log("a is 1 < 2 < 3 < '5'"); break;- case "zebra":+ case 10: case "alpha": case "beta":- case 10:+ case "zebra": console.log("a is 10 < 'alpha' < 'beta' < 'zebra'"); break;- case true: case false:- case 20:+ case true: case 15:+ case 20: case "b": console.log("a is boolean false < true < 15 < 20 < 'b'"); break; case 100:- case "a": case "200":+ case "a": default: console.log("a is default 100 < '200' < 'a' < default"); break;