Skip to content

Commit

Permalink
create tsconfig and fix lots of type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rileyajones committed Feb 9, 2021
1 parent 01abe80 commit f6a4aa7
Show file tree
Hide file tree
Showing 40 changed files with 320 additions and 112 deletions.
3 changes: 2 additions & 1 deletion build-system/common/check-package-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function yellow(text) {
* package manager being used is determined.
**/
function ensureNpm() {
// @ts-ignore: Object is possibly 'undefined'.
if (!process.env.npm_execpath.includes('npm')) {
console.log(npmInfoMessage);
process.exit(1);
Expand All @@ -99,7 +100,7 @@ function ensureNpm() {
/**
* Check the node version and print a warning if it is not the latest LTS.
*
* @return {Promise}
* @return {Promise<void>}
**/
function checkNodeVersion() {
const nodeVersion = getStdout('node --version').trim();
Expand Down
2 changes: 1 addition & 1 deletion build-system/common/ctrlcHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ exports.createCtrlcHandler = function (command) {
/**
* Exits the Ctrl C handler process.
*
* @param {string} handlerProcess
* @param {string|number} handlerProcess
*/
exports.exitCtrlcHandler = function (handlerProcess) {
const exitCmd = killCmd + ' ' + handlerProcess + ' ' + killSuffix;
Expand Down
6 changes: 3 additions & 3 deletions build-system/common/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const shellCmd = process.platform == 'win32' ? 'cmd' : '/bin/bash';
* object.
*
* @param {string} cmd
* @param {?Object} options
* @param {?Object=} options
* @return {!Object}
*/
function exec(cmd, options) {
Expand All @@ -44,7 +44,7 @@ function exec(cmd, options) {
*
* @param {string} script
* @param {?Object} options
* @return {!ChildProcess}
* @return {!childProcess.ChildProcessWithoutNullStreams}
*/
function execScriptAsync(script, options) {
return childProcess.spawn(script, {shell: shellCmd, ...options});
Expand All @@ -54,7 +54,7 @@ function execScriptAsync(script, options) {
* Executes the provided command, and terminates the program in case of failure.
*
* @param {string} cmd
* @param {?Object} options
* @param {?Object=} options
*/
function execOrDie(cmd, options) {
const p = exec(cmd, options);
Expand Down
2 changes: 1 addition & 1 deletion build-system/common/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function logOnSameLine(...messages) {
if (!isCiBuild() && process.stdout.isTTY) {
process.stdout.moveCursor(0, -1);
process.stdout.cursorTo(0);
process.stdout.clearLine();
process.stdout.clearLine(0);
}
log(...messages);
}
Expand Down
2 changes: 1 addition & 1 deletion build-system/common/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function getOutput(cmd, options = {}) {
/**
* Executes the provided command, returning its stdout.
* @param {string} cmd
* @param {?Object} options
* @param {?Object=} options
* @return {string}
*/
function getStdout(cmd, options) {
Expand Down
3 changes: 2 additions & 1 deletion build-system/compile/check-for-unknown-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'use strict';

const through = require('through2');
const Stream = require('stream');
const {log} = require('../common/logging');
const {red, cyan, yellow} = require('ansi-colors');

Expand All @@ -29,7 +30,7 @@ const {red, cyan, yellow} = require('ansi-colors');
exports.checkForUnknownDeps = function () {
const regex = /[\w$]*module\$[\w$]+/;

return through.obj(function (file, encoding, cb) {
return through.obj(function (file, _encoding, cb) {
const contents = file.contents.toString();
if (!contents.includes('module$')) {
// Fast check, since regexes can backtrack like crazy.
Expand Down
3 changes: 2 additions & 1 deletion build-system/compile/closure-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
const closureCompiler = require('@ampproject/google-closure-compiler');
const path = require('path');
const pumpify = require('pumpify');
const { default: Stream, Writable } = require('stream');
const sourcemaps = require('gulp-sourcemaps');
const {cyan, red, yellow} = require('ansi-colors');
const {EventEmitter} = require('events');
Expand Down Expand Up @@ -114,7 +115,7 @@ function makeSourcemapsRelative(closureStream) {

/**
* @param {Array<string>} compilerOptions
* @return {stream.Writable}
* @return {Writable}
*/
function gulpClosureCompile(compilerOptions) {
const pluginOptions = {
Expand Down
16 changes: 12 additions & 4 deletions build-system/compile/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ let inProgress = 0;
const MAX_PARALLEL_CLOSURE_INVOCATIONS =
parseInt(argv.closure_concurrency, 10) || cpus().length;

// Compiles AMP with the closure compiler. This is intended only for
// production use. During development we intend to continue using
// babel, as it has much faster incremental compilation.
/**
* Compiles AMP with the closure compiler. This is intended only for
* production use. During development we intend to continue using
* babel, as it has much faster incremental compilation.
*
* @return {Promise<void>}
*/
async function closureCompile(
entryModuleFilename,
outputDir,
Expand Down Expand Up @@ -211,6 +215,10 @@ function compile(
}
externs.push('build-system/externs/amp.multipass.extern.js');

/**
* TODO(#28387) write a type for this.
* @type {*}
*/
/* eslint "google-camelcase/google-camelcase": 0*/
const compilerOptions = {
compilation_level: options.compilationLevel || 'SIMPLE_OPTIMIZATIONS',
Expand Down Expand Up @@ -377,7 +385,7 @@ function compile(
function printClosureConcurrency() {
log(
green('Using up to'),
cyan(MAX_PARALLEL_CLOSURE_INVOCATIONS),
cyan(MAX_PARALLEL_CLOSURE_INVOCATIONS.toString()),
green('concurrent invocations of closure compiler.')
);
if (!argv.closure_concurrency) {
Expand Down
3 changes: 1 addition & 2 deletions build-system/compile/internal-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ function getVersion() {
`HEAD~${numberOfCherryPicks}`
).slice(0, -2);

numberOfCherryPicks = String(numberOfCherryPicks).padStart(3, '0');
return `${lastCommitFormattedTime}${numberOfCherryPicks}`;
return `${lastCommitFormattedTime}${String(numberOfCherryPicks).padStart(3, '0')}`;
}

// Used to e.g. references the ads binary from the runtime to get version lock.
Expand Down
4 changes: 2 additions & 2 deletions build-system/compile/post-closure-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const argv = require('minimist')(process.argv.slice(2));
const babel = require('@babel/core');
const path = require('path');
const remapping = require('@ampproject/remapping');
const remapping = require('@ampproject/remapping').default;
const terser = require('terser');
const through = require('through2');
const {debug, CompilationLifecycles} = require('./debug-compilation-lifecycle');
Expand Down Expand Up @@ -75,7 +75,7 @@ async function terserMinify(code, filename) {
* @return {!Promise}
*/
exports.postClosureBabel = function () {
return through.obj(async function (file, enc, next) {
return through.obj(async function (file, _enc, next) {
if ((!argv.esm && !argv.sxg) || path.extname(file.path) === '.map') {
debug(
CompilationLifecycles['complete'],
Expand Down
4 changes: 2 additions & 2 deletions build-system/compile/pre-closure-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ function preClosureBabel() {
*
* @param {Error} err
* @param {string} outputFilename
* @param {?Object} options
* @param {?Function} resolve
* @param {?Object=} options
* @param {?Function=} resolve
*/
function handlePreClosureError(err, outputFilename, options, resolve) {
log(red('ERROR:'), err.message, '\n');
Expand Down
2 changes: 1 addition & 1 deletion build-system/compile/sanitize.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const through = require('through2');
const argv = require('minimist')(process.argv.slice(2));

exports.sanitize = function () {
return through.obj((file, enc, next) => {
return through.obj((file, _enc, next) => {
if (!argv.sanitize_vars_for_diff) {
return next(null, file);
}
Expand Down
4 changes: 3 additions & 1 deletion build-system/compile/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ exports.transpileTs = async function (srcDir, srcFilename) {
}
return fileName;
};
const transformerHost = {
// TODO(#28387) fix this typing.
/** @type {*} */
const transformerHost = {
host: compilerHost,
options: tsOptions,
pathToModuleName,
Expand Down
8 changes: 8 additions & 0 deletions build-system/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare global {
interface Error {
showStack?: boolean;
status?: string;
}
}

export { }
2 changes: 1 addition & 1 deletion build-system/server/amp4test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ app.get('/a4a/:bid', (req, res) => {
});

/**
* @param {{body: string, css: string|undefined, extensions: Array<string>|undefined, head: string|undefined, spec: string|undefined}} config
* @param {{body: string, css?: string|undefined, extensions: Array<string>|undefined, head?: string|undefined, spec?: string|undefined, mode?: string|undefined}} config
* @return {string}
*/
function composeDocument(config) {
Expand Down
4 changes: 2 additions & 2 deletions build-system/server/app-index/amphtml-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ const addRequiredExtensionsToHead = (
addExtension(name, {isTemplate: true, ...defaultConf});

Array.from(matchIterator(componentTagNameRegex, docStr))
.map(([unusedFullMatch, tagName]) => componentExtensionName(tagName))
.map(([, tagName]) => componentExtensionName(tagName))
.forEach(addExtension);

Array.from(matchIterator(templateTagTypeRegex, docStr))
.map(([unusedFullMatch, type]) => type)
.map(([, type]) => type)
.forEach(addTemplate);

// TODO(alanorozco): Too greedy. Parse "on" attributes instead.
Expand Down
1 change: 1 addition & 0 deletions build-system/server/app-index/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const ExamplesSelectModeOptional = ({basepath, selectModePrefix}) =>
selectModePrefix,
});

/** @param {{ name: string, href: string, boundHref?: string|undefined }} config */
const FileListItem = ({name, href, boundHref}) =>
html`
<div class="file-link-container" role="listitem">
Expand Down
4 changes: 2 additions & 2 deletions build-system/server/app-index/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ const joinFragments = (fragments, renderer = identity) =>

/**
* pass-through for syntax highlighting
* @param {!Array<string>} strings
* @param {!Array<string>|TemplateStringsArray} strings
* @param {...*} values
* @return {string}
*/
const html = (strings, ...values) =>
joinFragments(strings, (string, i) => string + (values[i] || ''));
joinFragments(Array.from(strings), (string, i) => string + (values[i] || ''));

module.exports = {html, joinFragments};
18 changes: 9 additions & 9 deletions build-system/server/app-index/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ const HeaderBackToMainLink = () => html` <a href="/">← Back to main</a> `;

const ProxyFormOptional = ({isMainPage}) => (isMainPage ? ProxyForm() : '');

function renderTemplate(opt_params) {
const {basepath, css, isMainPage, fileSet, serveMode, selectModePrefix} = {
basepath: '/',
isMainPage: false,
fileSet: [],
serveMode: 'default',
selectModePrefix: '/',
...(opt_params || {}),
};
function renderTemplate(opt_params = {}) {
const {
basepath = '/',
css,
isMainPage = false,
fileSet = [],
serveMode = 'default',
selectModePrefix = '/'
} = opt_params;

const body = joinFragments([
html`
Expand Down
2 changes: 1 addition & 1 deletion build-system/server/app-index/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const getBoundAttr = ({outerHTML}, attr) => {
if (!match) {
return;
}
const [_, valuePart] = match;
const [, valuePart] = match;
if (valuePart.charAt(0) == '"' ||
valuePart.charAt(0) == '\'') {
return valuePart.substring(1, valuePart.length - 1);
Expand Down
1 change: 1 addition & 0 deletions build-system/server/app-index/test/test-amphtml-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('devdash', () => {
});

it('fails without min required fields', () => {
// @ts-ignore
expect(() => AmpDoc({})).to.throw;
});

Expand Down
2 changes: 1 addition & 1 deletion build-system/server/app-index/test/test-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe('devdash', () => {
it('joins simple fragments', () => {
expect(joinFragments(['a', 'b', 'c'])).to.equal('abc');
});

it('joins mapped fragments', () => {
// @ts-ignore
expect(joinFragments([1, 2, 3], a => a + 1)).to.equal('234');
});

Expand Down

0 comments on commit f6a4aa7

Please sign in to comment.