Skip to content

Commit

Permalink
remove hacky globalThis.cdt
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Dec 7, 2019
1 parent 7ad9389 commit 2f80d5a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 56 deletions.
24 changes: 11 additions & 13 deletions build/build-cdt-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,31 @@ execFileSync('node_modules/.bin/tsc', [
// eslint-disable-next-line no-console
console.log('making modifications ...');

/** @type {[RegExp, string][]} */
const patterns = [
[/self./g, ''],
[/(SDK|Common)/g, 'globalThis.cdt.$1'],
];
for (const file of glob.sync(`${outDir}/**/*.js`)) {
const code = fs.readFileSync(file, 'utf-8');
const lines = code.match(/^.*(\r?\n|$)/mg) || [];
const code = fs.readFileSync(file, 'utf-8')
.replace('exports["default"]', 'exports.default');

let lines = code.match(/^.*(\r?\n|$)/mg) || [];
const cutoffIndex = lines.findIndex(line => line.includes('Legacy exported object'));
lines = lines.splice(0, cutoffIndex);

const modifiedLines = lines.map((line, i) => {
// Don't modify jsdoc comments.
if (/^\s*[/*]/.test(line)) {
return line;
}

let newLine = line;
let changed = false;
for (const pattern of patterns) {
if (!pattern[0].test(newLine)) continue;
changed = true;
newLine = newLine.replace(pattern[0], pattern[1]);
if (file.endsWith('ParsedURL.js')) {
newLine = newLine.replace(/Common.ParsedURL/g, 'ParsedURL');
}
if (changed) {
if (newLine !== line) {
// eslint-disable-next-line no-console
console.log(`${file}:${i}: ${line.trim()}`);
}
return newLine;
});
modifiedLines.unshift(`const Common = require('../../Common.js')\n`);
modifiedLines.unshift('// generated by build-cdt-lib.js\n');
modifiedLines.unshift('// @ts-nocheck\n');
fs.writeFileSync(file, modifiedLines.join(''));
Expand Down
12 changes: 12 additions & 0 deletions lighthouse-core/lib/cdt/Common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @license Copyright 2019 Google Inc. All Rights Reserved.
* 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.
*/
'use strict';

const Common = {
ParsedURL: require('./generated/common/ParsedURL.js').default,
};

module.exports = Common;
6 changes: 1 addition & 5 deletions lighthouse-core/lib/cdt/SDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
'use strict';
// @ts-nocheck

global.cdt = {};

require('./generated/common/ParsedURL.js');

const SDK = {
...require('./generated/sdk/SourceMap.js'),
};
Expand Down Expand Up @@ -53,7 +49,7 @@ function upperBound(object, comparator, left, right) {
}
}
return r;
};
}

/**
* @param {number} line
Expand Down
26 changes: 10 additions & 16 deletions lighthouse-core/lib/cdt/generated/common/ParsedURL.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-nocheck
// generated by build-cdt-lib.js
const Common = require('../../Common.js')
"use strict";
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
Expand Down Expand Up @@ -50,7 +51,7 @@ var ParsedURL = /** @class */ (function () {
this.lastPathComponent = '';
var isBlobUrl = this.url.startsWith('blob:');
var urlToMatch = isBlobUrl ? url.substring(5) : url;
var match = urlToMatch.match(globalThis.cdt.Common.ParsedURL._urlRegex());
var match = urlToMatch.match(ParsedURL._urlRegex());
if (match) {
this.isValid = true;
if (isBlobUrl) {
Expand Down Expand Up @@ -134,8 +135,8 @@ var ParsedURL = /** @class */ (function () {
* @return {!RegExp}
*/
ParsedURL._urlRegex = function () {
if (globalThis.cdt.Common.ParsedURL._urlRegexInstance) {
return globalThis.cdt.Common.ParsedURL._urlRegexInstance;
if (ParsedURL._urlRegexInstance) {
return ParsedURL._urlRegexInstance;
}
// RegExp groups:
// 1 - scheme, hostname, ?port
Expand All @@ -153,9 +154,9 @@ var ParsedURL = /** @class */ (function () {
var pathRegex = /(\/[^#?]*)?/;
var queryRegex = /(?:\?([^#]*))?/;
var fragmentRegex = /(?:#(.*))?/;
globalThis.cdt.Common.ParsedURL._urlRegexInstance = new RegExp('^(' + schemeRegex.source + userRegex.source + hostRegex.source + portRegex.source + ')' + pathRegex.source +
ParsedURL._urlRegexInstance = new RegExp('^(' + schemeRegex.source + userRegex.source + hostRegex.source + portRegex.source + ')' + pathRegex.source +
queryRegex.source + fragmentRegex.source + '$');
return globalThis.cdt.Common.ParsedURL._urlRegexInstance;
return ParsedURL._urlRegexInstance;
};
/**
* @param {string} url
Expand All @@ -178,7 +179,7 @@ var ParsedURL = /** @class */ (function () {
* @return {string}
*/
ParsedURL.extractExtension = function (url) {
url = globalThis.cdt.Common.ParsedURL.urlWithoutHash(url);
url = ParsedURL.urlWithoutHash(url);
var indexOfQuestionMark = url.indexOf('?');
if (indexOfQuestionMark !== -1) {
url = url.substr(0, indexOfQuestionMark);
Expand Down Expand Up @@ -262,7 +263,7 @@ var ParsedURL = /** @class */ (function () {
*/
ParsedURL.splitLineAndColumn = function (string) {
// Only look for line and column numbers in the path to avoid matching port numbers.
var beforePathMatch = string.match(globalThis.cdt.Common.ParsedURL._urlRegex());
var beforePathMatch = string.match(ParsedURL._urlRegex());
var beforePath = '';
var pathAndAfter = string;
if (beforePathMatch) {
Expand Down Expand Up @@ -389,21 +390,14 @@ var ParsedURL = /** @class */ (function () {
};
return ParsedURL;
}());
exports["default"] = ParsedURL;
exports.default = ParsedURL;
/**
* @return {?Common.ParsedURL}
*/
String.prototype.asParsedURL = function () {
var parsedURL = new globalThis.cdt.Common.ParsedURL(this.toString());
var parsedURL = new ParsedURL(this.toString());
if (parsedURL.isValid) {
return parsedURL;
}
return null;
};
/* Legacy exported object */
globalThis.cdt.Common = globalThis.cdt.Common || {};
globalThis.cdt.Common = globalThis.cdt.Common || {};
/**
* @constructor
*/
globalThis.cdt.Common.ParsedURL = ParsedURL;
31 changes: 9 additions & 22 deletions lighthouse-core/lib/cdt/generated/sdk/SourceMap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-nocheck
// generated by build-cdt-lib.js
const Common = require('../../Common.js')
"use strict";
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
Expand Down Expand Up @@ -74,7 +75,7 @@ var SourceMap = /** @class */ (function () {
};
return SourceMap;
}());
exports["default"] = SourceMap;
exports.default = SourceMap;
/**
* @unrestricted
*/
Expand Down Expand Up @@ -193,7 +194,7 @@ var TextSourceMap = /** @class */ (function () {
if (this._json.sections) {
var sectionWithURL = !!this._json.sections.find(function (section) { return !!section.url; });
if (sectionWithURL) {
globalThis.cdt.Common.console.warn("SourceMap \"" + sourceMappingURL + "\" contains unsupported \"URL\" field in one of its sections.");
Common.console.warn("SourceMap \"" + sourceMappingURL + "\" contains unsupported \"URL\" field in one of its sections.");
}
}
this._eachSection(this._parseSources.bind(this));
Expand All @@ -207,7 +208,7 @@ var TextSourceMap = /** @class */ (function () {
TextSourceMap.load = function (sourceMapURL, compiledURL) {
var callback;
var promise = new Promise(function (fulfill) { return callback = fulfill; });
globalThis.cdt.SDK.multitargetNetworkManager.loadResource(sourceMapURL, contentLoaded);
SDK.multitargetNetworkManager.loadResource(sourceMapURL, contentLoaded);
return promise;
/**
* @param {number} statusCode
Expand All @@ -228,7 +229,7 @@ var TextSourceMap = /** @class */ (function () {
}
catch (e) {
console.error(e);
globalThis.cdt.Common.console.warn('DevTools failed to parse SourceMap: ' + sourceMapURL);
Common.console.warn('DevTools failed to parse SourceMap: ' + sourceMapURL);
callback(null);
}
}
Expand Down Expand Up @@ -263,9 +264,9 @@ var TextSourceMap = /** @class */ (function () {
TextSourceMap.prototype.sourceContentProvider = function (sourceURL, contentType) {
var info = this._sourceInfos.get(sourceURL);
if (info.content) {
return globalThis.cdt.Common.StaticContentProvider.fromString(sourceURL, contentType, info.content);
return Common.StaticContentProvider.fromString(sourceURL, contentType, info.content);
}
return new globalThis.cdt.SDK.CompilerSourceMappingContentProvider(sourceURL, contentType);
return new SDK.CompilerSourceMappingContentProvider(sourceURL, contentType);
};
/**
* @override
Expand Down Expand Up @@ -400,10 +401,10 @@ var TextSourceMap = /** @class */ (function () {
}
for (var i = 0; i < sourceMap.sources.length; ++i) {
var href = sourceRoot + sourceMap.sources[i];
var url = globalThis.cdt.Common.ParsedURL.completeURL(this._baseURL, href) || href;
var url = Common.ParsedURL.completeURL(this._baseURL, href) || href;
var source = sourceMap.sourcesContent && sourceMap.sourcesContent[i];
if (url === this._compiledURL && source) {
url += globalThis.cdt.Common.UIString('? [sm]');
url += Common.UIString('? [sm]');
}
this._sourceInfos.set(url, new TextSourceMap.SourceInfo(source, null));
sourcesList.push(url);
Expand Down Expand Up @@ -562,17 +563,3 @@ TextSourceMap.SourceInfo = /** @class */ (function () {
return SourceInfo;
}());
TextSourceMap._sourcesListSymbol = Symbol('sourcesList');
/* Legacy exported object */
globalThis.cdt.SDK = globalThis.cdt.SDK || {};
/* Legacy exported object */
globalThis.cdt.SDK = globalThis.cdt.SDK || {};
/** @interface */
globalThis.cdt.SDK.SourceMap = SourceMap;
/** @constructor */
globalThis.cdt.SDK.SourceMapV3 = SourceMapV3;
/** @constructor */
globalThis.cdt.SDK.SourceMapEntry = SourceMapEntry;
/** @constructor */
globalThis.cdt.SDK.TextSourceMap = TextSourceMap;
/** @constructor */
globalThis.cdt.SDK.SourceMap.EditResult = EditResult;

0 comments on commit 2f80d5a

Please sign in to comment.