Comparing version 0.21.0 to 0.21.1
@@ -11,2 +11,4 @@ module.exports = { | ||
printWidth: 60, | ||
// Don't reformat code examples in README | ||
embeddedLanguageFormatting: "off", | ||
}, | ||
@@ -13,0 +15,0 @@ }, |
@@ -12,2 +12,3 @@ "use strict"; | ||
[ | ||
["??"], | ||
["||"], | ||
@@ -265,5 +266,2 @@ ["&&"], | ||
var parent = this.getParentNode(); | ||
if (!parent) { | ||
return false; | ||
} | ||
var name = this.getName(); | ||
@@ -284,6 +282,10 @@ // If the value of this path is some child of a Node and not a Node | ||
} | ||
if (parent.type === "ParenthesizedExpression" || | ||
(node.extra && node.extra.parenthesized)) { | ||
if (parent && parent.type === "ParenthesizedExpression") { | ||
return false; | ||
} | ||
if (node.extra && node.extra.parenthesized) { | ||
return true; | ||
} | ||
if (!parent) | ||
return false; | ||
switch (node.type) { | ||
@@ -500,4 +502,6 @@ case "UnaryExpression": | ||
} | ||
// s[i + 1] and s[i + 2] represent the array between the parent | ||
// SequenceExpression node and its child nodes | ||
if (n.SequenceExpression.check(parent) && | ||
parentName === "expressions" && | ||
s[i + 1] === "expressions" && | ||
childName === 0) { | ||
@@ -504,0 +508,0 @@ assert_1.default.strictEqual(parent.expressions[0], child); |
@@ -61,3 +61,3 @@ "use strict"; | ||
function runString(code, transformer, options) { | ||
var writeback = options && options.writeback || defaultWriteback; | ||
var writeback = (options && options.writeback) || defaultWriteback; | ||
transformer(parser_1.parse(code, options), function (node) { | ||
@@ -64,0 +64,0 @@ writeback(print(node, options).code); |
{ | ||
"author": "Ben Newman <bn@cs.stanford.edu>", | ||
"name": "recast", | ||
"version": "0.21.0", | ||
"version": "0.21.1", | ||
"description": "JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator", | ||
@@ -30,3 +30,3 @@ "keywords": [ | ||
"lint": "eslint --ext .ts .", | ||
"format": "prettier --write '{lib,test}/**.ts' '*rc.js'", | ||
"format": "prettier --write .", | ||
"clean": "ts-emit-clean", | ||
@@ -54,4 +54,4 @@ "prepack": "npm run build", | ||
"@babel/parser": "7.14.8", | ||
"@babel/preset-env": "7.12.13", | ||
"@types/esprima": "4.0.2", | ||
"@babel/preset-env": "7.16.11", | ||
"@types/esprima": "4.0.3", | ||
"@types/glob": "7.2.0", | ||
@@ -65,5 +65,5 @@ "@types/mocha": "8.2.0", | ||
"glob": "7.2.0", | ||
"lint-staged": "^10.2.6", | ||
"lint-staged": "^12.4.1", | ||
"mocha": "9.0.2", | ||
"prettier": "^2.0.5", | ||
"prettier": "^2.6.2", | ||
"reify": "0.20.12", | ||
@@ -70,0 +70,0 @@ "ts-emit-clean": "1.0.0", |
@@ -39,15 +39,20 @@ "use strict"; | ||
"optionalChaining", | ||
["pipelineOperator", { | ||
[ | ||
"pipelineOperator", | ||
{ | ||
proposal: "minimal", | ||
}], | ||
["recordAndTuple", { | ||
}, | ||
], | ||
[ | ||
"recordAndTuple", | ||
{ | ||
syntaxType: "hash", | ||
}], | ||
}, | ||
], | ||
"throwExpressions", | ||
"topLevelAwait", | ||
"v8intrinsic", | ||
] | ||
], | ||
}; | ||
} | ||
exports.default = getBabelOptions; | ||
; |
@@ -34,2 +34,1 @@ "use strict"; | ||
exports.parse = parse; | ||
; |
@@ -8,3 +8,3 @@ "use strict"; | ||
// that's what's available. | ||
exports.parser = function () { | ||
exports.parser = (function () { | ||
try { | ||
@@ -16,3 +16,3 @@ return require("@babel/parser"); | ||
} | ||
}(); | ||
})(); | ||
// This module is suitable for passing as options.parser when calling | ||
@@ -31,2 +31,1 @@ // recast.parse to process JavaScript code with Babel: | ||
exports.parse = parse; | ||
; |
@@ -22,3 +22,3 @@ "use strict"; | ||
tokens: true, | ||
jsx: util_1.getOption(options, "jsx", false) | ||
jsx: util_1.getOption(options, "jsx", false), | ||
}); | ||
@@ -31,2 +31,1 @@ if (!Array.isArray(ast.comments)) { | ||
exports.parse = parse; | ||
; |
@@ -20,2 +20,1 @@ "use strict"; | ||
exports.parse = parse; | ||
; |
@@ -20,2 +20,1 @@ "use strict"; | ||
exports.parse = parse; | ||
; |
@@ -8,9 +8,8 @@ # recast, _v_. ![CI](https://github.com/benjamn/recast/workflows/CI/badge.svg) [![Join the chat at https://gitter.im/benjamn/recast](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/benjamn/recast?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
Installation | ||
--- | ||
## Installation | ||
From NPM: | ||
From npm: | ||
npm install recast | ||
From GitHub: | ||
@@ -23,6 +22,6 @@ | ||
Import style | ||
--- | ||
## Import style | ||
Recast is designed to be imported using **named** imports: | ||
```js | ||
@@ -37,2 +36,3 @@ import { parse, print } from "recast"; | ||
If you're using CommonJS: | ||
```js | ||
@@ -46,4 +46,3 @@ const { parse, print } = require("recast"); | ||
Usage | ||
--- | ||
## Usage | ||
@@ -53,2 +52,3 @@ Recast exposes two essential interfaces, one for parsing JavaScript code (`require("recast").parse`) and the other for reprinting modified syntax trees (`require("recast").print`). | ||
Here's a simple but non-trivial example of how you might use `.parse` and `.print`: | ||
```js | ||
@@ -69,5 +69,7 @@ import * as recast from "recast"; | ||
``` | ||
Now do *whatever* you want to `ast`. Really, anything at all! | ||
Now do _whatever_ you want to `ast`. Really, anything at all! | ||
See [ast-types](https://github.com/benjamn/ast-types) (especially the [def/core.ts](https://github.com/benjamn/ast-types/blob/master/def/core.ts)) module for a thorough overview of the `ast` API. | ||
```js | ||
@@ -98,7 +100,11 @@ // Grab a reference to the function declaration we just parsed. | ||
``` | ||
When you finish manipulating the AST, let `recast.print` work its magic: | ||
```js | ||
const output = recast.print(ast).code; | ||
``` | ||
The `output` string now looks exactly like this, weird formatting and all: | ||
```js | ||
@@ -111,13 +117,19 @@ var add = function(b, a) { | ||
``` | ||
The magic of Recast is that it reprints only those parts of the syntax tree that you modify. In other words, the following identity is guaranteed: | ||
```js | ||
recast.print(recast.parse(source)).code === source | ||
``` | ||
Whenever Recast cannot reprint a modified node using the original source code, it falls back to using a generic pretty printer. So the worst that can happen is that your changes trigger some harmless reformatting of your code. | ||
If you really don't care about preserving the original formatting, you can access the pretty printer directly: | ||
```js | ||
var output = recast.prettyPrint(ast, { tabWidth: 2 }).code; | ||
``` | ||
And here's the exact `output`: | ||
```js | ||
@@ -128,6 +140,6 @@ var add = function(b, a) { | ||
``` | ||
Note that the weird formatting was discarded, yet the behavior and abstract structure of the code remain the same. | ||
Using a different parser | ||
--- | ||
## Using a different parser | ||
@@ -170,5 +182,6 @@ By default, Recast uses the [Esprima JavaScript parser](https://www.npmjs.com/package/esprima) when you call `recast.parse(code)`. While Esprima supports almost all modern ECMAScript syntax, you may want to use a different parser to enable TypeScript or Flow syntax, or just because you want to match other compilation tools you might be using. | ||
Source maps | ||
--- | ||
After calling `recast.parse`, if you're going to transform the AST, make sure that the `.original` property is preserved. With Babel, for instance, if you call `transformFromAST`, you must pass `cloneInputAst: false` in its options. ([More detail](https://github.com/babel/babel/issues/12882). | ||
## Source maps | ||
One of the coolest consequences of tracking and reusing original source code during reprinting is that it's pretty easy to generate a high-resolution mapping between the original code and the generated code—completely automatically! | ||
@@ -179,2 +192,3 @@ | ||
All you have to think about is how to manipulate the syntax tree, and Recast will give you a [source map](https://github.com/mozilla/source-map) in exchange for specifying the names of your source file(s) and the desired name of the map: | ||
```js | ||
@@ -186,3 +200,3 @@ var result = recast.print(transform(recast.parse(source, { | ||
}); | ||
console.log(result.code); // Resulting string of code. | ||
@@ -206,9 +220,8 @@ console.log(result.map); // JSON source map. | ||
Options | ||
--- | ||
## Options | ||
All Recast API functions take second parameter with configuration options, documented in | ||
[options.ts](https://github.com/benjamn/recast/blob/master/lib/options.ts) | ||
Motivation | ||
--- | ||
## Motivation | ||
@@ -215,0 +228,0 @@ The more code you have, the harder it becomes to make big, sweeping changes quickly and confidently. Even if you trust yourself not to make too many mistakes, and no matter how proficient you are with your text editor, changing tens of thousands of lines of code takes precious, non-refundable time. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
238947
50
5716
222