-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Files app: Script to convert JS to TS
The script tries as a best effort to perform the mechanical steps to convert a file from JS to TS. 1. Removes references of the js_library() and deps on it from the BUILD files. 2. Moves the file name from JS to TS in the file_names.gni 3. Tries to move the type definition from JSDoc to TS syntax. 3.1 Removes all @ts-ignores. 3.2 Replaces the `ash/common/assert.js` with `js/assert.js`. 4. Runs git mv to rename the file from JS to TS. Run manually the following conversions without failures in the script: $ python ui/file_manager/base/js/convert_to_ts.py ui/file_manager/file_manager/common/js/app_util.js $ python ui/file_manager/base/js/convert_to_ts.py ui/file_manager/file_manager/common/js/volume_manager_types_unittest.js See results in crrev.com/c/4979126/1 Bug: b:289003444 Change-Id: I11ef7df5b0bed054889c9e53f5ed0a31454885af Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4975650 Commit-Queue: Luciano Pacheco <lucmult@chromium.org> Reviewed-by: Wenbo Jie <wenbojie@chromium.org> Reviewed-by: Ben Reich <benreich@chromium.org> Cr-Commit-Position: refs/heads/main@{#1215908}
- Loading branch information
Luciano Pacheco
authored and
Chromium LUCI CQ
committed
Oct 27, 2023
1 parent
0e06df9
commit 941c2ed
Showing
9 changed files
with
1,034 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright 2023 The Chromium Authors | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
import os | ||
|
||
import unittest | ||
from convert_to_ts import to_build_file, process_build_file, process_js_file, process_file_names_gni | ||
from pprint import pprint | ||
from pathlib import Path | ||
from difflib import Differ | ||
|
||
_HERE_DIR = os.path.dirname(__file__) | ||
|
||
|
||
def to_expected(fname): | ||
name, ext = os.path.splitext(fname) | ||
return name + '_expected' + ext + '.txt' | ||
|
||
|
||
class ConvertToTs(unittest.TestCase): | ||
def setUp(self): | ||
self.fname = Path(_HERE_DIR, | ||
os.path.join('tests', 'testing_convert_to_ts.js')) | ||
self.unittest_fname = Path( | ||
_HERE_DIR, | ||
os.path.join('tests', 'testing_convert_to_ts_unittest.js')) | ||
self.build_file = to_build_file(self.fname).with_name('_BUILD.gn') | ||
self.file_names_gni = Path(_HERE_DIR, | ||
os.path.join('tests', '_file_names.gni')) | ||
|
||
# The file_names.gni we process different for JS and unittest. | ||
self.file_names_gni_js_result = Path( | ||
_HERE_DIR, os.path.join('tests', | ||
'_file_names_js_expected.gni.txt')) | ||
self.file_names_gni_unittest_result = Path( | ||
_HERE_DIR, | ||
os.path.join('tests', '_file_names_unittest_expected.gni.txt')) | ||
|
||
def compare_to_expected(self, content, fname, expected_fname=None): | ||
expected_fname = expected_fname or to_expected(fname) | ||
with open(expected_fname) as f: | ||
expected = f.readlines() | ||
try: | ||
self.assertEqual(content, expected) | ||
except BaseException: | ||
print('-- Conversion result ----------------------') | ||
for l in content: | ||
print(l.replace('\n', '')) | ||
print('-- Diff: ----------------------------------') | ||
for l in list(Differ().compare(expected, content)): | ||
print(l, end='') | ||
print('-------------------------------------------') | ||
|
||
raise | ||
|
||
def test_build_file(self): | ||
new_file = process_build_file(self.build_file, self.fname) | ||
self.compare_to_expected(new_file, self.build_file) | ||
|
||
def test_js_file(self): | ||
new_file = process_js_file(self.fname) | ||
self.compare_to_expected(new_file, self.fname) | ||
|
||
def test_file_names_js(self): | ||
new_file = process_file_names_gni(self.fname, self.file_names_gni) | ||
self.compare_to_expected(new_file, self.fname, | ||
self.file_names_gni_js_result) | ||
|
||
def test_file_names_unittest(self): | ||
new_file = process_file_names_gni(self.unittest_fname, | ||
self.file_names_gni) | ||
self.compare_to_expected(new_file, self.unittest_fname, | ||
self.file_names_gni_unittest_result) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2023 The Chromium Authors | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
# Used for testting the script convert_to_ts.py | ||
|
||
group("js_modules") { | ||
deps = [ | ||
":other", | ||
":testing_convert_to_ts", | ||
] | ||
} | ||
|
||
js_library("testing_convert_to_ts") { | ||
deps = [ | ||
":storage", | ||
"//ui/file_manager/file_manager/externs:file_manager_private", | ||
"//ui/file_manager/file_manager/externs:volume_manager", | ||
] | ||
if (something) { | ||
# Just to add an inner scope. | ||
externs_list = | ||
[ "//ui/file_manager/file_manager/externs/app_window_common.js" ] | ||
} | ||
} | ||
|
||
js_unittest("testing_convert_to_ts") { | ||
# Using the same name as the js_library(), because it shouldn't matter the name. | ||
deps = [ | ||
":storage", | ||
"//ui/file_manager/file_manager/externs:file_manager_private", | ||
"//ui/file_manager/file_manager/externs:volume_manager", | ||
] | ||
if (something) { | ||
# Just to add an inner scope. | ||
externs_list = | ||
[ "//ui/file_manager/file_manager/externs/app_window_common.js" ] | ||
} | ||
} | ||
|
||
js_library("other") { | ||
deps = [ ":testing_convert_to_ts" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright 2023 The Chromium Authors | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
# Used for testting the script convert_to_ts.py | ||
|
||
group("js_modules") { | ||
deps = [ | ||
":other", | ||
] | ||
} | ||
|
||
|
||
|
||
js_library("other") { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2023 The Chromium Authors | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
static_js_files = [ | ||
"file_manager/background/js/file_manager_base.js", | ||
"file_manager/background/js/file_operation_manager.js", | ||
"file_manager/background/js/file_operation_util.js", | ||
"base/js/tests/testing_convert_to_ts.js", | ||
] | ||
|
||
ts_files = [ | ||
# Common. | ||
"file_manager/common/js/api.ts", | ||
"file_manager/common/js/cr_ui.ts", | ||
] | ||
|
||
ts_test_files = [ | ||
# Common. | ||
"file_manager/common/js/entry_utils_unittest.ts", | ||
"file_manager/common/js/file_types_base_unittest.ts", | ||
] | ||
|
||
unittest_files = [ | ||
"base/js/tests/testing_convert_to_ts_unittest.js", | ||
|
||
# Common: | ||
"file_manager/common/js/array_data_model_unittest.js", | ||
"file_manager/common/js/async_util_unittest.js", | ||
] |
30 changes: 30 additions & 0 deletions
30
ui/file_manager/base/js/tests/_file_names_js_expected.gni.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2023 The Chromium Authors | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
static_js_files = [ | ||
"file_manager/background/js/file_manager_base.js", | ||
"file_manager/background/js/file_operation_manager.js", | ||
"file_manager/background/js/file_operation_util.js", | ||
] | ||
|
||
ts_files = [ | ||
"base/js/tests/testing_convert_to_ts.ts", | ||
# Common. | ||
"file_manager/common/js/api.ts", | ||
"file_manager/common/js/cr_ui.ts", | ||
] | ||
|
||
ts_test_files = [ | ||
# Common. | ||
"file_manager/common/js/entry_utils_unittest.ts", | ||
"file_manager/common/js/file_types_base_unittest.ts", | ||
] | ||
|
||
unittest_files = [ | ||
"base/js/tests/testing_convert_to_ts_unittest.js", | ||
|
||
# Common: | ||
"file_manager/common/js/array_data_model_unittest.js", | ||
"file_manager/common/js/async_util_unittest.js", | ||
] |
30 changes: 30 additions & 0 deletions
30
ui/file_manager/base/js/tests/_file_names_unittest_expected.gni.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2023 The Chromium Authors | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
static_js_files = [ | ||
"file_manager/background/js/file_manager_base.js", | ||
"file_manager/background/js/file_operation_manager.js", | ||
"file_manager/background/js/file_operation_util.js", | ||
"base/js/tests/testing_convert_to_ts.js", | ||
] | ||
|
||
ts_files = [ | ||
# Common. | ||
"file_manager/common/js/api.ts", | ||
"file_manager/common/js/cr_ui.ts", | ||
] | ||
|
||
ts_test_files = [ | ||
"base/js/tests/testing_convert_to_ts_unittest.ts", | ||
# Common. | ||
"file_manager/common/js/entry_utils_unittest.ts", | ||
"file_manager/common/js/file_types_base_unittest.ts", | ||
] | ||
|
||
unittest_files = [ | ||
|
||
# Common: | ||
"file_manager/common/js/array_data_model_unittest.js", | ||
"file_manager/common/js/async_util_unittest.js", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import {assert} from 'chrome://resources/ash/common/assert.js'; | ||
// @ts-ignore: error TS2792: Cannot find module | ||
// 'chrome://resources/cr_components/color_change_listener/colors_css_updater.js'. | ||
// Did you mean to set the 'moduleResolution' option to 'nodenext', or to add | ||
// aliases to the 'paths' option? | ||
import {ColorChangeUpdater} from 'chrome://resources/cr_components/color_change_listener/colors_css_updater.js'; | ||
|
||
import {FileManagerBaseInterface} from '../../../file_manager/externs/background/file_manager_base.js'; | ||
|
||
|
||
// @ts-ignore: error TS2314: Generic type 'Array<T>' requires 1 type | ||
// argument(s). | ||
/** @type {!Array} */ | ||
let directoryChangedListeners; | ||
|
||
/** | ||
* Root class of the former background page. | ||
* @extends {Base} | ||
* @implements {FileManagerBaseInterface} | ||
*/ | ||
export class FileManagerBase { | ||
constructor() { | ||
/** | ||
* Map of all currently open file dialogs. The key is an app ID. | ||
* @type {!Record<string, !Window>} | ||
*/ | ||
this.dialogs = {}; | ||
|
||
/** @private @type {number} */ | ||
this.index_; | ||
|
||
/** | ||
* some description. | ||
* @private @type {string} | ||
*/ | ||
this.bla = ''; | ||
|
||
/** | ||
* some description. | ||
* @protected @type {string} | ||
*/ | ||
this.ble = ''; | ||
} | ||
|
||
/** | ||
* @return {!Promise<!VolumeManager>} | ||
*/ | ||
async getVolumeManager() { | ||
return volumeManagerFactory.getInstance(); | ||
} | ||
|
||
/** | ||
* @return {?VolumeManager} | ||
*/ | ||
getVolumeManager2() { | ||
return /** @type {!VolumeManager} */ (this.bla); | ||
} | ||
|
||
/** | ||
* @private @return {!Promise<!VolumeManager>} | ||
*/ | ||
async getVolumeManager3_() { | ||
return volumeManagerFactory.getInstance(); | ||
} | ||
|
||
/** | ||
* Register callback to be invoked after initialization. | ||
* If the initialization is already done, the callback is invoked immediately. | ||
* | ||
* @param {function():void} callback Initialize callback to be registered. | ||
*/ | ||
ready(callback) { | ||
// @ts-ignore: error TS2531: Object is possibly 'null'. | ||
this.initializationPromise_.then(callback); | ||
} | ||
|
||
/** | ||
* Launches a new File Manager window. | ||
* | ||
* @param {!FilesAppState=} appState App state. | ||
* @return {!Promise<void>} Resolved when the new window is opened. | ||
*/ | ||
// @ts-ignore: error TS2739: Type '{}' is missing the following properties | ||
// from type 'FilesAppState': currentDirectoryURL, selectionURL | ||
async launchFileManager(appState = {}) { | ||
return launchFileManager(appState); | ||
} | ||
|
||
/** | ||
* Opens the volume root (or opt directoryPath) in main UI. | ||
* | ||
* @param {!Event} event An event with the volumeId or | ||
* devicePath. | ||
* @private | ||
*/ | ||
handleViewEvent_(event) { | ||
util.doIfPrimaryContext(() => { | ||
this.handleViewEventInternal_(event); | ||
}); | ||
} | ||
|
||
/** | ||
* Retrieves the root file entry of the volume on the requested device. | ||
* | ||
* @param {!string} volumeId ID of the volume to navigate to. | ||
* @param {string} _toBeRemoved | ||
* @return {!Promise<!import("../../externs/volume_info.js").VolumeInfo>} | ||
* @private | ||
*/ | ||
retrieveVolumeInfo_(volumeId, _toBeRemoved) { | ||
// @ts-ignore: error TS2322: Type 'Promise<void | VolumeInfo>' is not | ||
// assignable to type 'Promise<VolumeInfo>'. | ||
return volumeManagerFactory.getInstance().then( | ||
(/** | ||
* @param {!VolumeManager} volumeManager | ||
*/ | ||
(volumeManager) => { | ||
return volumeManager.whenVolumeInfoReady(volumeId).catch((e) => { | ||
console.warn( | ||
'Unable to find volume for id: ' + volumeId + | ||
'. Error: ' + e.message); | ||
}); | ||
})); | ||
} | ||
} |
Oops, something went wrong.