2 Star 0 Fork 0

chromium_develop/chromium_tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
accessibility
android
binary_size
bisect_repackage
cfi
check_ecs_deps
checkbins
checklicenses
checkperms
checkteamtags
chrome_extensions/open_my_editor
clang
code_coverage
compile_test
coverity
cr
cros
cygprofile
determinism
diagnosis
dromaeo_benchmark_runner
dump_process_memory
emacs
find_runtime_symbols
flags
flakiness
fuchsia
gdb
generate_library_loader
generate_shim_headers
generate_stubs
get_swarming_logs
git
gn
grit
gritsettings
idl_parser
imagediff
infra
ipc_fuzzer
json_comment_eater
json_schema_compiler
highlighters
test
BUILD.gn
DIR_METADATA
OWNERS
PRESUBMIT.py
cc_generator.py
code.py
code_test.py
compiler.py
cpp_bundle_generator.py
cpp_bundle_generator_test.py
cpp_generator.py
cpp_namespace_environment.py
cpp_type_generator.py
cpp_type_generator_test.py
cpp_util.py
cpp_util_test.py
feature_compiler.py
feature_compiler_test.py
features_cc_generator.py
features_compiler.py
features_h_generator.py
h_generator.py
idl_schema.py
idl_schema_test.py
js_externs_generator.py
js_externs_generator_test.py
js_interface_generator.py
js_interface_generator_test.py
js_util.py
json_features.gni
json_parse.py
json_schema.py
json_schema_api.gni
json_schema_test.py
manifest_parse_util.cc
manifest_parse_util.h
memoize.py
model.py
model_test.py
namespace_resolver.py
preview.py
schema_loader.py
schema_util.py
schema_util_test.py
util.cc
util.h
util_cc_helper.py
json_to_struct
l10n
linux
lldb
luci-go
mac
mb
md_browser
media_engagement_preload
memory
memory_inspector
metrics
msan
oopif
origin_trials
page_cycler
perf
polymer
privacy_budget
protoc_wrapper
python
real_world_impact
resources
resultdb
security
site_compare
skia_goldctl/linux
stats_viewer
strict_enum_value_checker
style_variable_generator
sublime
swarming_client
symsrc
tcmalloc
tests
traceline
traffic_annotation
translation
ubsan
usb_gadget
v8_context_snapshot
valgrind
variations
vim
vscode
web_bluetooth
web_dev_style
win
.style.yapf
DEPS
DIR_METADATA
OWNERS
README.en.md
README.md
auto-nav.py
autotest.py
bash-completion
bisect-builds.py
bisect_test.py
boilerplate.py
buildstate.bat
buildstate.py
check_git_config.py
check_grd_for_unused_strings.py
clang-format-js
diagnose-me.py
download_optimization_profile.py
gypv8sh.py
include_tracer.py
ipc_messages_log.py
licenses.py
make-gtest-filter.py
multi_process_rss.py
nocompile_driver.py
omahaproxy.py
perry.py
remove_duplicate_includes.py
remove_stale_pyc_files.py
roll_webgl_conformance.py
run-swarmed.py
sort-headers.py
sort_sources.py
uberblame.py
unused-symbols-report.py
update_pgo_profiles.py
yes_no.py
克隆/下载
js_externs_generator.py 8.15 KB
一键复制 编辑 原始数据 按行查看 历史
李想 提交于 3年前 . chromium origin init
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Generator that produces an externs file for the Closure Compiler.
Note: This is a work in progress, and generated externs may require tweaking.
See https://developers.google.com/closure/compiler/docs/api-tutorial3#externs
"""
from code import Code
from js_util import JsUtil
from model import *
from schema_util import *
import os
import sys
import re
NOTE = """// NOTE: The format of types has changed. 'FooType' is now
// 'chrome.%s.FooType'.
// Please run the closure compiler before committing changes.
// See https://chromium.googlesource.com/chromium/src/+/master/docs/closure_compilation.md
"""
class JsExternsGenerator(object):
def Generate(self, namespace):
return _Generator(namespace).Generate()
class _Generator(object):
def __init__(self, namespace):
self._namespace = namespace
self._class_name = None
self._js_util = JsUtil()
def Generate(self):
"""Generates a Code object with the schema for the entire namespace.
"""
c = Code()
# /abs/path/src/tools/json_schema_compiler/
script_dir = os.path.dirname(os.path.abspath(__file__))
# /abs/path/src/
src_root = os.path.normpath(os.path.join(script_dir, '..', '..'))
# tools/json_schema_compiler/
src_to_script = os.path.relpath(script_dir, src_root)
# tools/json_schema_compiler/compiler.py
compiler_path = os.path.join(src_to_script, 'compiler.py')
(c.Append(self._GetHeader(compiler_path, self._namespace.name))
.Append())
self._AppendNamespaceObject(c)
for js_type in self._namespace.types.values():
self._AppendType(c, js_type)
for prop in self._namespace.properties.values():
self._AppendProperty(c, prop)
for function in self._namespace.functions.values():
self._AppendFunction(c, function)
for event in self._namespace.events.values():
self._AppendEvent(c, event)
c.TrimTrailingNewlines()
return c
def _GetHeader(self, tool, namespace):
"""Returns the file header text.
"""
return (self._js_util.GetLicense() + '\n' +
self._js_util.GetInfo(tool) + (NOTE % namespace) + '\n' +
('/** @fileoverview Externs generated from namespace: %s */' %
namespace))
def _AppendType(self, c, js_type):
"""Given a Type object, generates the Code for this type's definition.
"""
if js_type.property_type is PropertyType.ENUM:
self._AppendEnumJsDoc(c, js_type)
else:
self._AppendTypeJsDoc(c, js_type)
c.Append()
def _AppendEnumJsDoc(self, c, js_type):
""" Given an Enum Type object, generates the Code for the enum's definition.
"""
c.Sblock(line='/**', line_prefix=' * ')
c.Append('@enum {string}')
self._js_util.AppendSeeLink(c, self._namespace.name, 'type',
js_type.simple_name)
c.Eblock(' */')
c.Append('%s.%s = {' % (self._GetNamespace(), js_type.name))
def get_property_name(e):
# Enum properties are normified to be in ALL_CAPS_STYLE.
# Assume enum '1ring-rulesThemAll'.
# Transform to '1ring-rules_Them_All'.
e = re.sub(r'([a-z])([A-Z])', r'\1_\2', e)
# Transform to '1ring_rules_Them_All'.
e = re.sub(r'\W', '_', e)
# Transform to '_1ring_rules_Them_All'.
e = re.sub(r'^(\d)', r'_\1', e)
# Transform to '_1RING_RULES_THEM_ALL'.
return e.upper()
c.Append('\n'.join(
[" %s: '%s'," % (get_property_name(v.name), v.name)
for v in js_type.enum_values]))
c.Append('};')
def _IsTypeConstructor(self, js_type):
"""Returns true if the given type should be a @constructor. If this returns
false, the type is a typedef.
"""
return any(prop.type_.property_type is PropertyType.FUNCTION
for prop in js_type.properties.values())
def _AppendTypeJsDoc(self, c, js_type, optional=False):
"""Appends the documentation for a type as a Code.
"""
c.Sblock(line='/**', line_prefix=' * ')
if js_type.description:
for line in js_type.description.splitlines():
c.Append(line)
if js_type.jsexterns:
for line in js_type.jsexterns.splitlines():
c.Append(line)
is_constructor = self._IsTypeConstructor(js_type)
if js_type.property_type is not PropertyType.OBJECT:
self._js_util.AppendTypeJsDoc(c, self._namespace.name, js_type, optional)
elif is_constructor:
c.Comment('@constructor', comment_prefix = '', wrap_indent=4)
c.Comment('@private', comment_prefix = '', wrap_indent=4)
elif js_type.jsexterns is None:
self._AppendTypedef(c, js_type.properties)
self._js_util.AppendSeeLink(c, self._namespace.name, 'type',
js_type.simple_name)
c.Eblock(' */')
var = '%s.%s' % (self._GetNamespace(), js_type.simple_name)
if is_constructor: var += ' = function() {}'
var += ';'
c.Append(var)
if is_constructor:
c.Append()
self._class_name = js_type.name
for prop in js_type.properties.values():
if prop.type_.property_type is PropertyType.FUNCTION:
self._AppendFunction(c, prop.type_.function)
else:
self._AppendTypeJsDoc(c, prop.type_, prop.optional)
c.Append()
self._class_name = None
def _AppendTypedef(self, c, properties):
"""Given an OrderedDict of properties, Appends code containing a @typedef.
"""
c.Append('@typedef {')
if properties:
self._js_util.AppendObjectDefinition(
c, self._namespace.name, properties, new_line=False)
else:
c.Append('Object', new_line=False)
c.Append('}', new_line=False)
def _AppendProperty(self, c, prop):
"""Appends the code representing a top-level property, including its
documentation. For example:
/** @type {string} */
chrome.runtime.id;
"""
self._AppendTypeJsDoc(c, prop.type_, prop.optional)
c.Append()
def _AppendFunction(self, c, function):
"""Appends the code representing a function, including its documentation.
For example:
/**
* @param {string} title The new title.
*/
chrome.window.setTitle = function(title) {};
"""
self._js_util.AppendFunctionJsDoc(c, self._namespace.name, function)
params = self._GetFunctionParams(function)
c.Append('%s.%s = function(%s) {};' % (self._GetNamespace(),
function.name, params))
c.Append()
def _AppendEvent(self, c, event):
"""Appends the code representing an event.
For example:
/** @type {!ChromeEvent} */
chrome.bookmarks.onChildrenReordered;
"""
c.Sblock(line='/**', line_prefix=' * ')
if (event.description):
c.Comment(event.description, comment_prefix='')
c.Append('@type {!ChromeEvent}')
self._js_util.AppendSeeLink(c, self._namespace.name, 'event', event.name)
c.Eblock(' */')
c.Append('%s.%s;' % (self._GetNamespace(), event.name))
c.Append()
def _AppendNamespaceObject(self, c):
"""Appends the code creating namespace object.
For example:
/** @const */
chrome.bookmarks = {};
"""
c.Append('/** @const */')
c.Append('chrome.%s = {};' % self._namespace.name)
c.Append()
def _GetFunctionParams(self, function):
"""Returns the function params string for function.
"""
params = function.params[:]
param_names = [param.name for param in params]
# TODO(https://crbug.com/1142991): Update this to represent promises better,
# rather than just appended as a callback.
if function.returns_async:
param_names.append(function.returns_async.name)
return ', '.join(param_names)
def _GetNamespace(self):
"""Returns the namespace to be prepended to a top-level typedef.
For example, it might return "chrome.namespace".
Also optionally includes the class name if this is in the context
of outputting the members of a class.
For example, "chrome.namespace.ClassName.prototype"
"""
if self._class_name:
return 'chrome.%s.%s.prototype' % (self._namespace.name, self._class_name)
return 'chrome.%s' % self._namespace.name
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chromium_develop/chromium_tools.git
git@gitee.com:chromium_develop/chromium_tools.git
chromium_develop
chromium_tools
chromium_tools
master

搜索帮助