Move externs to types folder

Also support auto-load all types from types folder for template test.

Change-Id: I113d9dd69b6f1b1e016dcf0b00e942446bbb6a6f
This commit is contained in:
Tao Zhou
2019-11-06 16:11:09 +01:00
parent d9a9a99071
commit 93852d4da9
2 changed files with 94 additions and 70 deletions

View File

@@ -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);
});
});
});
});

View File

@@ -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;