<commit_before><?php

/*
 * Copyright 2011 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace TwigJs\Compiler\Expression;

use TwigJs\JsCompiler;
use TwigJs\TypeCompilerInterface;

class NameCompiler implements TypeCompilerInterface
{
    public function getType()
    {
        return 'Twig_Node_Expression_Name';
    }

    public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
    {
        if (!$node instanceof \Twig_Node_Expression_Name) {
            throw new \RuntimeException(sprintf('$node must be an instanceof of \Expression_Name, but got "%s".', get_class($node)));
        }

        $name = $node->getAttribute('name');

        if ($node->getAttribute('is_defined_test')) {
            if ($node->isSpecial()) {
                $compiler->repr(true);
            } else {
                $compiler->raw('(')->repr($name)->raw(' in context)');
            }
        } elseif ($node->isSpecial()) {
            static $specialVars = array(
                '_self' => 'this',
                '_context' => 'context',
                '_charset' => 'this.env_.getCharset()',
            );

            if (!isset($specialVars[$name])) {
                throw new \RuntimeException(sprintf('The special var "%s" is not supported by the NameCompiler.', $name));
            }

            $compiler->raw($specialVars[$name]);
        } else {
            if (isset($compiler->localVarMap[$name])) {
                $compiler->raw($compiler->localVarMap[$name]);

                return;
            }

            // FIXME: Add strict behavior?
            //        see Template::getContext()
            $compiler
                ->string('get' . ucfirst($name))
                ->raw(' in context ? ')
                ->raw('context.get' . ucfirst($name))
                ->raw('() : null')
            ;
        }
    }
}<commit_msg>Also check the local property for backwards compatability<commit_after><?php

/*
 * Copyright 2011 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace TwigJs\Compiler\Expression;

use TwigJs\JsCompiler;
use TwigJs\TypeCompilerInterface;

class NameCompiler implements TypeCompilerInterface
{
    public function getType()
    {
        return 'Twig_Node_Expression_Name';
    }

    public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
    {
        if (!$node instanceof \Twig_Node_Expression_Name) {
            throw new \RuntimeException(sprintf('$node must be an instanceof of \Expression_Name, but got "%s".', get_class($node)));
        }

        $name = $node->getAttribute('name');

        if ($node->getAttribute('is_defined_test')) {
            if ($node->isSpecial()) {
                $compiler->repr(true);
            } else {
                $compiler->raw('(')->repr($name)->raw(' in context)');
            }
        } elseif ($node->isSpecial()) {
            static $specialVars = array(
                '_self' => 'this',
                '_context' => 'context',
                '_charset' => 'this.env_.getCharset()',
            );

            if (!isset($specialVars[$name])) {
                throw new \RuntimeException(sprintf('The special var "%s" is not supported by the NameCompiler.', $name));
            }

            $compiler->raw($specialVars[$name]);
        } else {
            if (isset($compiler->localVarMap[$name])) {
                $compiler->raw($compiler->localVarMap[$name]);

                return;
            }

            // FIXME: Add strict behavior?
            //        see Template::getContext()
            $compiler
                ->string('get' . ucfirst($name))
                ->raw(' in context ? ')
                ->raw('context.get' . ucfirst($name))
                ->raw('() : ')
                ->string($name)
                ->raw(' in context ? ')
                ->raw('context.'.$name)
                ->raw(' : null')
            ;
        }
    }
}
{'fraction_non_alphanumeric': 0.13028663058729203, 'fraction_numerical': 0.0032070555221487274, 'mean_word_length': 2.0575980392156863, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3218452', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1278, 'n_words': 373}