0

WebUI: Delete js_modulizer(), no longer used.

This GN rule was used to auto-generate JS modules code from existing
checked-in non-module code and was useful during migration of Polymer2
to Polymer3. The last usages of this rule (in CrOS OOBE) were recently
removed and this migration tool can finally be deleted!

Fixed: 1184053
Change-Id: I5db1d9ee8a9302304091bb7ec66d5fa7ab51806c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4177148
Reviewed-by: Rebekah Potter <rbpotter@chromium.org>
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1094620}
This commit is contained in:
dpapad
2023-01-19 19:53:53 +00:00
committed by Chromium LUCI CQ
parent b90466b2e5
commit a6645f90d7
15 changed files with 0 additions and 364 deletions

@ -3,7 +3,6 @@
# found in the LICENSE file.
import("//third_party/closure_compiler/compile_js.gni")
import("//ui/webui/resources/tools/js_modulizer.gni")
js_type_check("closure_compile") {
deps = [ ":test_api" ]

@ -105,20 +105,6 @@ def CheckNoDisallowedJS(input_api, output_api):
return presubmit_support.DisallowNewJsFiles(input_api, output_api,
lambda f: not allow_js(f))
def CheckJsModulizer(input_api, output_api):
affected = input_api.AffectedFiles()
affected_files = [input_api.os_path.basename(f.LocalPath()) for f in affected]
results = []
if 'js_modulizer.py' in affected_files:
presubmit_path = input_api.PresubmitLocalPath()
sources = [input_api.os_path.join('tools', 'js_modulizer_test.py')]
tests = [input_api.os_path.join(presubmit_path, s) for s in sources]
results += input_api.canned_checks.RunUnitTests(
input_api, output_api, tests, run_on_python2=False)
return results
def CheckGenerateGrd(input_api, output_api):
affected = input_api.AffectedFiles()
affected_files = [input_api.os_path.basename(f.LocalPath()) for f in affected]

@ -1,77 +0,0 @@
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/config/chromeos/ui_mode.gni")
assert(is_chromeos_ash)
common_namespace_rewrites = [
"cr.addSingletonGetter|addSingletonGetter",
"cr.addWebUIListener|addWebUIListener",
"cr.dispatchSimpleEvent|dispatchSimpleEvent",
"cr.EventTracker|EventTracker",
"cr.icon.getFavicon|getFavicon",
"cr.icon.getImage|getImage",
"cr.isAndroid|isAndroid",
"cr.isChromeOS|isChromeOS",
"cr.isIOS|isIOS",
"cr.isLacros|isLacros",
"cr.isLinux|isLinux",
"cr.isMac|isMac",
"cr.isWindows|isWindows",
"cr.png.convertImageSequenceToPng|convertImageSequenceToPng",
"cr.removeWebUIListener|removeWebUIListener",
"cr.sendWithPromise|sendWithPromise",
"cr_slider.SliderTick|SliderTick",
"cr.toastManager.getToastManager|getToastManager",
"cr.ui.FocusOutlineManager|FocusOutlineManager",
"cr.ui.FocusRowBehavior|FocusRowBehavior",
"cr.ui.focusWithoutInk|focusWithoutInk",
"cr.ui.KeyboardShortcutList|KeyboardShortcutList",
"cr.ui.Store|Store",
"Polymer.dom.|",
"Polymer.dom(|dom(",
"Polymer.IronA11yAnnouncer|IronA11yAnnouncer",
"Polymer.PaperRippleBehavior|PaperRippleBehavior",
"Polymer.RenderStatus.afterNextRender|afterNextRender",
"Polymer.RenderStatus.beforeNextRender|beforeNextRender",
# TODO(dpapad): Add more such rewrites as they get discovered.
]
template("js_modulizer") {
_outputs = []
foreach(_input, invoker.input_files) {
_outputs += [ "$target_gen_dir/" + get_path_info(_input, "name") + ".m.js" ]
}
action(target_name) {
script = "//ui/webui/resources/tools/js_modulizer.py"
inputs = invoker.input_files
outputs = _outputs
args = [
"--in_folder",
rebase_path(".", root_build_dir),
"--out_folder",
rebase_path(target_gen_dir, root_build_dir),
"--input_files",
] + invoker.input_files
args += [ "--namespace_rewrites" ] + common_namespace_rewrites
if (defined(invoker.namespace_rewrites)) {
args += invoker.namespace_rewrites
}
}
# Appends the compiled outputs of |target_name| to the dependent's
# runtime_data list.
source_set("${target_name}_runtime_data") {
testonly = true
deps = [ ":${invoker.target_name}" ]
data = _outputs
}
}

@ -1,153 +0,0 @@
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Generates JS files that use native JS Modules from existing JS files that
# either use cr.define() or add to the global namespace directly (like
# assert.js).
#
# This is useful for avoiding code duplication while migration to JS Modules is
# in progress. The eventual goal is to migrate all code to JS modules, at which
# point auto-generation will be unnecessary, and this script can be deleted,
# and the generated code should become the canonical checked-in code.
#
# In order to auto-generate the JS modules, metadata is added to the original
# file in the form of JS comments, like "#export", "#import" and
# "#cr_define_end". See examples in js_modulizer_test.py.
#
# Variables:
# input_files:
# The input JS files to be processed.
#
# in_folder:
# The folder where |input_files| reside.
#
# out_folder:
# The output folder for the generated JS files. Each generated file has the
# '.m.js' suffix, e.g. assert.js -> assert.m.js.
# namespace_rewrites:
# A list of string replacements for replacing global namespaced references
# with explicitly imported dependencies in the generated JS module.
# For example "cr.foo.Bar|Bar" will replace all occurrences of "cr.foo.Bar"
# with "Bar". This flag works identically with the one in
# polymer_modulizer.gni.
import argparse
import io
import os
import re
import sys
_CWD = os.getcwd()
IMPORT_LINE_REGEX = '// #import'
EXPORT_LINE_REGEX = '/* #export */'
IGNORE_LINE_REGEX = '\s*/\* #ignore \*/(\S|\s)*'
# Ignore lines that contain <include> tags, (for example see util.js).
INCLUDE_LINE_REGEX = '^// <include '
# Ignore this line.
CR_DEFINE_START_REGEX = 'cr.define\('
# Ignore all lines after this comment.
CR_DEFINE_END_REGEX = r'\s+// #cr_define_end'
# Replace various global references with their non-namespaced version, for
# example "cr.ui.Foo" becomes "Foo".
def _rewrite_namespaces(string, namespace_rewrites):
for rewrite in namespace_rewrites:
string = string.replace(rewrite, namespace_rewrites[rewrite])
return string
def ProcessFile(filename, out_folder, namespace_rewrites):
# Gather indices of lines to be removed.
indices_to_remove = []
with io.open(filename, encoding='utf-8', mode='r') as f:
lines = f.readlines()
ignore_remaining_lines = False
cr_define_start_index = -1
cr_define_end_index = -1
for i, line in enumerate(lines):
if ignore_remaining_lines:
indices_to_remove.append(i)
continue
if re.match(INCLUDE_LINE_REGEX, line):
indices_to_remove.append(i)
continue
if re.match(IGNORE_LINE_REGEX, line):
indices_to_remove.append(i)
continue
if re.match(CR_DEFINE_START_REGEX, line):
assert cr_define_start_index == -1, (
'Multiple cr.define() calls not supported.')
assert cr_define_end_index == -1, 'Unexpected #cr_define_end'
cr_define_start_index = i
indices_to_remove.append(i)
continue
if re.match(CR_DEFINE_END_REGEX, line):
assert cr_define_start_index >= 0, 'Unexpected #cr_define_end'
assert cr_define_end_index == -1, (
'Multiple #cr_define_end calls not supported.')
cr_define_end_index = i
indices_to_remove.append(i)
ignore_remaining_lines = True
continue
line = line.replace(EXPORT_LINE_REGEX, 'export')
line = line.replace(IMPORT_LINE_REGEX, 'import')
line = _rewrite_namespaces(line, namespace_rewrites)
lines[i] = line
if cr_define_start_index != -1:
assert cr_define_end_index != -1, 'No cr_define_end found'
# Process line numbers in descending order, such that the array can be
# modified in-place.
indices_to_remove.reverse()
for i in indices_to_remove:
del lines[i]
out_filename = os.path.splitext(os.path.basename(filename))[0] + '.m.js'
# Reconstruct file.
# Specify the newline character so that the exact same file is generated
# across platforms.
with io.open(os.path.join(out_folder, out_filename), 'wb') as f:
for l in lines:
f.write(l.encode('utf-8'))
return
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--input_files', nargs='*', required=True)
parser.add_argument('--in_folder', required=True)
parser.add_argument('--out_folder', required=True)
parser.add_argument('--namespace_rewrites', required=False, nargs="*")
args = parser.parse_args(argv)
# Extract namespace rewrites from arguments.
namespace_rewrites = {}
if args.namespace_rewrites:
for r in args.namespace_rewrites:
before, after = r.split('|')
namespace_rewrites[before] = after
in_folder = os.path.normpath(os.path.join(_CWD, args.in_folder))
out_folder = os.path.normpath(os.path.join(_CWD, args.out_folder))
for f in args.input_files:
ProcessFile(os.path.join(in_folder, f), out_folder, namespace_rewrites)
if __name__ == '__main__':
main(sys.argv[1:])

@ -1,65 +0,0 @@
#!/usr/bin/env python3
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import js_modulizer
import os
import shutil
import tempfile
import unittest
_HERE_DIR = os.path.dirname(__file__)
class JsModulizerTest(unittest.TestCase):
def setUp(self):
self._out_folder = None
def tearDown(self):
shutil.rmtree(self._out_folder)
def _read_out_file(self, file_name):
assert self._out_folder
with open (os.path.join(self._out_folder, file_name), 'rb') as f:
return f.read()
def _run_test_(self, js_file, js_file_expected, namespace_rewrites=None):
assert not self._out_folder
self._out_folder = tempfile.mkdtemp(dir=_HERE_DIR)
args = [
'--input_files', js_file,
'--in_folder', os.path.join(_HERE_DIR, 'tests'),
'--out_folder', self._out_folder,
]
if namespace_rewrites:
args += ['--namespace_rewrites'] + namespace_rewrites
js_modulizer.main(args)
js_out_file = os.path.basename(js_file).replace('.js', '.m.js')
actual_js = self._read_out_file(js_out_file)
with open(os.path.join(_HERE_DIR, 'tests', js_file_expected), 'rb') as f:
expected_js = f.read();
self.assertEqual(expected_js, actual_js)
def testSuccess_WithoutCrDefine(self):
self._run_test_('without_cr_define.js', 'without_cr_define_expected.js')
def testSuccess_WithCrDefine(self):
self._run_test_('with_cr_define.js', 'with_cr_define_expected.js')
def testSuccess_WithRename(self):
self._run_test_(
'with_rename.js', 'with_rename_expected.js', ['cr.foo.Bar|Bar'])
def testSuccess_WithIgnore(self):
self._run_test_('with_ignore.js', 'with_ignore_expected.js')
def testSuccess_NonAscii(self):
self._run_test_('non_ascii.js', 'non_ascii_expected.js')
if __name__ == '__main__':
unittest.main()

@ -1,7 +0,0 @@
/* #export */ function asciiLanguageName() {
return 'English';
}
/* #export */ function nonAsciiLanguageName() {
return 'Ελληνικά';
}
function baz() {}

@ -1,7 +0,0 @@
export function asciiLanguageName() {
return 'English';
}
export function nonAsciiLanguageName() {
return 'Ελληνικά';
}
function baz() {}

@ -1,11 +0,0 @@
cr.define('cr.foo', function() {
/* #export */ function foo() {}
function bar() {}
/* #export */ function baz() {}
// #cr_define_end
return {
foo: foo,
baz: baz,
};
});

@ -1,4 +0,0 @@
export function foo() {}
function bar() {}
export function baz() {}

@ -1,5 +0,0 @@
/* #ignore */ console.log('Hello');
console.log('World');
// clang-format off
/* #ignore */ console.log('Goodbye');
// clang-format on

@ -1,3 +0,0 @@
console.log('World');
// clang-format off
// clang-format on

@ -1,3 +0,0 @@
// #import {Bar} from './module.m.js';
console.log(cr.foo.Bar);
/** @type {cr.foo.Bar} */ const bar = 'foo';

@ -1,3 +0,0 @@
import {Bar} from './module.m.js';
console.log(Bar);
/** @type {Bar} */ const bar = 'foo';

@ -1,6 +0,0 @@
// #import {other} from './other.m.js';
// <include src="other.js">
/* #export */ function foo() {}
function bar() {}
/* #export */ function baz() {}

@ -1,5 +0,0 @@
import {other} from './other.m.js';
export function foo() {}
function bar() {}
export function baz() {}