From 94c34e0132a851e26e605bc3a0ddd3f7f1fafdec Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 16 Jun 2015 01:53:53 +0100 Subject: [PATCH] throw error when attemping to replace a Program root node with another node not of type Program - closes #1762 --- src/babel/traversal/path/replacement.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/babel/traversal/path/replacement.js b/src/babel/traversal/path/replacement.js index d8ec9dc01c..3f425b891e 100644 --- a/src/babel/traversal/path/replacement.js +++ b/src/babel/traversal/path/replacement.js @@ -93,9 +93,13 @@ export function replaceWith(replacement, whateverAllowed) { } // normalise inserting an entire AST - if (t.isProgram(replacement)) { + if (t.isProgram(replacement) && !this.isProgram()) { replacement = replacement.body; whateverAllowed = true; + } else { + if (this.isProgram()) { + throw new Error("You can only replace a Program root node with another Program node"); + } } if (Array.isArray(replacement)) {