Attempt to replace the condition of if or hook immediately that is a
reference to a name that is assigned immediately before.
553372
private void tryOptimizeConditionalAfterAssign(Node n) {
    Node next = n.getNext();

    // Look for patterns like the following and replace the if-condition with
    // a constant value so it can later be folded:
    //   var a = /a/;
    //   if (a) {foo(a)}
    // or
    //   a = 0;
    //   a ? foo(a) : c;
    // or
    //   a = 0;
    //   a || foo(a);
    // or
    //   a = 0;
    //   a && foo(a)
    //
    // TODO(johnlenz): This would be better handled by control-flow sensitive
    // constant propagation. As the other case that I want to handle is:
    //   i=0; for(;i<0;i++){}
    // as right now nothing facilitates removing a loop like that.
    // This is here simply to remove the cruft left behind goog.userAgent and
    // similar cases.

    if (isSimpleAssignment(n) && isConditionalStatement(next)) {
      Node lhsAssign = getSimpleAssignmentName(n);

      Node condition = getConditionalStatementCondition(next);
      if (lhsAssign.isName() && condition.isName()
          && lhsAssign.getString().equals(condition.getString())) {
        Node rhsAssign = getSimpleAssignmentValue(n);
        TernaryValue value = NodeUtil.getImpureBooleanValue(rhsAssign);
        if (value != TernaryValue.UNKNOWN) {
          Node replacementConditionNode =
              NodeUtil.booleanNode(value.toBoolean(true));
          condition.replaceWith(replacementConditionNode);
          reportChangeToEnclosingScope(replacementConditionNode);
        }
      }
    }
  }
347868
def fix_reference_name(name, blacklist=None):
    """"""
    name = "".join(re.split(r'[^0-9a-zA-Z_]', name))
    while name and not re.match(r'([a-zA-Z]+[0-9a-zA-Z_]*)$', name):
        if not re.match(r'[a-zA-Z]', name[0]):
            name = name[1:]
            continue
    name = str(name)
    if not name:
        name = "data"
    if blacklist is not None and name in blacklist:
        get_new_name = lambda index: name+('%03d' % index)
        index = 0
        while get_new_name(index) in blacklist:
            index += 1
        name = get_new_name(index)
    return name
729901
public void addChange(String name, Enum<?> before, Enum<?> after) {
        String beforeStr = before != null ? before.name() : null;
        String afterStr = after != null ? after.name() : null;
        addChange(name, beforeStr, afterStr);
    }
520249
function handleNameUpdate(newFilenameInputEl, fileId) {
        var newName = newFilenameInputEl.value,
            origExtension;

        if (newName !== undefined && qq.trimStr(newName).length > 0) {
            origExtension = getOriginalExtension(fileId);

            if (origExtension !== undefined) {
                newName = newName + "." + origExtension;
            }

            spec.onSetName(fileId, newName);
        }

        spec.onEditingStatusChange(fileId, false);
    }
422725
def trigger(self, name, *a, **ka):
        '''  '''
        hooks = self.hooks[name]
        if ka.pop('reversed', False): hooks = hooks[::-1]
        return [hook(*a, **ka) for hook in hooks]
1034412
def immediate(self, name, value):
        
        setattr(self, name, value)
        self._all.add(name)
449878
function(oldName, newName) {
    var change = false;
    for (var i = 0; i < this.arguments_.length; i++) {
      if (Blockly.Names.equals(oldName, this.arguments_[i])) {
        this.arguments_[i] = newName;
        change = true;
      }
    }
    if (change) {
      this.updateParams_();
      // Update the mutator's variables if the mutator is open.
      if (this.mutator.isVisible()) {
        var blocks = this.mutator.workspace_.getAllBlocks();
        for (var i = 0, block; block = blocks[i]; i++) {
          if (block.type == 'procedures_mutatorarg' &&
              Blockly.Names.equals(oldName, block.getFieldValue('NAME'))) {
            block.setFieldValue(newName, 'NAME');
          }
        }
      }
    }
  }
678022
def bump_context(self, name):
        """"""
        data = self._context(name)
        data["priority"] = self._next_priority
        self._flush_tools()
1192878
function(name, action, context) {
        if (!name || !action) {
            return;
        }
        name = 'before' + name;
        return this.on(name, action, context);
    }
