Skip to content

Commit

Permalink
🏗 Enforce JSDoc across build-system/ for complete type-checking (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimha authored May 27, 2021
1 parent 3e8f1fc commit e2d83cc
Show file tree
Hide file tree
Showing 41 changed files with 236 additions and 24 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ module.exports = {
'extensions/**/test-e2e/*.js',
'ads/**/test/**/*.js',
'testing/**/*.js',
'build-system/**/test/*.js',
],
'rules': {
'require-jsdoc': 0,
Expand Down
1 change: 0 additions & 1 deletion build-system/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ module.exports = {
'local/no-module-exports': 0,
'local/no-rest': 0,
'local/no-spread': 0,
'require-jsdoc': 0,
},
};
4 changes: 4 additions & 0 deletions build-system/OWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
pattern: '{package.json,package-lock.json}',
owners: [{name: 'ampproject/wg-infra', requestReviews: false}],
},
{
pattern: 'tsconfig.json',
owners: [{name: 'rileyajones', notify: true}],
},
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ module.exports = function ({template, types: t}) {
return filename.endsWith('.jss.js');
}

/**
* @param {string} name
* @return {string}
*/
function classnameId(name) {
return `\$${name}`;
}
Expand Down Expand Up @@ -238,6 +242,11 @@ module.exports = function ({template, types: t}) {
);
}

