def FindInvalidCSSVariables(json_string, input_file, git_runner=RunGit):
    style_generator = CSSStyleGenerator()
    style_generator.AddJSONToModel(json_string, in_file=input_file)

    context = style_generator.in_file_to_context[input_file]
    if (not context or 'prefix' not in context):
        raise KeyError('This tool only works on files with a CSS prefix.')

    css_prefix = '--' + context['prefix'] + '-'

    valid_names = style_generator.GetCSSVarNames()

    found_names_list = git_runner([
        'grep', '-ho',
        '\\%s[a-z-]*' % css_prefix, '--', '*.css', '*.html', '*.js'
    ]).splitlines()
    found_names = set()
    for name in found_names_list:
        rgb_suffix = '-rgb'
        if name.endswith(rgb_suffix):
            name = name[:-len(rgb_suffix)]
        found_names.add(name)
    return {
        'unspecified': found_names.difference(valid_names),
        'unused': valid_names.difference(found_names),
        'css_prefix': css_prefix,
    }
Beispiel #2
0
 def get_css_var_names_for_contents(contents_function):
     style_generator = CSSStyleGenerator()
     for f in files:
         file_contents = contents_function(f)
         if len(file_contents) == 0:
             continue
         style_generator.AddJSONToModel('\n'.join(file_contents),
                                        in_file=f.LocalPath())
     return style_generator.GetCSSVarNames()