From 93852d4da92b070b1537d439edf99ea0018e72d6 Mon Sep 17 00:00:00 2001 From: Tao Zhou Date: Wed, 6 Nov 2019 16:11:09 +0100 Subject: [PATCH] Move externs to types folder Also support auto-load all types from types folder for template test. Change-Id: I113d9dd69b6f1b1e016dcf0b00e942446bbb6a6f --- .../app/template_test_srcs/template_test.js | 101 ++++++------------ polygerrit-ui/app/types/custom-externs.js | 63 +++++++++++ 2 files changed, 94 insertions(+), 70 deletions(-) create mode 100644 polygerrit-ui/app/types/custom-externs.js diff --git a/polygerrit-ui/app/template_test_srcs/template_test.js b/polygerrit-ui/app/template_test_srcs/template_test.js index f7d9c43814..d715d7d442 100644 --- a/polygerrit-ui/app/template_test_srcs/template_test.js +++ b/polygerrit-ui/app/template_test_srcs/template_test.js @@ -1,47 +1,6 @@ const fs = require('fs'); const twinkie = require('fried-twinkie'); -/** - * For the purposes of template type checking, externs should be added for - * anything set on the window object. Note that sub-properties of these - * declared properties are considered something separate. - */ -const EXTERN_NAMES = [ - 'Gerrit', - 'GrAnnotation', - 'GrAttributeHelper', - 'GrChangeActionsInterface', - 'GrChangeReplyInterface', - 'GrDiffBuilder', - 'GrDiffBuilderImage', - 'GrDiffBuilderSideBySide', - 'GrDiffBuilderUnified', - 'GrDiffGroup', - 'GrDiffLine', - 'GrDomHooks', - 'GrEditConstants', - 'GrEtagDecorator', - 'GrFileListConstants', - 'GrGapiAuth', - 'GrGerritAuth', - 'GrLinkTextParser', - 'GrPluginEndpoints', - 'GrPopupInterface', - 'GrRangeNormalizer', - 'GrReporting', - 'GrReviewerUpdatesParser', - 'GrCountStringFormatter', - 'GrThemeApi', - 'SiteBasedCache', - 'FetchPromisesCache', - 'GrRestApiHelper', - 'GrDisplayNameUtils', - 'GrReviewerSuggestionsProvider', - 'moment', - 'page', - 'util', -]; - fs.readdir('./polygerrit-ui/temp/behaviors/', (err, data) => { if (err) { console.log('error /polygerrit-ui/temp/behaviors/ directory'); @@ -89,37 +48,39 @@ fs.readdir('./polygerrit-ui/temp/behaviors/', (err, data) => { mappings = mappingSpecificFile; } - additionalSources.push({ - path: 'custom-externs.js', - src: '/** @externs */' + - EXTERN_NAMES.map( name => { return `var ${name};`; }).join(' '), - }); - - /** Types in Gerrit */ - additionalSources.push({ - path: './polygerrit-ui/app/types/types.js', - src: fs.readFileSync( - `./polygerrit-ui/app/types/types.js`, 'utf-8'), - }); - - const toCheck = []; - for (key of Object.keys(mappings)) { - if (mappings[key].html && mappings[key].js) { - toCheck.push({ - htmlSrcPath: mappings[key].html, - jsSrcPath: mappings[key].js, - jsModule: 'polygerrit.' + mappings[key].package, + /** + * Types in Gerrit. + * All types should be under `./polygerrit-ui/app/types` folder and end with `js`. + */ + fs.readdir('./polygerrit-ui/app/types/', (err, typeFiles) => { + for (const typeFile of typeFiles) { + if (!typeFile.endsWith('.js')) continue; + additionalSources.push({ + path: `./polygerrit-ui/app/types/${typeFile}`, + src: fs.readFileSync( + `./polygerrit-ui/app/types/${typeFile}`, 'utf-8'), }); } - } - twinkie.checkTemplate(toCheck, additionalSources) - .then(() => {}, joinedErrors => { - if (joinedErrors) { + const toCheck = []; + for (key of Object.keys(mappings)) { + if (mappings[key].html && mappings[key].js) { + toCheck.push({ + htmlSrcPath: mappings[key].html, + jsSrcPath: mappings[key].js, + jsModule: 'polygerrit.' + mappings[key].package, + }); + } + } + + twinkie.checkTemplate(toCheck, additionalSources) + .then(() => {}, joinedErrors => { + if (joinedErrors) { + process.exit(1); + } + }).catch(e => { + console.error(e); process.exit(1); - } - }).catch(e => { - console.error(e); - process.exit(1); - }); + }); + }); }); diff --git a/polygerrit-ui/app/types/custom-externs.js b/polygerrit-ui/app/types/custom-externs.js new file mode 100644 index 0000000000..afa094c02c --- /dev/null +++ b/polygerrit-ui/app/types/custom-externs.js @@ -0,0 +1,63 @@ +/** + * @license + * Copyright (C) 2019 The Android Open Source Project + * + * 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. + */ + +/** + * For the purposes of template type checking, externs should be added for + * anything set on the window object. Note that sub-properties of these + * declared properties are considered something separate. + * + * This file is only for template type checking, not used in Gerrit code. + */ + +/* eslint-disable no-var */ +/* eslint-disable no-unused-vars */ +/** @externs */ +// @unused + +var Gerrit; +var GrAnnotation; +var GrAttributeHelper; +var GrChangeActionsInterface; +var GrChangeReplyInterface; +var GrDiffBuilder; +var GrDiffBuilderImage; +var GrDiffBuilderSideBySide; +var GrDiffBuilderUnified; +var GrDiffGroup; +var GrDiffLine; +var GrDomHooks; +var GrEditConstants; +var GrEtagDecorator; +var GrFileListConstants; +var GrGapiAuth; +var GrGerritAuth; +var GrLinkTextParser; +var GrPluginEndpoints; +var GrPopupInterface; +var GrRangeNormalizer; +var GrReporting; +var GrReviewerUpdatesParser; +var GrCountStringFormatter; +var GrThemeApi; +var SiteBasedCache; +var FetchPromisesCache; +var GrRestApiHelper; +var GrDisplayNameUtils; +var GrReviewerSuggestionsProvider; +var moment; +var page; +var util; \ No newline at end of file