Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️🏗 Allow dead-code-eliminating config.urls #37998

Merged
merged 17 commits into from
Apr 5, 2022
2 changes: 1 addition & 1 deletion 3p/integration-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {IntegrationAmpContext} from './ampcontext-integration';
import {installEmbedStateListener, manageWin} from './environment';
import {getAmpConfig, getEmbedType, getLocation} from './frame-metadata';

import {urls} from '../src/config';
import * as urls from '../src/config/urls';
import {getSourceUrl, isProxyOrigin, parseUrlDeprecated} from '../src/url';

/**
Expand Down
2 changes: 1 addition & 1 deletion ads/alp/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {parseQueryString} from '#core/types/string/url';

import {dev} from '#utils/log';

import {urls} from '../../src/config';
import * as urls from '../../src/config/urls';
import {openWindowDialog} from '../../src/open-window-dialog';
import {
addParamToUrl,
Expand Down
1 change: 1 addition & 0 deletions build-system/babel-config/minified-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function getMinifiedConfig(buildFor = 'preact') {

const plugins = [
'optimize-objstr',
'./build-system/babel-plugins/babel-plugin-deep-pure',
'./build-system/babel-plugins/babel-plugin-mangle-object-values',
'./build-system/babel-plugins/babel-plugin-jsx-style-object',
getImportResolverPlugin(buildFor),
Expand Down
84 changes: 84 additions & 0 deletions build-system/babel-plugins/babel-plugin-deep-pure/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const pureFnName = 'pure';
const pureFnImportSource = '#core/types/pure';

/**
* Deeply adds #__PURE__ comments to an expression passed to a function named
* `pure()`.
* @return {import('@babel/core').PluginObj}
*/
module.exports = function () {
alanorozco marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param {import('@babel/core').NodePath<import('@babel/types').CallExpression>} path
* @return {boolean}
*/
function isPureFnCallExpression(path) {
return (
path.node.arguments.length === 1 &&
path.get('callee').isIdentifier({name: pureFnName})
alanorozco marked this conversation as resolved.
Show resolved Hide resolved
);
}

/** @param {import('@babel/core').NodePath} path */
function addPureComment(path) {
path.addComment('leading', ' #__PURE__ ');
}

/** @param {import('@babel/core').NodePath<import('@babel/types').CallExpression>} path */
function replaceWithFirstArgument(path) {
path.replaceWith(path.node.arguments[0]);
}

/**
* @param {null | undefined | import('@babel/core').NodePath} path
* @return {path is import('@babel/types').ImportSpecifier}
*/
function isPureFnImportSpecifier(path) {
if (!path?.isImportSpecifier()) {
return false;
}
const {parentPath} = path;
return (
parentPath.isImportDeclaration() &&
parentPath.get('source').isStringLiteral({value: pureFnImportSource})
);
}

return {
name: 'deep-pure',
visitor: {
Program: {
enter(path) {
const pureFnBinding = path.scope.getBinding(pureFnName);
if (isPureFnImportSpecifier(pureFnBinding?.path)) {
pureFnBinding?.path.remove();
alanorozco marked this conversation as resolved.
Show resolved Hide resolved
} else {
path.skip();
}
},
},
CallExpression(path) {
if (isPureFnCallExpression(path)) {
path.traverse({
NewExpression(path) {
addPureComment(path);
},
CallExpression(path) {
if (isPureFnCallExpression(path)) {
replaceWithFirstArgument(path);
} else {
addPureComment(path);
}
},
MemberExpression(path) {
throw path.buildCodeFrameError(
`${pureFnName}() expressions cannot contain member expressions`
);
},
});
replaceWithFirstArgument(path);
}
path.skip();
},
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import {pure} from '#core/types/pure';
pure(error.memberExpression);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [
"../../../.."
],
"throws": "pure() expressions cannot contain member expressions"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {pure} from 'something other than #core/types/pure';
const ignored = pure(
foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo')
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
"../../../.."
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { pure } from 'something other than #core/types/pure';
const ignored = pure(foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const pure = (v) => v;
const ignored = pure(
foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo')
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
"../../../.."
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const pure = v => v;

const ignored = pure(foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {pure} from '#core/types/pure';
const ignored = foo();
const a = pure(
foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo')
);
const b = pure('foo');
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
"../../../.."
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import '#core/types/pure';
const ignored = foo();
const a = /* #__PURE__ */foo() || /* #__PURE__ */new Bar() || /* #__PURE__ */foo('bar', /* #__PURE__ */bar(), /* #__PURE__ */new Baz()) || 'foo';
const b = 'foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const runner = require('@babel/helper-plugin-test-runner').default;

runner(__dirname);
7 changes: 6 additions & 1 deletion build-system/eslint-rules/no-export-side-effect.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ module.exports = function (context) {
return declarator.init;
})
.filter(function (init) {
return init && /(?:Call|New)Expression/.test(init.type);
return (
init &&
/(?:Call|New)Expression/.test(init.type) &&
// Allow pure()
init.callee?.name !== 'pure'
);
})
.forEach(function (init) {
context.report({
Expand Down
1 change: 1 addition & 0 deletions build-system/eslint-rules/no-import-rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const imports = {
'setStyles',
],
'#core/types/enum': ['mangleObjectValues'],
'#core/types/pure': ['pure'],
'#experiments': ['isExperimentOn'],
'#utils/log': ['user', 'dev'],
'#preact/utils': ['propName', 'tabindexFromProps'],
Expand Down
2 changes: 1 addition & 1 deletion build-system/test-configs/dep-check-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ exports.rules = [
allowlist: [
'3p/**->src/utils/log.js',
'3p/**->src/url.js',
'3p/**->src/config.js',
'3p/**->src/config/urls.js',
'3p/**->src/mode.js',
'3p/polyfills.js->src/polyfills/math-sign.js',
'3p/polyfills.js->src/polyfills/object-assign.js',
Expand Down
6 changes: 3 additions & 3 deletions build-system/test-configs/forbidden-terms.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ const forbiddenTermsGlobal = {
'build-system/tasks/build.js',
'build-system/tasks/default-task.js',
'build-system/tasks/dist.js',
'src/config.js',
'src/config/urls.js',
'src/experiments/index.js',
'src/mode.js',
'src/core/mode/test.js',
Expand Down Expand Up @@ -922,7 +922,7 @@ const forbiddenTermsSrcInclusive = {
'(cdn|3p)\\.ampproject\\.': {
message:
'The CDN domain should typically not be hardcoded in source ' +
'code. Use a property of urls from src/config.js instead.',
'code. Use urls from src/config/urls.js instead.',
allowlist: [
'ads/_a4a-config.js',
'build-system/server/amp4test.js',
Expand All @@ -936,7 +936,7 @@ const forbiddenTermsSrcInclusive = {
'build-system/tasks/performance/helpers.js',
'src/3p-frame.js',
'src/amp-story-player/amp-story-player-impl.js',
'src/config.js',
'src/config/urls.js',
'testing/local-amp-chrome-extension/background.js',
'tools/experiments/experiments.js',
'validator/js/engine/htmlparser-interface.js',
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-a4a/0.1/amp-ad-template-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Services} from '#service';

import {devAssert} from '#utils/log';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {getMode} from '../../../src/mode';
import {
getServiceForDoc,
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-a4a/0.1/amp-ad-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {parseExtensionUrl} from '#service/extension-script';

import {dev} from '#utils/log';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {isSecureUrlDeprecated} from '../../../src/url';

const TAG = 'amp-ad-util';
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-a4a/0.1/head-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {includes} from '#core/types/string';
import {Services} from '#service';
import {parseExtensionUrl} from '#service/extension-script';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {preloadFriendlyIframeEmbedExtensions} from '../../../src/friendly-iframe-embed';
import {getMode} from '../../../src/mode';

Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-a4a/0.1/template-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
mergeExtensionsMetadata,
} from './amp-ad-utils';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {preloadFriendlyIframeEmbedExtensions} from '../../../src/friendly-iframe-embed';

/** @const {string} */
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-access/0.1/login-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Services} from '#service';
import {getData, listen} from '#utils/event-helper';
import {dev, userAssert} from '#utils/log';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {getMode} from '../../../src/mode';
import {openWindowDialog} from '../../../src/open-window-dialog';
import {parseUrlDeprecated} from '../../../src/url';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {endsWith} from '#core/types/string';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';

export class ExternalReorderHeadTransformer {
/** constructor */
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-analytics/0.1/iframe-transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {devAssert, user} from '#utils/log';

import {IframeTransportMessageQueue} from './iframe-transport-message-queue';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {getMode} from '../../../src/mode';

/**
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-analytics/0.1/test/test-iframe-transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {user} from '#utils/log';

import {expectPostMessage} from '#testing/iframe';

import {urls} from '../../../../src/config';
import * as urls from '../../../../src/config/urls';
import {addParamsToUrl} from '../../../../src/url';
import {
IframeTransport,
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-cache-url/0.1/amp-cache-url.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ampToolboxCacheUrl from '@ampproject/toolbox-cache-url';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';

export class AmpCacheUrlService {
/**
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-geo/0.1/amp-geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {dev, user, userAssert} from '#utils/log';
import {GEO_IN_GROUP} from './amp-geo-in-group';
import {ampGeoPresets} from './amp-geo-presets';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {getMode} from '../../../src/mode';

/** @const */
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-geo/0.1/test/test-amp-geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {vsyncForTesting} from '#service/vsync-impl';

import {user} from '#utils/log';

import {urls} from '../../../../src/config';
import * as urls from '../../../../src/config/urls';
import {AmpGeo} from '../amp-geo';
import {GEO_IN_GROUP} from '../amp-geo-in-group';

Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-iframe/0.1/amp-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {IntersectionObserver3pHost} from '#utils/intersection-observer-3p-host';
import {user, userAssert} from '#utils/log';

import {isAdPositionAllowed} from '../../../src/ad-helper';
import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {getConsentDataToForward} from '../../../src/consent';
import {
isAdLike,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Services} from '#service';
import {listen} from '#utils/event-helper';
import {dev, user, userAssert} from '#utils/log';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {getMode} from '../../../src/mode';
import {removeFragment} from '../../../src/url';

Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-mraid/0.1/amp-mraid.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {dev} from '#utils/log';

import {MraidService} from './mraid-service';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {getMode} from '../../../src/mode';

const TAG = 'amp-mraid';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {Services} from '#service';
import {loadPromise} from '#utils/event-helper';
import {dev, devAssert} from '#utils/log';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {listenFor, postMessage} from '../../../src/iframe-helper';
import {getMode} from '../../../src/mode';
import {getServicePromiseForDoc} from '../../../src/service-helpers';
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-script/0.1/amp-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {dev, user, userAssert} from '#utils/log';
import {UserActivationTracker} from './user-activation-tracker';

import {CSS} from '../../../build/amp-script-0.1.css';
import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {getElementServiceForDoc} from '../../../src/element-service';
import {cancellation} from '../../../src/error-reporting';
import {getMode} from '../../../src/mode';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Services} from '#service';

import {userAssert} from '#utils/log';

import {urls} from '../../../src/config';
import * as urls from '../../../src/config/urls';
import {loadScript} from '../../../src/validator-integration';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/3p-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {tryParseJson} from '#core/types/object/json';

import {dev, devAssert, user, userAssert} from '#utils/log';

import {urls} from './config';
import * as urls from './config/urls';
import {getContextMetadata} from './iframe-attributes';
import {assertHttpsUrl, parseUrlDeprecated} from './url';

Expand Down
2 changes: 1 addition & 1 deletion src/amp-story-player/amp-story-player-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {PageScroller} from './page-scroller';

import {cssText} from '../../build/amp-story-player-shadow.css';
import {applySandbox} from '../3p-frame';
import {urls} from '../config';
import * as urls from '../config/urls';
import {getMode} from '../mode';
import {
addParamsToUrl,
Expand Down
Loading