Remove duplicated getStatementParent and refactor requires to imports in tests (#5746)

* Additional testcase and require->import refactorings

* Removed duplicated getStatementParent function. Refactored all babel-traverse tests to use ESmodules
This commit is contained in:
Mauro Bringolf 2017-05-31 21:33:25 +02:00 committed by Henry Zhu
parent a6273a92ec
commit c142bbc429
7 changed files with 42 additions and 37 deletions

View File

@ -42,11 +42,20 @@ export function getFunctionParent() : ?NodePath {
export function getStatementParent() : NodePath {
let path = this;
do {
if (Array.isArray(path.container)) {
return path;
if (!path.parentPath || (Array.isArray(path.container) && path.isStatement())) {
break;
} else {
path = path.parentPath;
}
} while (path = path.parentPath);
} while (path);
if (path && (path.isProgram() || path.isFile())) {
throw new Error("File/Program node, we can't possibly find a statement parent to this");
}
return path;
}
/**

View File

@ -4,24 +4,6 @@ import type TraversalContext from "../index";
import NodePath from "./index";
import * as t from "babel-types";
export function getStatementParent(): ?NodePath {
let path = this;
do {
if (!path.parentPath || (Array.isArray(path.container) && path.isStatement())) {
break;
} else {
path = path.parentPath;
}
} while (path);
if (path && (path.isProgram() || path.isFile())) {
throw new Error("File/Program node, we can't possibly find a statement parent to this");
}
return path;
}
export function getOpposite() : ?NodePath {
if (this.key === "left") {
return this.getSibling("right");

View File

@ -1,6 +1,7 @@
const traverse = require("../lib").default;
const assert = require("assert");
const parse = require("babylon").parse;
import traverse from "../lib";
import assert from "assert";
import { parse } from "babylon";
import { expect } from "chai";
describe("path/ancestry", function () {
describe("isAncestor", function () {
@ -62,4 +63,17 @@ describe("path/ancestry", function () {
assert(!numberPath.isDescendant(stringPath));
});
});
describe("getStatementParent", function () {
const ast = parse("var a = 1;");
it("should throw", function () {
expect(function () {
traverse(ast, {
Program(path) {
path.getStatementParent();
},
});
}).to.throw(/File\/Program node/);
});
});
});

View File

@ -1,6 +1,6 @@
const traverse = require("../lib").default;
const assert = require("assert");
const parse = require("babylon").parse;
import traverse from "../lib";
import assert from "assert";
import { parse } from "babylon";
function getPath(code) {
const ast = parse(code);

View File

@ -1,6 +1,6 @@
const traverse = require("../lib").default;
const assert = require("assert");
const parse = require("babylon").parse;
import traverse from "../lib";
import assert from "assert";
import { parse } from "babylon";
describe("path/family", function () {
describe("getBindingIdentifiers", function () {

View File

@ -1,6 +1,6 @@
const traverse = require("../lib").default;
const assert = require("assert");
const parse = require("babylon").parse;
import traverse from "../lib";
import assert from "assert";
import { parse } from "babylon";
function getPath(code) {
const ast = parse(code);

View File

@ -1,7 +1,7 @@
const cloneDeep = require("lodash/cloneDeep");
const traverse = require("../lib").default;
const assert = require("assert");
const parse = require("babylon").parse;
import cloneDeep from "lodash/cloneDeep";
import traverse from "../lib";
import assert from "assert";
import { parse } from "babylon";
describe("traverse", function () {
const code = `