Comparing version 0.0.30 to 0.0.31
# code-red changelog | ||
## 0.0.31 | ||
* Expose wrapped versions of Acorn methods to facilitate comment preservation ([#26](https://github.com/Rich-Harris/code-red/issues/26)) | ||
## 0.0.30 | ||
@@ -4,0 +8,0 @@ |
@@ -11,2 +11,55 @@ (function (global, factory) { | ||
const get_comment_handlers = (comments, raw) => ({ | ||
// pass to acorn options | ||
onComment: (block, value, start, end) => { | ||
if (block && /\n/.test(value)) { | ||
let a = start; | ||
while (a > 0 && raw[a - 1] !== '\n') a -= 1; | ||
let b = a; | ||
while (/[ \t]/.test(raw[b])) b += 1; | ||
const indentation = raw.slice(a, b); | ||
value = value.replace(new RegExp(`^${indentation}`, 'gm'), ''); | ||
} | ||
comments.push({ type: block ? 'Block' : 'Line', value, start, end }); | ||
}, | ||
// pass to estree-walker options | ||
enter(node) { | ||
let comment; | ||
while (comments[0] && comments[0].start < (node ).start) { | ||
comment = comments.shift(); | ||
comment.value = comment.value.replace(re, (match, id, at, hash, value) => { | ||
if (hash) return `#${value}`; | ||
if (at) return `@${value}`; | ||
return match; | ||
}); | ||
const next = comments[0] || node; | ||
(comment ).has_trailing_newline = ( | ||
comment.type === 'Line' || | ||
/\n/.test(raw.slice(comment.end, (next ).start)) | ||
); | ||
(node.leadingComments || (node.leadingComments = [])).push(comment); | ||
} | ||
}, | ||
leave(node) { | ||
if (comments[0]) { | ||
const slice = raw.slice((node ).end, comments[0].start); | ||
if (/^[,) \t]*$/.test(slice)) { | ||
node.trailingComments = [comments.shift()]; | ||
} | ||
} | ||
} | ||
}); | ||
function handle(node, state) { | ||
@@ -1524,2 +1577,3 @@ const handler = handlers[node.type]; | ||
const acorn_opts = (comments, raw) => { | ||
const { onComment } = get_comment_handlers(comments, raw); | ||
return { | ||
@@ -1531,16 +1585,3 @@ ecmaVersion: 11, | ||
allowReturnOutsideFunction: true, | ||
onComment: (block, value, start, end) => { | ||
if (block && /\n/.test(value)) { | ||
let a = start; | ||
while (a > 0 && raw[a - 1] !== '\n') a -= 1; | ||
let b = a; | ||
while (/[ \t]/.test(raw[b])) b += 1; | ||
const indentation = raw.slice(a, b); | ||
value = value.replace(new RegExp(`^${indentation}`, 'gm'), ''); | ||
} | ||
comments.push({ type: block ? 'Block' : 'Line', value, start, end }); | ||
} | ||
onComment | ||
} ; | ||
@@ -1554,26 +1595,7 @@ }; | ||
const { enter, leave } = get_comment_handlers(comments, raw); | ||
estreeWalker.walk(node, { | ||
enter(node) { | ||
let comment; | ||
enter, | ||
while (comments[0] && comments[0].start < (node ).start) { | ||
comment = comments.shift(); | ||
comment.value = comment.value.replace(re, (match, id, at, hash, value) => { | ||
if (hash) return `#${value}`; | ||
if (at) return `@${value}`; | ||
return match; | ||
}); | ||
const next = comments[0] || node; | ||
(comment ).has_trailing_newline = ( | ||
comment.type === 'Line' || | ||
/\n/.test(raw.slice(comment.end, (next ).start)) | ||
); | ||
(node.leadingComments || (node.leadingComments = [])).push(comment); | ||
} | ||
}, | ||
leave(node, parent, key, index) { | ||
@@ -1645,9 +1667,3 @@ if (node.type === 'Identifier') { | ||
if (comments[0]) { | ||
const slice = raw.slice((node ).end, comments[0].start); | ||
if (/^[,) \t]*$/.test(slice)) { | ||
node.trailingComments = [comments.shift()]; | ||
} | ||
} | ||
leave(node); | ||
} | ||
@@ -1718,4 +1734,22 @@ }); | ||
const parse = (source, opts) => { | ||
const comments = []; | ||
const { onComment, enter, leave } = get_comment_handlers(comments, source); | ||
const ast = acorn.parse(source, { onComment, ...opts }); | ||
estreeWalker.walk(ast , { enter, leave }); | ||
return ast; | ||
}; | ||
const parseExpressionAt = (source, index, opts) => { | ||
const comments = []; | ||
const { onComment, enter, leave } = get_comment_handlers(comments, source); | ||
const ast = acorn.parseExpressionAt(source, index, { onComment, ...opts }); | ||
estreeWalker.walk(ast , { enter, leave }); | ||
return ast; | ||
}; | ||
exports.b = b; | ||
exports.p = p; | ||
exports.parse = parse; | ||
exports.parseExpressionAt = parseExpressionAt; | ||
exports.print = print; | ||
@@ -1722,0 +1756,0 @@ exports.x = x; |
{ | ||
"name": "code-red", | ||
"description": "code-red", | ||
"version": "0.0.30", | ||
"version": "0.0.31", | ||
"repository": "Rich-Harris/code-red", | ||
@@ -6,0 +6,0 @@ "main": "dist/code-red.js", |
@@ -6,1 +6,3 @@ import { Property, Node, Expression } from 'estree'; | ||
export { print } from './print/index'; | ||
export declare const parse: (source: string, opts: any) => any; | ||
export declare const parseExpressionAt: (source: string, index: number, opts: any) => 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
83489
11
2884