685514
function parsePair(name, value, pos, isOption) {
    if (!name || !value || !name.length || !value.length) {
        return new Issue('E050', pos);
    }

    var nameRegex = /^[a-zA-Z0-9-_]+$/;
    if (!nameRegex.test(name)) {
        return new Issue('E051', pos, {name: name});
    }

    // Strip quotes and replace single quotes with double quotes
    var squote = '\'', dquote = '"'; // Single and double quote, for sanity
    if (value[0] === squote  ||  value[0] === dquote) {
        value = value.substr(1, value.length - 2);
    }
    value = value.replace(/\'/g, dquote);

    // Treat _ and - interchangeably
    name = name.replace(/_/g, '-');

    // check if our value is for a preset.
    if (name === 'preset') {
        if (value !== '$previous' && !presets.presets[value]) {
            return new Issue('E052', pos, {preset: value});
        } else {
            return { type: 'preset', value: value };
        }
    }

    // it's not a preset.
    var parsed = null;
    if (value === '$previous') {
        parsed = '$previous';
    } else if (value[0] === '$') {
        var vs = value.substr(1);
        if (!presets.presets[vs]) {
            return new Issue('E052', pos, {preset: vs});
        }
        parsed = presets.presets[vs][name];
    } else {
        if (!isOption(name)) {
            return new Issue('E054', pos, {name: name});
        }
        try {
            parsed = JSON.parse(value);
        } catch (e) {
            if (!nameRegex.test(value)) {
                return new Issue('E053', pos, {rule: name, value: value});
            }
            parsed = value;
        }
    }

    return { type: 'rule', name: name, value: parsed };
}
762459
def add_if_unique(self, name):
        
        with self.lock:
            if name not in self.names:
                self.names.append(name)
                return True
        return False
523423
def _try_then(self):
        """"""
        if self._cached is not None and self._callback is not None:
            self._callback(*self._cached[0], **self._cached[1])
704896
private Node tryReplaceIf(Node n) {

    Node next = null;
    for (Node child = n.getFirstChild();
         child != null; child = next){
      next = child.getNext();
      if (child.isIf()){
        Node cond = child.getFirstChild();
        Node thenBranch = cond.getNext();
        Node elseBranch = thenBranch.getNext();
        Node nextNode = child.getNext();

        if (nextNode != null && elseBranch == null
            && isReturnBlock(thenBranch)
            && nextNode.isIf()) {
          Node nextCond = nextNode.getFirstChild();
          Node nextThen = nextCond.getNext();
          Node nextElse = nextThen.getNext();
          if (thenBranch.isEquivalentToTyped(nextThen)) {
            // Transform
            //   if (x) return 1; if (y) return 1;
            // to
            //   if (x||y) return 1;
            child.detach();
            child.detachChildren();
            Node newCond = new Node(Token.OR, cond);
            nextNode.replaceChild(nextCond, newCond);
            newCond.addChildToBack(nextCond);
            reportChangeToEnclosingScope(newCond);
          } else if (nextElse != null
              && thenBranch.isEquivalentToTyped(nextElse)) {
            // Transform
            //   if (x) return 1; if (y) foo() else return 1;
            // to
            //   if (!x&&y) foo() else return 1;
            child.detach();
            child.detachChildren();
            Node newCond = new Node(Token.AND,
                IR.not(cond).srcref(cond));
            nextNode.replaceChild(nextCond, newCond);
            newCond.addChildToBack(nextCond);
            reportChangeToEnclosingScope(newCond);
          }
        } else if (nextNode != null && elseBranch == null
            && isReturnBlock(thenBranch) && isReturnExpression(nextNode)) {
          Node thenExpr = null;
          // if(x)return; return 1 -> return x?void 0:1
          if (isReturnExpressBlock(thenBranch)) {
            thenExpr = getBlockReturnExpression(thenBranch);
            thenExpr.detach();
          } else {
            thenExpr = NodeUtil.newUndefinedNode(child);
          }

          Node elseExpr = nextNode.getFirstChild();

          cond.detach();
          elseExpr.detach();

          Node returnNode = IR.returnNode(
                                IR.hook(cond, thenExpr, elseExpr)
                                    .srcref(child));
          n.replaceChild(child, returnNode);
          n.removeChild(nextNode);
          reportChangeToEnclosingScope(n);
          // everything else in the block is dead code.
          break;
        } else if (elseBranch != null && statementMustExitParent(thenBranch)) {
          child.removeChild(elseBranch);
          n.addChildAfter(elseBranch, child);
          reportChangeToEnclosingScope(n);
        }
      }
    }
    return n;
  }
781082
public void replace( Token original , Token target  ) {
        if( first == original )
            first = target;
        if( last == original )
            last = target;

        target.next = original.next;
        target.previous = original.previous;

        if( original.next != null )
            original.next.previous = target;
        if( original.previous != null )
            original.previous.next = target;

        original.next = original.previous = null;
    }
1147381
private void beforeValue() throws JSONException {
        if (stack.isEmpty()) {
            return;
        }

        Scope context = peek();
        if (context == Scope.EMPTY_ARRAY) { // first in array
            replaceTop(Scope.NONEMPTY_ARRAY);
            newline();
        } else if (context == Scope.NONEMPTY_ARRAY) { // another in array
            out.append(',');
            newline();
        } else if (context == Scope.DANGLING_KEY) { // value for key
            out.append(indent == null ? ":" : ": ");
            replaceTop(Scope.NONEMPTY_OBJECT);
        } else if (context != Scope.NULL) {
            throw new JSONException("Nesting problem");
        }
    }
1091792
def _hooks_apply_before_serialize(
        hooks,  # type: Optional[Hooks]
        state,  # type: _ProcessorState
        value  # type: Any
):
    # type: (...) -> Any
    """"""
    if hooks and hooks.before_serialize:
        return hooks.before_serialize(ProcessorStateView(state), value)

    return value
877602
function isForInRef(ref) {
            let target = ref.identifier.parent;


            // "for (var ...) { return; }"
            if (target.type === "VariableDeclarator") {
                target = target.parent.parent;
            }

            if (target.type !== "ForInStatement") {
                return false;
            }

            // "for (...) { return; }"
            if (target.body.type === "BlockStatement") {
                target = target.body.body[0];

            // "for (...) return;"
            } else {
                target = target.body;
            }

            // For empty loop body
            if (!target) {
                return false;
            }

            return target.type === "ReturnStatement";
        }