<commit_before># Copyright 2008 Google Inc.
#
# 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.

"""Print classes, functions and modules which contain static data."""

from __future__ import print_function
from __future__ import unicode_literals

import collections

from . import ast
from . import metrics


__author__ = '<EMAIL> (<NAME>)'


def _find_warnings(filename, lines, ast_list, static_is_optional):
    def print_warning(node, name):
        print("{}:{}: Static data '{}'".format(
            filename,
            lines.get_line_number(node.start),
            name))

    def find_static(function_node):
        for node in function_node.body:
            if node.name == 'static':
                # TODO(nnorwitz): should ignore const. Is static const common
                # here?
                print_warning(node, lines.get_line(node.start).strip())

    for node in ast_list:
        if isinstance(node, ast.VariableDeclaration):
            # Ignore 'static' at module scope so we can find globals too.
            is_static = 'static' in node.type.modifiers
            is_not_const = 'const' not in node.type.modifiers

            if is_not_const and (static_is_optional or is_static):
                print_warning(node, node.name)
        elif isinstance(node, ast.Function):
            if node.body:
                find_static(node)
        elif isinstance(node, ast.Class) and node.body:
            _find_warnings(filename, lines, node.body, False)


def _find_unused_static_warnings(filename, lines, ast_list):
    """Warn about unused static variables."""
    static_declarations = {}

    for node in ast_list:
        if (
            isinstance(node, ast.VariableDeclaration) and
            'static' in node.type.modifiers
        ):
            static_declarations[node.name] = node

    static_use_counts = collections.Counter()
    for node in ast_list:
        if isinstance(node, ast.Function) and node.body:
            for child in node.body:
                if child.name in static_declarations:
                    static_use_counts[child.name] += 1

    for name in sorted(static_declarations):
        if not static_use_counts[name]:
            print("{}:{}: Unused variable '{}'".format(
                filename,
                lines.get_line_number(static_declarations[name].start),
                name))


def run(filename, source, entire_ast, include_paths):
    lines = metrics.Metrics(source)<commit_msg>Use dictionary comprehension<commit_after># Copyright 2008 Google Inc.
#
# 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.

"""Print classes, functions and modules which contain static data."""

from __future__ import print_function
from __future__ import unicode_literals

import collections

from . import ast
from . import metrics


__author__ = '<EMAIL> (<NAME>)'


def _find_warnings(filename, lines, ast_list, static_is_optional):
    def print_warning(node, name):
        print("{}:{}: Static data '{}'".format(
            filename,
            lines.get_line_number(node.start),
            name))

    def find_static(function_node):
        for node in function_node.body:
            if node.name == 'static':
                # TODO(nnorwitz): should ignore const. Is static const common
                # here?
                print_warning(node, lines.get_line(node.start).strip())

    for node in ast_list:
        if isinstance(node, ast.VariableDeclaration):
            # Ignore 'static' at module scope so we can find globals too.
            is_static = 'static' in node.type.modifiers
            is_not_const = 'const' not in node.type.modifiers

            if is_not_const and (static_is_optional or is_static):
                print_warning(node, node.name)
        elif isinstance(node, ast.Function):
            if node.body:
                find_static(node)
        elif isinstance(node, ast.Class) and node.body:
            _find_warnings(filename, lines, node.body, False)


def _find_unused_static_warnings(filename, lines, ast_list):
    """Warn about unused static variables."""
    static_declarations = {
        node.name: node
        for node in ast_list
        if (isinstance(node, ast.VariableDeclaration) and
            'static' in node.type.modifiers)
    }

    static_use_counts = collections.Counter()
    for node in ast_list:
        if isinstance(node, ast.Function) and node.body:
            for child in node.body:
                if child.name in static_declarations:
                    static_use_counts[child.name] += 1

    for name in sorted(static_declarations):
        if not static_use_counts[name]:
            print("{}:{}: Unused variable '{}'".format(
                filename,
                lines.get_line_number(static_declarations[name].start),
                name))


def run(filename, source, entire_ast, include_paths):
    lines = metrics.Metrics(source)
{'fraction_non_alphanumeric': 0.08796060451689591, 'fraction_numerical': 0.003056546102903719, 'mean_word_length': 2.626847290640394, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2595839', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1574, 'n_words': 523}