Comparing version 0.0.1 to 0.0.2
# code-red changelog | ||
## 0.0.2 | ||
* Support `@`-prefixed names (replaceable globals) | ||
* Support `#`-prefixed names (automatically deconflicted) | ||
## 0.0.1 | ||
* First experimental release |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('astring'), require('source-map'), require('acorn'), require('estree-walker')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'astring', 'source-map', 'acorn', 'estree-walker'], factory) : | ||
(factory((global.CodeRed = {}),global.astring,global.SourceMap,global.acorn,global.estreeWalker)); | ||
}(this, (function (exports,astring,SourceMap,acorn,estreeWalker) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('astring'), require('source-map'), require('periscopic'), require('estree-walker'), require('is-reference'), require('acorn')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'astring', 'source-map', 'periscopic', 'estree-walker', 'is-reference', 'acorn'], factory) : | ||
(factory((global.CodeRed = {}),global.astring,global.SourceMap,global.perisopic,global.estreeWalker,global.is_reference,global.acorn)); | ||
}(this, (function (exports,astring,SourceMap,perisopic,estreeWalker,is_reference,acorn) { 'use strict'; | ||
const generator = Object.assign({}, astring.baseGenerator, { | ||
AwaitExpression( node, state) { | ||
state.write('await '); | ||
const { argument } = node; | ||
this[argument.type](argument, state); | ||
}, | ||
is_reference = is_reference && is_reference.hasOwnProperty('default') ? is_reference['default'] : is_reference; | ||
FunctionExpression( node, state) { | ||
const params = [].concat(...node.params.map((param) => { | ||
if (param.type === 'SequenceExpression') { | ||
return param.expressions; | ||
function deconflict(name, names) { | ||
const original = name; | ||
let i = 1; | ||
while (names.has(name)) { | ||
name = `${original}$${i++}`; | ||
} | ||
return name; | ||
} | ||
function print(node, opts = {}) { | ||
const { | ||
getName = (x) => x | ||
} = opts; | ||
const { map: scope_map } = perisopic.analyze(node); | ||
const deconflicted = new WeakMap(); | ||
estreeWalker.walk(node, { | ||
enter(node, parent) { | ||
if (is_reference(node, parent)) ; | ||
} | ||
}); | ||
const generator = Object.assign({}, astring.baseGenerator, { | ||
AwaitExpression( node, state) { | ||
state.write('await '); | ||
const { argument } = node; | ||
this[argument.type](argument, state); | ||
}, | ||
FunctionExpression( node, state) { | ||
const params = [].concat(...node.params.map((param) => { | ||
if (param.type === 'SequenceExpression') { | ||
return param.expressions; | ||
} | ||
return param; | ||
})); | ||
return astring.baseGenerator.FunctionExpression.call(this, { ...node, params }, state); | ||
}, | ||
Identifier( node, state) { | ||
if (node.name[0] === '@') { | ||
node = { ...node, name: getName(node.name.slice(1)) }; | ||
} | ||
return param; | ||
})); | ||
if (node.name[0] === '#') { | ||
const scope = scope_map.get(node); | ||
const owner = scope.find_owner(node.name); | ||
return astring.baseGenerator.FunctionExpression.call(this, { ...node, params }, state); | ||
} | ||
}); | ||
if (!deconflicted.has(owner)) { | ||
deconflicted.set(owner, new Map()); | ||
} | ||
function print(node, opts = {}) { | ||
const deconflict_map = deconflicted.get(owner); | ||
if (!deconflict_map.has(node.name)) { | ||
deconflict_map.set(node.name, deconflict(node.name.slice(1), owner.references)); | ||
} | ||
const name = deconflict_map.get(node.name); | ||
node = { ...node, name }; | ||
} | ||
return astring.baseGenerator.Identifier.call(this, node, state); | ||
} | ||
}); | ||
const map = new SourceMap.SourceMapGenerator({ | ||
@@ -43,2 +95,7 @@ file: opts.file | ||
const sigils = { | ||
'@': 'AT', | ||
'#': 'HASH' | ||
}; | ||
const join = (strings) => { | ||
@@ -49,3 +106,3 @@ let str = strings[0]; | ||
} | ||
return str; | ||
return str.replace(/([@#])(\w+)/g, (_m, sigil, name) => `___${sigils[sigil]}___${name}`); | ||
}; | ||
@@ -60,8 +117,13 @@ | ||
if (node.type === 'Identifier') { | ||
const match = /___(\d+)___/.exec(node.name); | ||
if (match && +match[1] in values) { | ||
const match = /___(?:(\d+)|(AT)|(HASH))___(\w+)?/.exec(node.name); | ||
if (match) { | ||
const value = match[1] | ||
? +match[1] in values ? values[+match[1]] : match[1] | ||
: { type: 'Identifier', name: `${match[2] ? `@` : `#`}${match[4]}` }; | ||
if (index === null) { | ||
parent[key] = values[+match[1]]; | ||
parent[key] = value; | ||
} else { | ||
parent[key][index] = values[+match[1]]; | ||
parent[key][index] = value; | ||
} | ||
@@ -68,0 +130,0 @@ } |
{ | ||
"name": "code-red", | ||
"description": "code-red", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"repository": "Rich-Harris/code-red", | ||
@@ -37,4 +37,6 @@ "main": "dist/code-red.js", | ||
"estree-walker": "^0.6.1", | ||
"is-reference": "^1.1.3", | ||
"periscopic": "^1.0.0", | ||
"source-map": "^0.7.3" | ||
} | ||
} |
@@ -74,2 +74,7 @@ # code-red | ||
## Optimiser | ||
TODO add an optimiser that e.g. collapses consecutive identical if blocks | ||
## Compiler | ||
@@ -76,0 +81,0 @@ |
@@ -5,2 +5,3 @@ import * as acorn from 'acorn'; | ||
file?: string; | ||
getName?: (name: string) => string; | ||
}; | ||
@@ -7,0 +8,0 @@ export declare function print(node: acorn.Node, opts?: PrintOptions): { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11623
255
91
6
+ Addedis-reference@^1.1.3
+ Addedperiscopic@^1.0.0
+ Added@types/estree@1.0.6(transitive)
+ Addedis-reference@1.2.1(transitive)
+ Addedperiscopic@1.1.0(transitive)