react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / umd / node_modules / uglify-js / lib / propmangle.js
80743 views/***********************************************************************12A JavaScript tokenizer / parser / beautifier / compressor.3https://github.com/mishoo/UglifyJS245-------------------------------- (C) ---------------------------------67Author: Mihai Bazon8<[email protected]>9http://mihai.bazon.net/blog1011Distributed under the BSD license:1213Copyright 2012 (c) Mihai Bazon <[email protected]>1415Redistribution and use in source and binary forms, with or without16modification, are permitted provided that the following conditions17are met:1819* Redistributions of source code must retain the above20copyright notice, this list of conditions and the following21disclaimer.2223* Redistributions in binary form must reproduce the above24copyright notice, this list of conditions and the following25disclaimer in the documentation and/or other materials26provided with the distribution.2728THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY29EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE30IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR31PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE32LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,33OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,34PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR35PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY36THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR37TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF38THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF39SUCH DAMAGE.4041***********************************************************************/4243"use strict";4445function find_builtins() {46var a = [];47[ Object, Array, Function, Number,48String, Boolean, Error, Math,49Date, RegExp50].forEach(function(ctor){51Object.getOwnPropertyNames(ctor).map(add);52if (ctor.prototype) {53Object.getOwnPropertyNames(ctor.prototype).map(add);54}55});56function add(name) {57push_uniq(a, name);58}59return a;60}6162function mangle_properties(ast, options) {63options = defaults(options, {64reserved : null,65cache : null,66only_cache : false67});6869var reserved = options.reserved;70if (reserved == null)71reserved = find_builtins();7273var cache = options.cache;74if (cache == null) {75cache = {76cname: -1,77props: new Dictionary()78};79}8081var names_to_mangle = [];8283// step 1: find candidates to mangle84ast.walk(new TreeWalker(function(node){85if (node instanceof AST_ObjectKeyVal) {86add(node.key);87}88else if (node instanceof AST_ObjectProperty) {89// setter or getter, since KeyVal is handled above90add(node.key.name);91}92else if (node instanceof AST_Dot) {93if (this.parent() instanceof AST_Assign) {94add(node.property);95}96}97else if (node instanceof AST_Sub) {98if (this.parent() instanceof AST_Assign) {99addStrings(node.property);100}101}102}));103104// step 2: transform the tree, renaming properties105return ast.transform(new TreeTransformer(function(node){106if (node instanceof AST_ObjectKeyVal) {107if (should_mangle(node.key)) {108node.key = mangle(node.key);109}110}111else if (node instanceof AST_ObjectProperty) {112// setter or getter113if (should_mangle(node.key.name)) {114node.key.name = mangle(node.key.name);115}116}117else if (node instanceof AST_Dot) {118if (should_mangle(node.property)) {119node.property = mangle(node.property);120}121}122else if (node instanceof AST_Sub) {123node.property = mangleStrings(node.property);124}125// else if (node instanceof AST_String) {126// if (should_mangle(node.value)) {127// AST_Node.warn(128// "Found \"{prop}\" property candidate for mangling in an arbitrary string [{file}:{line},{col}]", {129// file : node.start.file,130// line : node.start.line,131// col : node.start.col,132// prop : node.value133// }134// );135// }136// }137}));138139// only function declarations after this line140141function can_mangle(name) {142if (reserved.indexOf(name) >= 0) return false;143if (options.only_cache) {144return cache.props.has(name);145}146if (/^[0-9.]+$/.test(name)) return false;147return true;148}149150function should_mangle(name) {151if (reserved.indexOf(name) >= 0) return false;152return cache.props.has(name)153|| names_to_mangle.indexOf(name) >= 0;154}155156function add(name) {157if (can_mangle(name))158push_uniq(names_to_mangle, name);159}160161function mangle(name) {162var mangled = cache.props.get(name);163if (!mangled) {164do {165mangled = base54(++cache.cname);166} while (!can_mangle(mangled));167cache.props.set(name, mangled);168}169return mangled;170}171172function addStrings(node) {173var out = {};174try {175(function walk(node){176node.walk(new TreeWalker(function(node){177if (node instanceof AST_Seq) {178walk(node.cdr);179return true;180}181if (node instanceof AST_String) {182add(node.value);183return true;184}185if (node instanceof AST_Conditional) {186walk(node.consequent);187walk(node.alternative);188return true;189}190throw out;191}));192})(node);193} catch(ex) {194if (ex !== out) throw ex;195}196}197198function mangleStrings(node) {199return node.transform(new TreeTransformer(function(node){200if (node instanceof AST_Seq) {201node.cdr = mangleStrings(node.cdr);202}203else if (node instanceof AST_String) {204if (should_mangle(node.value)) {205node.value = mangle(node.value);206}207}208else if (node instanceof AST_Conditional) {209node.consequent = mangleStrings(node.consequent);210node.alternative = mangleStrings(node.alternative);211}212return node;213}));214}215216}217218219