Comparing version 0.17.6 to 0.18.0
@@ -5,8 +5,15 @@ "use strict"; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var assert_1 = __importDefault(require("assert")); | ||
var types_1 = __importDefault(require("./types")); | ||
var n = types_1.default.namedTypes; | ||
var isArray = types_1.default.builtInTypes.array; | ||
var isObject = types_1.default.builtInTypes.object; | ||
var types = __importStar(require("ast-types")); | ||
var n = types.namedTypes; | ||
var isArray = types.builtInTypes.array; | ||
var isObject = types.builtInTypes.object; | ||
var lines_1 = require("./lines"); | ||
@@ -50,3 +57,3 @@ var util_1 = require("./util"); | ||
else if (isObject.check(node)) { | ||
names = types_1.default.getFieldNames(node); | ||
names = types.getFieldNames(node); | ||
} | ||
@@ -280,3 +287,3 @@ else { | ||
var comments = n.Node.check(value) && | ||
types_1.default.getFieldValue(value, "comments"); | ||
types.getFieldValue(value, "comments"); | ||
if (!comments || comments.length === 0) { | ||
@@ -289,4 +296,4 @@ return innerLines; | ||
var comment = commentPath.getValue(); | ||
var leading = types_1.default.getFieldValue(comment, "leading"); | ||
var trailing = types_1.default.getFieldValue(comment, "trailing"); | ||
var leading = types.getFieldValue(comment, "leading"); | ||
var trailing = types.getFieldValue(comment, "trailing"); | ||
if (leading || (trailing && !(n.Statement.check(value) || | ||
@@ -293,0 +300,0 @@ comment.type === "Block" || |
@@ -1,2 +0,2 @@ | ||
"use strict";; | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -14,6 +14,6 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var assert_1 = __importDefault(require("assert")); | ||
var types_1 = __importDefault(require("./types")); | ||
var n = types_1.default.namedTypes; | ||
var isArray = types_1.default.builtInTypes.array; | ||
var isNumber = types_1.default.builtInTypes.number; | ||
var types = __importStar(require("ast-types")); | ||
var n = types.namedTypes; | ||
var isArray = types.builtInTypes.array; | ||
var isNumber = types.builtInTypes.number; | ||
var util = __importStar(require("./util")); | ||
@@ -31,3 +31,3 @@ var FastPath = function FastPath(value) { | ||
} | ||
if (obj instanceof types_1.default.NodePath) { | ||
if (obj instanceof types.NodePath) { | ||
// For backwards compatibility, unroll NodePath instances into | ||
@@ -451,3 +451,3 @@ // lightweight FastPath [..., name, value] stacks. | ||
if (n.Node.check(node)) { | ||
return types_1.default.someField(node, function (_name, child) { | ||
return types.someField(node, function (_name, child) { | ||
return containsCallExpression(child); | ||
@@ -543,2 +543,1 @@ }); | ||
exports.default = FastPath; | ||
module.exports = exports["default"]; |
import { Options } from "./options"; | ||
import { Position as Pos } from "./types"; | ||
import { namedTypes } from "ast-types"; | ||
declare type Pos = namedTypes.Position; | ||
declare type LineInfo = { | ||
@@ -42,3 +43,3 @@ readonly line: string; | ||
}; | ||
skipSpaces(pos: Pos, backward?: boolean, modifyInPlace?: boolean): Pos | null; | ||
skipSpaces(pos: Pos, backward?: boolean, modifyInPlace?: boolean): namedTypes.Position | null; | ||
trimLeft(): Lines; | ||
@@ -45,0 +46,0 @@ trimRight(): Lines; |
@@ -1,3 +0,5 @@ | ||
import { SourceLocation as Loc, Position as Pos } from "./types"; | ||
import { namedTypes } from "ast-types"; | ||
import { Lines } from "./lines"; | ||
declare type Pos = namedTypes.Position; | ||
declare type Loc = namedTypes.SourceLocation; | ||
export default class Mapping { | ||
@@ -13,1 +15,2 @@ sourceLines: Lines; | ||
} | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
"use strict";; | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -203,2 +203,1 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
module.exports = exports["default"]; |
@@ -14,6 +14,6 @@ "use strict"; | ||
var assert_1 = __importDefault(require("assert")); | ||
var types_1 = __importDefault(require("./types")); | ||
var b = types_1.default.builders; | ||
var isObject = types_1.default.builtInTypes.object; | ||
var isArray = types_1.default.builtInTypes.array; | ||
var types = __importStar(require("ast-types")); | ||
var b = types.builders; | ||
var isObject = types.builtInTypes.object; | ||
var isArray = types.builtInTypes.array; | ||
var options_1 = require("./options"); | ||
@@ -20,0 +20,0 @@ var lines_1 = require("./lines"); |
@@ -15,12 +15,12 @@ "use strict"; | ||
var linesModule = __importStar(require("./lines")); | ||
var types_1 = __importDefault(require("./types")); | ||
var Printable = types_1.default.namedTypes.Printable; | ||
var Expression = types_1.default.namedTypes.Expression; | ||
var ReturnStatement = types_1.default.namedTypes.ReturnStatement; | ||
var SourceLocation = types_1.default.namedTypes.SourceLocation; | ||
var types = __importStar(require("ast-types")); | ||
var Printable = types.namedTypes.Printable; | ||
var Expression = types.namedTypes.Expression; | ||
var ReturnStatement = types.namedTypes.ReturnStatement; | ||
var SourceLocation = types.namedTypes.SourceLocation; | ||
var util_1 = require("./util"); | ||
var fast_path_1 = __importDefault(require("./fast-path")); | ||
var isObject = types_1.default.builtInTypes.object; | ||
var isArray = types_1.default.builtInTypes.array; | ||
var isString = types_1.default.builtInTypes.string; | ||
var isObject = types.builtInTypes.object; | ||
var isArray = types.builtInTypes.array; | ||
var isString = types.builtInTypes.string; | ||
var riskyAdjoiningCharExp = /[0-9a-z_$]/i; | ||
@@ -376,4 +376,4 @@ var Patcher = function Patcher(lines) { | ||
} | ||
newPath.stack.push(k, types_1.default.getFieldValue(newNode, k)); | ||
oldPath.stack.push(k, types_1.default.getFieldValue(oldNode, k)); | ||
newPath.stack.push(k, types.getFieldValue(newNode, k)); | ||
oldPath.stack.push(k, types.getFieldValue(oldNode, k)); | ||
var canReprint = findAnyReprints(newPath, oldPath, reprints); | ||
@@ -380,0 +380,0 @@ newPath.stack.length -= 2; |
@@ -5,6 +5,13 @@ "use strict"; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var assert_1 = __importDefault(require("assert")); | ||
var types_1 = __importDefault(require("./types")); | ||
var n = types_1.default.namedTypes; | ||
var types = __importStar(require("ast-types")); | ||
var n = types.namedTypes; | ||
var source_map_1 = __importDefault(require("source-map")); | ||
@@ -11,0 +18,0 @@ var SourceMapConsumer = source_map_1.default.SourceMapConsumer; |
@@ -1,27 +0,50 @@ | ||
import types, { ASTNode } from "./lib/types"; | ||
import * as types from "ast-types"; | ||
import { parse } from "./lib/parser"; | ||
import { Options } from "./lib/options"; | ||
export interface Parser { | ||
parse(source: string, options?: any): ASTNode; | ||
export { | ||
/** | ||
* Parse a string of code into an augmented syntax tree suitable for | ||
* arbitrary modification and reprinting. | ||
*/ | ||
parse, | ||
/** | ||
* Convenient shorthand for the ast-types package. | ||
*/ | ||
types, }; | ||
/** | ||
* Traverse and potentially modify an abstract syntax tree using a | ||
* convenient visitor syntax: | ||
* | ||
* recast.visit(ast, { | ||
* names: [], | ||
* visitIdentifier: function(path) { | ||
* var node = path.value; | ||
* this.visitor.names.push(node.name); | ||
* this.traverse(path); | ||
* } | ||
* }); | ||
*/ | ||
export { visit } from "ast-types"; | ||
/** | ||
* Options shared between parsing and printing. | ||
*/ | ||
export { Options } from "./lib/options"; | ||
/** | ||
* Reprint a modified syntax tree using as much of the original source | ||
* code as possible. | ||
*/ | ||
export declare function print(node: types.ASTNode, options?: Options): import("./lib/printer").PrintResultType; | ||
/** | ||
* Print without attempting to reuse any original source code. | ||
*/ | ||
export declare function prettyPrint(node: types.ASTNode, options?: Options): import("./lib/printer").PrintResultType; | ||
/** | ||
* Convenient command-line interface (see e.g. example/add-braces). | ||
*/ | ||
export declare function run(transformer: Transformer, options?: RunOptions): void; | ||
export interface Transformer { | ||
(ast: types.ASTNode, callback: (ast: types.ASTNode) => void): void; | ||
} | ||
declare function print(node: ASTNode, options?: Options): import("./lib/printer").PrintResultType; | ||
declare function prettyPrint(node: ASTNode, options?: Options): import("./lib/printer").PrintResultType; | ||
interface Transformer { | ||
(ast: ASTNode, callback: (ast: ASTNode) => void): void; | ||
} | ||
interface RunOptions extends Options { | ||
export interface RunOptions extends Options { | ||
writeback?(code: string): void; | ||
} | ||
declare function run(transformer: Transformer, options?: RunOptions): void; | ||
interface Main { | ||
parse: typeof parse; | ||
visit: typeof types.visit; | ||
print: typeof print; | ||
prettyPrint: typeof prettyPrint; | ||
types: typeof types; | ||
run: typeof run; | ||
} | ||
declare const main: Main; | ||
export default main; | ||
export { ASTNode, NamedTypes, Builders, NodePath, Type, } from "./lib/types"; | ||
export { Options } from "./lib/options"; |
100
main.js
@@ -1,19 +0,56 @@ | ||
"use strict";; | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var fs_1 = __importDefault(require("fs")); | ||
var types_1 = __importDefault(require("./lib/types")); | ||
var types = __importStar(require("ast-types")); | ||
exports.types = types; | ||
var parser_1 = require("./lib/parser"); | ||
exports.parse = parser_1.parse; | ||
var printer_1 = require("./lib/printer"); | ||
/** | ||
* Traverse and potentially modify an abstract syntax tree using a | ||
* convenient visitor syntax: | ||
* | ||
* recast.visit(ast, { | ||
* names: [], | ||
* visitIdentifier: function(path) { | ||
* var node = path.value; | ||
* this.visitor.names.push(node.name); | ||
* this.traverse(path); | ||
* } | ||
* }); | ||
*/ | ||
var ast_types_1 = require("ast-types"); | ||
exports.visit = ast_types_1.visit; | ||
/** | ||
* Reprint a modified syntax tree using as much of the original source | ||
* code as possible. | ||
*/ | ||
function print(node, options) { | ||
return new printer_1.Printer(options).print(node); | ||
} | ||
exports.print = print; | ||
/** | ||
* Print without attempting to reuse any original source code. | ||
*/ | ||
function prettyPrint(node, options) { | ||
return new printer_1.Printer(options).printGenerically(node); | ||
} | ||
exports.prettyPrint = prettyPrint; | ||
/** | ||
* Convenient command-line interface (see e.g. example/add-braces). | ||
*/ | ||
function run(transformer, options) { | ||
return runFile(process.argv[2], transformer, options); | ||
} | ||
exports.run = run; | ||
function runFile(path, transformer, options) { | ||
@@ -37,60 +74,1 @@ fs_1.default.readFile(path, "utf-8", function (err, code) { | ||
} | ||
var main = {}; | ||
Object.defineProperties(main, { | ||
/** | ||
* Parse a string of code into an augmented syntax tree suitable for | ||
* arbitrary modification and reprinting. | ||
*/ | ||
parse: { | ||
enumerable: true, | ||
value: parser_1.parse | ||
}, | ||
/** | ||
* Traverse and potentially modify an abstract syntax tree using a | ||
* convenient visitor syntax: | ||
* | ||
* recast.visit(ast, { | ||
* names: [], | ||
* visitIdentifier: function(path) { | ||
* var node = path.value; | ||
* this.visitor.names.push(node.name); | ||
* this.traverse(path); | ||
* } | ||
* }); | ||
*/ | ||
visit: { | ||
enumerable: true, | ||
value: types_1.default.visit | ||
}, | ||
/** | ||
* Reprint a modified syntax tree using as much of the original source | ||
* code as possible. | ||
*/ | ||
print: { | ||
enumerable: true, | ||
value: print | ||
}, | ||
/** | ||
* Print without attempting to reuse any original source code. | ||
*/ | ||
prettyPrint: { | ||
enumerable: false, | ||
value: prettyPrint | ||
}, | ||
/** | ||
* Customized version of require("ast-types"). | ||
*/ | ||
types: { | ||
enumerable: false, | ||
value: types_1.default | ||
}, | ||
/** | ||
* Convenient command-line interface (see e.g. example/add-braces). | ||
*/ | ||
run: { | ||
enumerable: false, | ||
value: run | ||
} | ||
}); | ||
exports.default = main; | ||
module.exports = exports["default"]; |
{ | ||
"author": "Ben Newman <bn@cs.stanford.edu>", | ||
"name": "recast", | ||
"version": "0.17.6", | ||
"version": "0.18.0", | ||
"description": "JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator", | ||
@@ -25,9 +25,8 @@ "keywords": [ | ||
"scripts": { | ||
"tsc": "tsc --noEmit", | ||
"mocha": "test/run.sh", | ||
"debug": "test/run.sh --inspect-brk", | ||
"test": "npm run tsc && npm run mocha", | ||
"build": "tsc && ts-add-module-exports", | ||
"test": "npm run build && npm run mocha", | ||
"build": "npm run clean && tsc", | ||
"clean": "ts-emit-clean", | ||
"prepack": "npm run clean && npm run build", | ||
"prepack": "npm run build", | ||
"postpack": "npm run clean" | ||
@@ -39,3 +38,3 @@ }, | ||
"dependencies": { | ||
"ast-types": "0.12.4", | ||
"ast-types": "0.13.0", | ||
"esprima": "~4.0.0", | ||
@@ -46,18 +45,17 @@ "private": "^0.1.8", | ||
"devDependencies": { | ||
"@babel/core": "^7.4.0", | ||
"@babel/parser": "^7.4.2", | ||
"@babel/preset-env": "^7.4.2", | ||
"@types/esprima": "^4.0.2", | ||
"@types/glob": "^7.1.1", | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.12.21", | ||
"esprima-fb": "^15001.1001.0-dev-harmony-fb", | ||
"flow-parser": "^0.95.1", | ||
"glob": "^7.1.3", | ||
"mocha": "^6.0.2", | ||
"reify": "^0.18.1", | ||
"ts-add-module-exports": "^1.0.0", | ||
"ts-emit-clean": "^1.0.0", | ||
"ts-node": "^7.0.1", | ||
"typescript": "^3.3.4000" | ||
"@babel/core": "7.4.4", | ||
"@babel/parser": "7.4.4", | ||
"@babel/preset-env": "7.4.4", | ||
"@types/esprima": "4.0.2", | ||
"@types/glob": "7.1.1", | ||
"@types/mocha": "5.2.6", | ||
"@types/node": "12.0.0", | ||
"esprima-fb": "15001.1001.0-dev-harmony-fb", | ||
"flow-parser": "0.98.1", | ||
"glob": "7.1.4", | ||
"mocha": "6.1.4", | ||
"reify": "0.18.1", | ||
"ts-emit-clean": "1.0.0", | ||
"ts-node": "7.0.1", | ||
"typescript": "3.4.5" | ||
}, | ||
@@ -64,0 +62,0 @@ "engines": { |
@@ -1,2 +0,2 @@ | ||
"use strict";; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -42,2 +42,2 @@ var util_1 = require("../lib/util"); | ||
exports.default = getBabelOptions; | ||
module.exports = exports["default"]; | ||
; |
@@ -22,22 +22,43 @@ # recast, _v_. [![Build Status](https://travis-ci.org/benjamn/recast.svg?branch=master)](https://travis-ci.org/benjamn/recast) [![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) [![Greenkeeper badge](https://badges.greenkeeper.io/benjamn/recast.svg)](https://greenkeeper.io/) | ||
Import style | ||
--- | ||
Recast is designed to be imported using **named** imports: | ||
```js | ||
import { parse, print } from "recast"; | ||
console.log(print(parse(source)).code); | ||
import * as recast from "recast"; | ||
console.log(recast.print(recast.parse(source)).code); | ||
``` | ||
If you're using CommonJS: | ||
```js | ||
const { parse, print } = require("recast"); | ||
console.log(print(parse(source)).code); | ||
const recast = require("recast"); | ||
console.log(recast.print(recast.parse(source)).code); | ||
``` | ||
Usage | ||
--- | ||
In less poetic terms, Recast exposes two essential interfaces, one for parsing JavaScript code (`require("recast").parse`) and the other for reprinting modified syntax trees (`require("recast").print`). | ||
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 | ||
var recast = require("recast"); | ||
import * as recast from "recast"; | ||
// Let's turn this function declaration into a variable declaration. | ||
var code = [ | ||
"function add(a, b) {", | ||
" return a +", | ||
" // Weird formatting, huh?", | ||
" b;", | ||
"}" | ||
const code = [ | ||
"function add(a, b) {", | ||
" return a +", | ||
" // Weird formatting, huh?", | ||
" b;", | ||
"}" | ||
].join("\n"); | ||
// Parse the code using an interface similar to require("esprima").parse. | ||
var ast = recast.parse(code); | ||
const ast = recast.parse(code); | ||
``` | ||
@@ -49,6 +70,6 @@ Now do *whatever* you want to `ast`. Really, anything at all! | ||
// Grab a reference to the function declaration we just parsed. | ||
var add = ast.program.body[0]; | ||
const add = ast.program.body[0]; | ||
// Make sure it's a FunctionDeclaration (optional). | ||
var n = recast.types.namedTypes; | ||
const n = recast.types.namedTypes; | ||
n.FunctionDeclaration.assert(add); | ||
@@ -58,3 +79,3 @@ | ||
// arguments will be dynamically type-checked against the Mozilla Parser API. | ||
var b = recast.types.builders; | ||
const b = recast.types.builders; | ||
@@ -64,7 +85,7 @@ // This kind of manipulation should seem familiar if you've used Esprima or the | ||
ast.program.body[0] = b.variableDeclaration("var", [ | ||
b.variableDeclarator(add.id, b.functionExpression( | ||
null, // Anonymize the function expression. | ||
add.params, | ||
add.body | ||
)) | ||
b.variableDeclarator(add.id, b.functionExpression( | ||
null, // Anonymize the function expression. | ||
add.params, | ||
add.body | ||
)) | ||
]); | ||
@@ -77,3 +98,3 @@ | ||
```js | ||
var output = recast.print(ast).code; | ||
const output = recast.print(ast).code; | ||
``` | ||
@@ -80,0 +101,0 @@ The `output` string now looks exactly like this, weird formatting and all: |
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
15
209
231452
44
5634
+ Addedast-types@0.13.0(transitive)
- Removedast-types@0.12.4(transitive)
Updatedast-types@0.13.0