From e51949e402cebffa688c8daa5ef592f4585f432d Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 25 Jul 2015 00:40:39 +0100 Subject: [PATCH] better inheritance for t.inherits --- packages/babel/src/types/index.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/babel/src/types/index.js b/packages/babel/src/types/index.js index bb6fd6a698..f492b4a271 100644 --- a/packages/babel/src/types/index.js +++ b/packages/babel/src/types/index.js @@ -32,6 +32,10 @@ export const STATEMENT_OR_BLOCK_KEYS = ["consequent", "body", "alternate"]; export const FLATTENABLE_KEYS = ["body", "expressions"]; export const FOR_INIT_KEYS = ["left", "init"]; export const COMMENT_KEYS = ["leadingComments", "trailingComments", "innerComments"]; +export const INHERIT_KEYS = { + optional: ["typeAnnotation", "typeParameters", "returnType"], + force: ["_scopeInfo", "_paths", "range", "start", "loc", "end"] +}; export const BOOLEAN_NUMBER_BINARY_OPERATORS = [">", "<", ">=", "<="]; export const COMPARISON_BINARY_OPERATORS = ["==", "===", "!=", "!==", "in", "instanceof"]; @@ -328,18 +332,18 @@ export function inheritsComments(child: Object, parent: Object): Object { export function inherits(child: Object, parent: Object): Object { if (!child || !parent) return child; - child._scopeInfo = parent._scopeInfo; - child._paths = parent._paths; + for (let key of (t.INHERIT_KEYS.optional: Array)) { + if (child[key] == null) { + child[key] = parent[key]; + } + } - child.range = parent.range; - child.start = parent.start; - child.loc = parent.loc; - child.end = parent.end; - - child.typeAnnotation = parent.typeAnnotation; - child.returnType = parent.returnType; + for (let key of (t.INHERIT_KEYS.force: Array)) { + child[key] = parent[key]; + } t.inheritsComments(child, parent); + return child; }