Experimental toolkit for writing x-to-JavaScript compilers
Installations
npm install code-red
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
18.15.0
NPM Version
9.5.0
Score
98.6
Supply Chain
99.6
Quality
78.2
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
Rich-Harris
Download Statistics
Total Downloads
38,720,792
Last Day
56,577
Last Week
814,289
Last Month
3,938,183
Last Year
33,138,854
GitHub Statistics
334 Stars
210 Commits
22 Forks
6 Watching
3 Branches
11 Contributors
Package Meta Information
Latest Version
1.0.4
Package Id
code-red@1.0.4
Unpacked Size
53.89 kB
Size
13.82 kB
File Count
11
NPM Version
9.5.0
Node Version
18.15.0
Publised On
11 Aug 2023
Total Downloads
Cumulative downloads
Total Downloads
38,720,792
Last day
-70.2%
56,577
Compared to previous day
Last week
-7.1%
814,289
Compared to previous week
Last month
28.5%
3,938,183
Compared to previous month
Last year
507.7%
33,138,854
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
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:
1import { b, x } from 'code-red'; 2 3const expression = x`i + j`; 4 5assert.equal(expression.type, 'AssignmentExpression'); 6assert.equal(expression.operator, '+'); 7assert.equal(expression.left.name, 'i'); 8assert.equal(expression.right.name, 'j'); 9 10const body = b` 11 const i = 1; 12 const j = 2; 13 const k = i + j; 14`; 15 16assert.equal(body.length, 3); 17assert.equal(body[0].type, 'VariableDeclaration');
Expressions in template literals correspond to replacement nodes — so you could express the above like so:
1const i = x`i`; 2const j = x`j`; 3const expression = x`${i} + ${j}`; 4 5const body = b` 6 const ${i} = 1; 7 const ${j} = 2; 8 const k = ${expression}; 9`;
The print
function takes a node and turns it into a {code, map}
object:
1const add = x` 2 function add(${i}, ${j}) { 3 return ${expression}; 4 } 5`; 6 7print(add).code; 8/* 9function add(i, j) { 10 return i + j; 11} 12*/ 13 14i.name = 'foo'; 15j.name = 'bar'; 16 17print(add).code; 18/* 19function add(foo, bar) { 20 return foo + bar; 21} 22*/
Prefixes
@
-prefixed names (replaceable globals)
So that you can use globals in your code. In Svelte, we use this to insert utility functions.
1// input 2import { x } from 'code-red'; 3x`@foo(bar)` 4 5// output 6FOO(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:
1// input 2import { x } from 'code-red'; 3x` 4function foo(#bar) { 5 return #bar * bar; 6}`; 7 8// output 9function foo(bar$1) { 10 return bar$1 * bar; 11}
Without conflicts, no $1
suffix:
1// input 2import { b } from 'code-red'; 3b`const foo = #bar => #bar * 2`; 4 5// output 6const 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
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
Found 3/15 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/Rich-Harris/code-red/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/Rich-Harris/code-red/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/Rich-Harris/code-red/ci.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 21 are checked with a SAST tool
Score
3.6
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More