/**
* @param {Path} importDeclaration
* @param {string} name
* @return {string}
*/
function getImportIdentifier(importDeclaration, name) {
return addNamed(
importDeclaration,
Expand Down
7 changes: 7 additions & 0 deletions build-system/common/esbuild-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ function getEsbuildBabelPlugin(
transformCache = new TransformCache('.babel-cache', '.js');
}

/**
* @param {string} filename
* @param {string} contents
* @param {string} hash
* @param {Object} babelOptions
* @return {Promise}
*/
async function transformContents(filename, contents, hash, babelOptions) {
if (enableCache) {
const cached = transformCache.get(hash);
Expand Down
4 changes: 3 additions & 1 deletion build-system/common/update-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ function patchShadowDom() {

writeIfUpdated(patchedName, file);
}

/**
* Adds a missing export statement to the preact module.
*/
function patchPreact() {
fs.ensureDirSync('node_modules/preact/dom');
const file = `export { render, hydrate } from 'preact';`;
Expand Down
12 changes: 12 additions & 0 deletions build-system/compile/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ async function closureCompile(
// Rate limit closure compilation to MAX_PARALLEL_CLOSURE_INVOCATIONS
// concurrent processes.
return new Promise(function (resolve, reject) {
/**
* Kicks off the first closure invocation.
*/
function start() {
inProgress++;
compile(
Expand All @@ -97,6 +100,9 @@ async function closureCompile(
);
}

/**
* Keeps track of the invocation count.
*/
function next() {
if (!queue.length) {
return;
Expand All @@ -110,6 +116,9 @@ async function closureCompile(
});
}

/**
* Cleans up the placeholder directories for fake build modules.
*/
function cleanupBuildDir() {
del.sync('build/fake-module');
del.sync('build/patched-module');
Expand Down Expand Up @@ -441,6 +450,9 @@ async function compile(
}
}

/**
* Indicates the current closure concurrency and how to override it.
*/
function printClosureConcurrency() {
log(
green('Using up to'),
Expand Down
19 changes: 17 additions & 2 deletions build-system/compile/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ const path = require('path');
const {getBabelOutputDir} = require('./pre-closure-babel');
const {VERSION: internalRuntimeVersion} = require('./internal-version');

/**
* Computes the base url for sourcemaps. Custom sourcemap URLs have placeholder
* {version} that should be replaced with the actual version. Also, ensures
* that a trailing slash exists.
* @param {Object} options
* @return {string}
*/
function getSourceMapBase(options) {
if (argv.sourcemap_url) {
// Custom sourcemap URLs have placeholder {version} that should be
// replaced with the actual version. Also, ensure trailing slash exists.
return String(argv.sourcemap_url)
.replace(/\{version\}/g, internalRuntimeVersion)
.replace(/([^/])$/, '$1/');
Expand All @@ -35,6 +40,10 @@ function getSourceMapBase(options) {
return `https://raw.githubusercontent.com/ampproject/amphtml/${internalRuntimeVersion}/`;
}

/**
* Updates all filepaths in the sourcemap output.
* @param {Object} sourcemaps
*/
function updatePaths(sourcemaps) {
const babelOutputDir = getBabelOutputDir();
sourcemaps.sources = sourcemaps.sources.map((source) =>
Expand All @@ -47,6 +56,12 @@ function updatePaths(sourcemaps) {
}
}

/**
* Writes the sourcemap output to disk.
* @param {string} sourcemapsFile
* @param {Object} options
* @return {Promise<void>}
*/
async function writeSourcemaps(sourcemapsFile, options) {
const sourcemaps = await fs.readJson(sourcemapsFile);
updatePaths(sourcemaps);
Expand Down
14 changes: 9 additions & 5 deletions build-system/compile/sanitize.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs-extra');
const prettier = require('prettier');

/**
* Sanitizes variable names in minified output to aid in debugging.
* 1. Normalizes the length of all jscomp variables, so that prettier will
* format it the same.
* 2. Strips numbers from the sanitized jscomp variables so that a single extra
* variable doesn't cause thousands of diffs.
* @param {string} file
* @return {Promise<void>}
*/
async function sanitize(file) {
if (!argv.sanitize_vars_for_diff) {
return;
}

const contents = await fs.readFile(file, 'utf-8');
const config = await prettier.resolveConfig(file);
const options = {filepath: file, parser: 'babel', ...config};
// Normalize the length of all jscomp variables, so that prettier will
// format it the same.
const replaced = Object.create(null);
let count = 0;
const presanitize = contents.replace(
Expand All @@ -38,8 +44,6 @@ async function sanitize(file) {
(replaced[match] = `___${String(count++).padStart(6, '0')}___`)
);
const formatted = prettier.format(presanitize, options);
// Finally, strip the numbers from the sanitized jscomp variables. This
// is so that a single extra variable doesn't cause thousands of diffs.
const sanitized = formatted.replace(/___\d+___/g, '______');
await fs.outputFile(file, sanitized);
}
Expand Down
8 changes: 8 additions & 0 deletions build-system/eslint-rules/forbidden-terms-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ module.exports = {
// greatly speeds up this rule.
const fileContent = {};

/**
* @param {string} filename
* @return {string}
*/
function readFileContent(filename) {
try {
return (
Expand All @@ -42,6 +46,10 @@ module.exports = {
}
}

/**
* @param {Object} fixer
* @param {Node} node
*/
function* removeFromArray(fixer, node) {
const {text} = context.getSourceCode();
let {start} = node;
Expand Down
6 changes: 6 additions & 0 deletions build-system/pr-check/bundle-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ const {Targets, buildTargetsInclude} = require('./build-targets');

const jobName = 'bundle-size.js';

/**
* Steps to run during push builds.
*/
function pushBuildWorkflow() {
timedExecOrDie('amp dist --noconfig --esm');
timedExecOrDie('amp dist --noconfig');
timedExecOrDie('amp bundle-size --on_push_build');
}

/**
* Steps to run during PR builds.
*/
function prBuildWorkflow() {
if (buildTargetsInclude(Targets.RUNTIME)) {
timedExecOrDie('amp dist --noconfig --esm');
Expand Down
4 changes: 4 additions & 0 deletions build-system/pr-check/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const {timedExecOrDie} = require('./utils');

const jobName = 'checks.js';

/**
* Steps to run during push builds.
*/
function pushBuildWorkflow() {
timedExecOrDie('amp presubmit');
timedExecOrDie('amp check-invalid-whitespaces');
Expand Down Expand Up @@ -53,6 +56,7 @@ function pushBuildWorkflow() {
}

/**
* Steps to run during PR builds.
* @return {Promise<void>}
*/
async function prBuildWorkflow() {
Expand Down
5 changes: 4 additions & 1 deletion build-system/pr-check/cross-browser-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,17 @@ function runUnitTestsForPlatform() {
);
}
}

/**
* Steps to run during push builds.
*/
function pushBuildWorkflow() {
runUnitTestsForPlatform();
timedExecOrDie('amp dist --fortesting');
runIntegrationTestsForPlatform();
}

/**
* Steps to run during PR builds.
* @return {Promise<void>}
*/
async function prBuildWorkflow() {
Expand Down
6 changes: 6 additions & 0 deletions build-system/pr-check/e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const {Targets, buildTargetsInclude} = require('./build-targets');

const jobName = 'e2e-tests.js';

/**
* Steps to run during push builds.
*/
function pushBuildWorkflow() {
try {
timedExecOrThrow(
Expand All @@ -44,6 +47,9 @@ function pushBuildWorkflow() {
}
}

/**
* Steps to run during PR builds.
*/
function prBuildWorkflow() {
if (buildTargetsInclude(Targets.RUNTIME, Targets.E2E_TEST)) {
timedExecOrDie('amp e2e --nobuild --headless --compiled');
Expand Down
6 changes: 6 additions & 0 deletions build-system/pr-check/experiment-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const {Targets, buildTargetsInclude} = require('./build-targets');

const jobName = `${experiment}-build.js`;

/**
* Steps to run during push builds.
*/
function pushBuildWorkflow() {
// Note that if config is invalid, this build would have been skipped by CircleCI.
const config = getExperimentConfig(experiment);
Expand All @@ -39,6 +42,9 @@ function pushBuildWorkflow() {
storeExperimentBuildToWorkspace(experiment);
}

/**
* Steps to run during PR builds.
*/
function prBuildWorkflow() {
if (
buildTargetsInclude(
Expand Down
6 changes: 6 additions & 0 deletions build-system/pr-check/experiment-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,18 @@ function runExperimentTests(config) {
}
}

/**
* Steps to run during push builds.
*/
function pushBuildWorkflow() {
// Note that if config is invalid, this build would have been skipped by CircleCI.
const config = getExperimentConfig(experiment);
runExperimentTests(config);
}

/**
* Steps to run during PR builds.
*/
function prBuildWorkflow() {
if (
buildTargetsInclude(
Expand Down
6 changes: 6 additions & 0 deletions build-system/pr-check/module-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ const {Targets, buildTargetsInclude} = require('./build-targets');

const jobName = 'module-build.js';

/**
* Steps to run during push builds.
*/
function pushBuildWorkflow() {
timedExecOrDie('amp dist --esm --fortesting');
storeModuleBuildToWorkspace();
}

/**
* Steps to run during PR builds.
*/
function prBuildWorkflow() {
// TODO(#31102): This list must eventually match the same buildTargets check
// found in pr-check/nomodule-build.js as we turn on the systems that
Expand Down
9 changes: 9 additions & 0 deletions build-system/pr-check/module-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const {Targets, buildTargetsInclude} = require('./build-targets');

const jobName = 'module-tests.js';

/**
* Adds a canary or prod config string to all esm and non-esm minified targets.
*/
function prependConfig() {
const targets = MINIFIED_TARGETS.flatMap((target) => [
`dist/${target}.js`,
Expand All @@ -37,11 +40,17 @@ function prependConfig() {
);
}

/**
* Steps to run during push builds.
*/
function pushBuildWorkflow() {
prependConfig();
timedExecOrDie('amp integration --nobuild --compiled --headless --esm');
}

/**
* Steps to run during PR builds.
*/
function prBuildWorkflow() {
if (buildTargetsInclude(Targets.RUNTIME, Targets.INTEGRATION_TEST)) {
prependConfig();
Expand Down
4 changes: 4 additions & 0 deletions build-system/pr-check/nomodule-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ const {Targets, buildTargetsInclude} = require('./build-targets');

const jobName = 'nomodule-build.js';

/**
* Steps to run during push builds.
*/
function pushBuildWorkflow() {
timedExecOrDie('amp dist --fortesting');
storeNomoduleBuildToWorkspace();
}

/**
* Steps to run during PR builds.
* @return {Promise<void>}
*/
async function prBuildWorkflow() {
Expand Down
Loading

0 comments on commit e2d83cc

Please sign in to comment.