Comparing version 0.0.8 to 0.0.9
# code-red changelog | ||
## 0.0.9 | ||
* Adopt estree types | ||
* Add a `p` function for creating properties | ||
## 0.0.8 | ||
@@ -4,0 +9,0 @@ |
@@ -30,3 +30,3 @@ (function (global, factory) { | ||
console.log(node); | ||
throw new Error('hmm'); | ||
throw new Error('we have an array where there probably should not be an array'); | ||
for (let i = 0; i < node.length; i += 1) { | ||
@@ -48,2 +48,3 @@ this.handle(node[i], state); | ||
if (!this[node.type]) { | ||
console.log(node); | ||
throw new Error(`Not implemented: ${node.type}`); | ||
@@ -58,3 +59,3 @@ } | ||
err.depth = 1; | ||
} else if (err.depth <= 3) { | ||
} else if (err.depth <= 2) { | ||
console.log(`${err.depth}:`, JSON.stringify(node, null, ' ')); | ||
@@ -90,2 +91,7 @@ err.depth += 1; | ||
if (!owner) { | ||
console.log(node); | ||
throw new Error(`Could not find owner for node`); | ||
} | ||
if (!deconflicted.has(owner)) { | ||
@@ -193,4 +199,4 @@ deconflicted.set(owner, new Map()); | ||
leave(node, parent, key, index) { | ||
delete node.start; | ||
delete node.end; | ||
delete (node ).start; | ||
delete (node ).end; | ||
@@ -205,10 +211,13 @@ if (node.type === 'Identifier') { | ||
let value = values[+match[1]]; | ||
if (typeof value === 'string') { | ||
value = { type: 'Identifier', name: value }; | ||
} else if (typeof value === 'number') { | ||
value = { type: 'Literal', value }; | ||
} | ||
if (index === null) { | ||
parent[key] = value; | ||
(parent )[key] = value; | ||
} else { | ||
parent[key][index] = value; | ||
(parent )[key][index] = value; | ||
} | ||
@@ -229,2 +238,7 @@ } | ||
if (node.type === 'TemplateElement') { | ||
re.lastIndex = 0; | ||
node.value.raw = (node.value.raw ).replace(re, (m, i) => +i in values ? values[+i] : m); | ||
} | ||
if (node.type === 'Program' || node.type === 'BlockStatement') { | ||
@@ -234,5 +248,21 @@ node.body = flatten_body(node.body, []); | ||
if (node.params) node.params = flatten(node.params, []); | ||
if (node.arguments) node.arguments = flatten(node.arguments, []); | ||
if (node.specifiers) node.specifiers = flatten(node.specifiers, []); | ||
if (node.type === 'ObjectExpression') { | ||
node.properties = node.properties.filter((prop) => prop.value); | ||
} | ||
if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'ArrowFunctionExpression') { | ||
node.params = flatten(node.params, []); | ||
} | ||
if (node.type === 'CallExpression' || node.type === 'NewExpression') { | ||
node.arguments = flatten(node.arguments, []); | ||
} | ||
if (node.type === 'ImportDeclaration' || node.type === 'ExportNamedDeclaration') { | ||
node.specifiers = flatten(node.specifiers, []); | ||
} | ||
if (node.type === 'ArrayExpression') { | ||
node.elements = flatten(node.elements, []); | ||
} | ||
} | ||
@@ -256,4 +286,3 @@ }); | ||
} catch (err) { | ||
console.log(`failed to parse:\n${str}`); // TODO proper error reporting | ||
throw err; | ||
handle_error(str, err); | ||
} | ||
@@ -268,5 +297,4 @@ } | ||
allowAwaitOutsideFunction: true, | ||
allowImportExportEverywhere: true, | ||
allowReturnOutsideFunction: true | ||
}); | ||
allowImportExportEverywhere: true | ||
}) ; | ||
@@ -277,9 +305,38 @@ inject(expression, values); | ||
} catch (err) { | ||
console.log(`failed to parse:\n${str}`); // TODO proper error reporting | ||
throw err; | ||
handle_error(str, err); | ||
} | ||
} | ||
function p(strings, ...values) { | ||
const str = `{${join(strings)}}`; | ||
try { | ||
const expression = acorn.parseExpressionAt(str, 0, { | ||
allowAwaitOutsideFunction: true, | ||
allowImportExportEverywhere: true | ||
}) ; | ||
inject(expression, values); | ||
return expression.properties[0]; | ||
} catch (err) { | ||
handle_error(str, err); | ||
} | ||
} | ||
function handle_error(str, err) { | ||
// TODO location/code frame | ||
str = str.replace(re, (m, i, s, n) => { | ||
if (s) return sigils[s] + n; | ||
return '${...}'; | ||
}); | ||
console.log(`failed to parse:\n${str}`); | ||
throw err; | ||
} | ||
exports.b = b; | ||
exports.x = x; | ||
exports.p = p; | ||
exports.print = print; | ||
@@ -286,0 +343,0 @@ |
{ | ||
"name": "code-red", | ||
"description": "code-red", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"repository": "Rich-Harris/code-red", | ||
@@ -15,4 +15,6 @@ "main": "dist/code-red.js", | ||
"@types/astring": "^1.3.0", | ||
"@types/estree": "0.0.39", | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.9.4", | ||
"estree-walker": "^0.8.1", | ||
"mocha": "^5.2.0", | ||
@@ -31,3 +33,4 @@ "rollup": "^0.65.2", | ||
"test": "mocha --opts mocha.opts", | ||
"prepublishOnly": "npm test && npm run build" | ||
"prepublishOnly": "npm test && npm run build", | ||
"repl": "node -e \"const { x, b, print } = require('./')\" -i" | ||
}, | ||
@@ -38,3 +41,2 @@ "license": "MIT", | ||
"astring": "github:Rich-Harris/astring#generic-handler", | ||
"estree-walker": "^0.6.1", | ||
"is-reference": "^1.1.3", | ||
@@ -41,0 +43,0 @@ "periscopic": "^1.0.0", |
@@ -1,4 +0,5 @@ | ||
import * as acorn from 'acorn'; | ||
export declare function b(strings: TemplateStringsArray, ...values: any[]): any; | ||
export declare function x(strings: TemplateStringsArray, ...values: any[]): acorn.Node; | ||
import { Node } from 'estree'; | ||
export declare function b(strings: TemplateStringsArray, ...values: any[]): Node; | ||
export declare function x(strings: TemplateStringsArray, ...values: any[]): Node; | ||
export declare function p(strings: TemplateStringsArray, ...values: any[]): Node; | ||
export { print } from './print/index'; |
@@ -1,2 +0,2 @@ | ||
import * as acorn from 'acorn'; | ||
import { Node } from 'estree'; | ||
declare type PrintOptions = { | ||
@@ -6,3 +6,3 @@ file?: string; | ||
}; | ||
export declare function print(node: acorn.Node, opts?: PrintOptions): { | ||
export declare function print(node: Node, opts?: PrintOptions): { | ||
code: string; | ||
@@ -9,0 +9,0 @@ map: any; |
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
20094
5
541
12
- Removedestree-walker@^0.6.1
- Removedestree-walker@0.6.1(transitive)