包详细信息

code-red

Rich-Harris3.2mMIT1.0.4

code-red

自述文件

code-red

Experimental toolkit for writing x-to-JavaScript compilers. It is used in Svelte.

API

The code-red package exposes three core functions — b, x and print.

b and x take a template literal and return an ESTree program body, or a single node:

import { b, x } from 'code-red';

const expression = x`i + j`;

assert.equal(expression.type, 'AssignmentExpression');
assert.equal(expression.operator, '+');
assert.equal(expression.left.name, 'i');
assert.equal(expression.right.name, 'j');

const body = b`
    const i = 1;
    const j = 2;
    const k = i + j;
`;

assert.equal(body.length, 3);
assert.equal(body[0].type, 'VariableDeclaration');

Expressions in template literals correspond to replacement nodes — so you could express the above like so:

const i = x`i`;
const j = x`j`;
const expression = x`${i} + ${j}`;

const body = b`
    const ${i} = 1;
    const ${j} = 2;
    const k = ${expression};
`;

The print function takes a node and turns it into a {code, map} object:

const add = x`
    function add(${i}, ${j}) {
        return ${expression};
    }
`;

print(add).code;
/*
function add(i, j) {
    return i + j;
}
*/

i.name = 'foo';
j.name = 'bar';

print(add).code;
/*
function add(foo, bar) {
    return foo + bar;
}
*/

Prefixes

@-prefixed names (replaceable globals)

So that you can use globals in your code. In Svelte, we use this to insert utility functions.

// input
import { x } from 'code-red';
x`@foo(bar)`

// output
FOO(bar)

#-prefixed names (automatically deconflicted names)

So that you can insert variables in your code without worrying if they clash with existing variable names.

bar used in user code and in inserted code gets a $1 suffix:

// input
import { x } from 'code-red';
x`
function foo(#bar) {
    return #bar * bar;
}`;

// output
function foo(bar$1) {
    return bar$1 * bar;
}

Without conflicts, no $1 suffix:

// input
import { b } from 'code-red';
b`const foo = #bar => #bar * 2`;

// output
const foo = bar => bar * 2;

Optimiser

TODO add an optimiser that e.g. collapses consecutive identical if blocks

Compiler

TODO add a code-red/compiler module that replaces template literals with the nodes they evaluate to, so that there's nothing to parse at runtime.

Sourcemaps

TODO support source mappings for inserted nodes with location information.

License

MIT

更新日志

code-red changelog

1.0.4

  • Add types to pkg.exports (#83)

1.0.3

  • Fix inline comments in function arguments (#79)

1.0.2

  • Use dts-buddy to generate type declarations (#78)

1.0.1

  • Remove dist directory from package (#77)
  • Fix position of inline comments (#76)

1.0.0

  • Breaking: remove CJS build, remove main and module (#75)

0.2.7

  • Update to es2022 (#73)

0.2.6

  • Replace sourcemap-codec with @jridgewell/sourcemap-codec (#74)

0.2.5

  • Prevent stack overflow with very large ASTs (#71)

0.2.4

  • Fix output for arrow functions where body is an object destructuring assignment (#70)

0.2.3

  • Fix output with comments within parenthesized return statement (#36)
  • Fix output for identifier at root of AST (#37)
  • Fix output for statements with empty bodies (#65)

0.2.2

  • Update dependencies (#63)

0.2.1

  • Fix handling of string literal raw values (#61)

0.2.0

  • Rewrite in JavaScript

0.1.7

  • Include dependencies (#56)
  • Add sourceMapEncodeMappings option (#51)

0.1.6

  • Only use shorthand for non-computed properties (#58)

0.1.5

  • Use node.raw where possible (#55)
  • Support BigInt ((#54))

0.1.4

  • Fix rendering of nullish coalescing operator alongside other logical operators (#52)

0.1.3

  • Support nullish coalescing operator (#42)
  • Support optional chaining (#43)

0.1.2

  • Don't crash when using an arrow function as a statement (#38)

0.1.1

  • Wrap arrow functions in parens as appropriate (#31)
  • Throw on invalid expressions (#31)

0.1.0

  • Throw on unhandled sigils (#30)

0.0.32

  • Prevent syntax errors when combining comments (#28)

0.0.31

  • Expose wrapped versions of Acorn methods to facilitate comment preservation (#26)

0.0.30

  • Wrap await argument in parens if necessary (#24)

0.0.29

  • Handle sigils in comments (#21)

0.0.28

  • Add toString and toUrl methods on sourcemap objects (#22)

0.0.27

  • Handle parenthesized expressions

0.0.26

  • Always replace comment values (#20)

0.0.25

  • Fix async/generator functions in object methods (#18)

0.0.24

  • Determine shorthand eligibility after stringification (#17)

0.0.23

  • Unescape sigils in literals (#16)

0.0.22

  • Prevent erroneous object shorthand when key is an identifier (#14)

0.0.21

  • Deconflict #-identifiers in function names (#10)
  • Fix object expression with string literal key matching value (#9)

0.0.20

  • Update deps

0.0.19

  • Attach comments

0.0.18

  • Handle mixed named/default imports (#3)
  • Update dependencies (#4)

0.0.17

  • Fixes and additions

0.0.16

  • Improve some aspects of generated code

0.0.15

  • Flatten patterns

0.0.13-14

  • Sourcemaps

0.0.12

  • Flatten object properties

0.0.11

  • Handle deconfliction edge case

0.0.10

  • Tweak some TypeScript stuff

0.0.9

  • Adopt estree types
  • Add a p function for creating properties

0.0.8

  • Allow strings to be treated as identifiers

0.0.7

  • Various

0.0.6

  • Flatten arguments and parameters

0.0.5

  • Omit missing statements
  • Flatten arrays of statements

0.0.4

  • Use fork of astring

0.0.3

  • Allow return outside function
  • Print code on syntax error

0.0.2

  • Support @-prefixed names (replaceable globals)
  • Support #-prefixed names (automatically deconflicted)

0.0.1

  • First experimental release