Merge pull request #409 from toasted-nutbread/update-global-declaration-format

Update global declaration format
This commit is contained in:
toasted-nutbread 2020-03-14 13:48:50 -04:00 committed by GitHub
commit 1cacbb6202
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
39 changed files with 399 additions and 71 deletions

View File

@ -63,7 +63,7 @@
"semi-spacing": ["error", {"before": false, "after": true}],
"space-in-parens": ["error", "never"],
"space-unary-ops": "error",
"spaced-comment": ["error", "always", {"markers": ["global"]}],
"spaced-comment": ["error", "always"],
"switch-colon-spacing": ["error", {"after": true, "before": false}],
"template-curly-spacing": ["error", "never"],
"template-tag-spacing": ["error", "never"],

View File

@ -16,7 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global requestJson*/
/* global
* requestJson
*/
/*
* AnkiConnect

View File

@ -16,7 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global jpIsStringEntirelyKana*/
/* global
* jpIsStringEntirelyKana
*/
class AudioUriBuilder {
constructor() {

View File

@ -16,13 +16,32 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global optionsSave, utilIsolate
conditionsTestValue, profileConditionsDescriptor
handlebarsRenderDynamic
requestText, requestJson, optionsLoad
dictConfigured, dictTermsSort, dictEnabledSet
jpConvertReading, jpDistributeFuriganaInflected, jpKatakanaToHiragana
AnkiNoteBuilder, AudioSystem, AudioUriBuilder, Translator, AnkiConnect, AnkiNull, Mecab, BackendApiForwarder, JsonSchema, ClipboardMonitor*/
/* global
* AnkiConnect
* AnkiNoteBuilder
* AnkiNull
* AudioSystem
* AudioUriBuilder
* BackendApiForwarder
* ClipboardMonitor
* JsonSchema
* Mecab
* Translator
* conditionsTestValue
* dictConfigured
* dictEnabledSet
* dictTermsSort
* handlebarsRenderDynamic
* jpConvertReading
* jpDistributeFuriganaInflected
* jpKatakanaToHiragana
* optionsLoad
* optionsSave
* profileConditionsDescriptor
* requestJson
* requestText
* utilIsolate
*/
class Backend {
constructor() {

View File

@ -16,7 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global jpIsStringPartiallyJapanese*/
/* global
* jpIsStringPartiallyJapanese
*/
class ClipboardMonitor extends EventDispatcher {
constructor({getClipboard}) {

View File

@ -16,7 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiCommandExec, apiGetEnvironmentInfo, apiOptionsGet*/
/* global
* apiCommandExec
* apiGetEnvironmentInfo
* apiOptionsGet
*/
function showExtensionInfo() {
const node = document.getElementById('extension-info');

View File

@ -16,7 +16,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global dictFieldSplit, requestJson, JsonSchema, JSZip*/
/* global
* JSZip
* JsonSchema
* dictFieldSplit
* requestJson
*/
class Database {
constructor() {

View File

@ -16,7 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global jpIsCodePointKanji, jpDistributeFurigana, Handlebars*/
/* global
* Handlebars
* jpDistributeFurigana
* jpIsCodePointKanji
*/
function handlebarsEscape(text) {
return Handlebars.Utils.escapeExpression(text);

View File

@ -16,7 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global wanakana*/
/* global
* wanakana
*/
const JP_HALFWIDTH_KATAKANA_MAPPING = new Map([
['ヲ', 'ヲヺ-'],

View File

@ -16,7 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global utilStringHashCode*/
/* global
* utilStringHashCode
*/
/*
* Generic options functions

View File

@ -16,7 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiOptionsGet*/
/* global
* apiOptionsGet
*/
async function searchFrontendSetup() {
await yomichan.prepare();

View File

@ -16,7 +16,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiGetQueryParserTemplatesHtml, TemplateHandler*/
/* global
* TemplateHandler
* apiGetQueryParserTemplatesHtml
*/
class QueryParserGenerator {
constructor() {

View File

@ -16,7 +16,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiTermsFind, apiOptionsSet, apiTextParse, apiTextParseMecab, TextScanner, QueryParserGenerator, docSentenceExtract*/
/* global
* QueryParserGenerator
* TextScanner
* apiOptionsSet
* apiTermsFind
* apiTextParse
* apiTextParseMecab
* docSentenceExtract
*/
class QueryParser extends TextScanner {
constructor(search) {

View File

@ -16,7 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiOptionsSet, apiTermsFind, apiClipboardGet, Display, QueryParser, ClipboardMonitor*/
/* global
* ClipboardMonitor
* Display
* QueryParser
* apiClipboardGet
* apiOptionsSet
* apiTermsFind
*/
class DisplaySearch extends Display {
constructor() {

View File

@ -16,10 +16,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global getOptionsContext, getOptionsMutable, settingsSaveOptions
ankiGetFieldMarkers, ankiGetFieldMarkersHtml
apiOptionsGet, apiTermsFind, apiGetDefaultAnkiFieldTemplates, apiTemplateRender
AnkiNoteBuilder*/
/* global
* AnkiNoteBuilder
* ankiGetFieldMarkers
* ankiGetFieldMarkersHtml
* apiGetDefaultAnkiFieldTemplates
* apiOptionsGet
* apiTemplateRender
* apiTermsFind
* getOptionsContext
* getOptionsMutable
* settingsSaveOptions
*/
function onAnkiFieldTemplatesReset(e) {
e.preventDefault();

View File

@ -16,9 +16,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global getOptionsContext, getOptionsMutable, settingsSaveOptions
utilBackgroundIsolate, utilAnkiGetDeckNames, utilAnkiGetModelNames, utilAnkiGetModelFieldNames
onFormOptionsChanged*/
/* global
* getOptionsContext
* getOptionsMutable
* onFormOptionsChanged
* settingsSaveOptions
* utilAnkiGetDeckNames
* utilAnkiGetModelFieldNames
* utilAnkiGetModelNames
* utilBackgroundIsolate
*/
// Private

View File

@ -16,8 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global getOptionsContext, getOptionsMutable, settingsSaveOptions, apiAudioGetUri
AudioSystem, AudioSourceUI*/
/* global
* AudioSourceUI
* AudioSystem
* apiAudioGetUri
* getOptionsContext
* getOptionsMutable
* settingsSaveOptions
*/
let audioSourceUI = null;
let audioSystem = null;

View File

@ -16,9 +16,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiOptionsGetFull, apiGetEnvironmentInfo, apiGetDefaultAnkiFieldTemplates
utilBackend, utilIsolate, utilBackgroundIsolate, utilReadFileArrayBuffer
optionsGetDefault, optionsUpdateVersion*/
/* global
* apiGetDefaultAnkiFieldTemplates
* apiGetEnvironmentInfo
* apiOptionsGetFull
* optionsGetDefault
* optionsUpdateVersion
* utilBackend
* utilBackgroundIsolate
* utilIsolate
* utilReadFileArrayBuffer
*/
// Exporting

View File

@ -16,7 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global conditionsNormalizeOptionValue*/
/* global
* conditionsNormalizeOptionValue
*/
class ConditionsUI {
static instantiateTemplate(templateSelector) {

View File

@ -16,11 +16,23 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global getOptionsContext, getOptionsMutable, getOptionsFullMutable, settingsSaveOptions, apiOptionsGetFull, apiOptionsGet
utilBackgroundIsolate, utilDatabaseDeleteDictionary, utilDatabaseGetDictionaryInfo, utilDatabaseGetDictionaryCounts
utilDatabasePurge, utilDatabaseImport
storageUpdateStats, storageEstimate
PageExitPrevention*/
/* global
* PageExitPrevention
* apiOptionsGet
* apiOptionsGetFull
* getOptionsContext
* getOptionsFullMutable
* getOptionsMutable
* settingsSaveOptions
* storageEstimate
* storageUpdateStats
* utilBackgroundIsolate
* utilDatabaseDeleteDictionary
* utilDatabaseGetDictionaryCounts
* utilDatabaseGetDictionaryInfo
* utilDatabaseImport
* utilDatabasePurge
*/
let dictionaryUI = null;

View File

@ -16,13 +16,26 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global getOptionsContext, apiOptionsSave
utilBackend, utilIsolate, utilBackgroundIsolate
ankiErrorShown, ankiFieldsToDict
ankiTemplatesUpdateValue, onAnkiOptionsChanged, onDictionaryOptionsChanged
appearanceInitialize, audioSettingsInitialize, profileOptionsSetup, dictSettingsInitialize
ankiInitialize, ankiTemplatesInitialize, storageInfoInitialize, backupInitialize
*/
/* global
* ankiErrorShown
* ankiFieldsToDict
* ankiInitialize
* ankiTemplatesInitialize
* ankiTemplatesUpdateValue
* apiOptionsSave
* appearanceInitialize
* audioSettingsInitialize
* backupInitialize
* dictSettingsInitialize
* getOptionsContext
* onAnkiOptionsChanged
* onDictionaryOptionsChanged
* profileOptionsSetup
* storageInfoInitialize
* utilBackend
* utilBackgroundIsolate
* utilIsolate
*/
function getOptionsMutable(optionsContext) {
return utilBackend().getOptions(

View File

@ -16,7 +16,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiOptionsGet, Popup, PopupProxyHost, Frontend, TextSourceRange*/
/* global
* Frontend
* Popup
* PopupProxyHost
* TextSourceRange
* apiOptionsGet
*/
class SettingsPopupPreview {
constructor() {

View File

@ -16,9 +16,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global getOptionsMutable, getOptionsFullMutable, settingsSaveOptions, apiOptionsGetFull
utilBackgroundIsolate, formWrite
conditionsClearCaches, ConditionsUI, profileConditionsDescriptor*/
/* global
* ConditionsUI
* apiOptionsGetFull
* conditionsClearCaches
* formWrite
* getOptionsFullMutable
* getOptionsMutable
* profileConditionsDescriptor
* settingsSaveOptions
* utilBackgroundIsolate
*/
let currentProfileIndex = 0;
let profileConditionsContainer = null;

View File

@ -16,7 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiGetEnvironmentInfo*/
/* global
* apiGetEnvironmentInfo
*/
function storageBytesToLabeledString(size) {
const base = 1000;

View File

@ -16,12 +16,28 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global requestJson
dictTermsMergeBySequence, dictTagBuildSource, dictTermsMergeByGloss, dictTermsSort, dictTagsSort
dictEnabledSet, dictTermsGroup, dictTermsCompressTags, dictTermsUndupe, dictTagSanitize
jpDistributeFurigana, jpConvertHalfWidthKanaToFullWidth, jpConvertNumericTofullWidth
jpConvertAlphabeticToKana, jpHiraganaToKatakana, jpKatakanaToHiragana, jpIsCodePointJapanese
Database, Deinflector*/
/* global
* Database
* Deinflector
* dictEnabledSet
* dictTagBuildSource
* dictTagSanitize
* dictTagsSort
* dictTermsCompressTags
* dictTermsGroup
* dictTermsMergeByGloss
* dictTermsMergeBySequence
* dictTermsSort
* dictTermsUndupe
* jpConvertAlphabeticToKana
* jpConvertHalfWidthKanaToFullWidth
* jpConvertNumericTofullWidth
* jpDistributeFurigana
* jpHiraganaToKatakana
* jpIsCodePointJapanese
* jpKatakanaToHiragana
* requestJson
*/
class Translator {
constructor() {

View File

@ -16,7 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global TextSourceElement, TextSourceRange, DOM*/
/* global
* DOM
* TextSourceElement
* TextSourceRange
*/
const REGEX_TRANSPARENT_COLOR = /rgba\s*\([^)]*,\s*0(?:\.0+)?\s*\)/;

View File

@ -16,7 +16,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global popupNestedInitialize, apiForward, apiGetMessageToken, Display*/
/* global
* Display
* apiForward
* apiGetMessageToken
* popupNestedInitialize
*/
class DisplayFloat extends Display {
constructor() {

View File

@ -16,7 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global PopupProxyHost, PopupProxy, Frontend*/
/* global
* Frontend
* PopupProxy
* PopupProxyHost
*/
async function main() {
await yomichan.prepare();

View File

@ -16,7 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiGetZoom, apiOptionsGet, apiTermsFind, apiKanjiFind, docSentenceExtract, TextScanner*/
/* global
* TextScanner
* apiGetZoom
* apiKanjiFind
* apiOptionsGet
* apiTermsFind
* docSentenceExtract
*/
class Frontend extends TextScanner {
constructor(popup, ignoreNodes) {

View File

@ -16,7 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiOptionsGet*/
/* global
* apiOptionsGet
*/
let popupNestedInitialized = false;

View File

@ -16,7 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiFrameInformationGet, FrontendApiReceiver, Popup*/
/* global
* FrontendApiReceiver
* Popup
* apiFrameInformationGet
*/
class PopupProxyHost {
constructor() {

View File

@ -16,7 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global FrontendApiSender*/
/* global
* FrontendApiSender
*/
class PopupProxy {
constructor(id, depth, parentId, parentFrameId, url) {

View File

@ -16,7 +16,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global apiInjectStylesheet, apiGetMessageToken*/
/* global
* apiGetMessageToken
* apiInjectStylesheet
*/
class Popup {
constructor(id, depth, frameIdPromise) {

View File

@ -16,7 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*global apiGetDisplayTemplatesHtml, TemplateHandler*/
/* global
* TemplateHandler
* apiGetDisplayTemplatesHtml
*/
class DisplayGenerator {
constructor() {

View File

@ -16,10 +16,24 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global docRangeFromPoint, docSentenceExtract
apiKanjiFind, apiTermsFind, apiNoteView, apiOptionsGet, apiDefinitionsAddable, apiDefinitionAdd
apiScreenshotGet, apiForward, apiAudioGetUri
AudioSystem, DisplayGenerator, WindowScroll, DisplayContext, DOM*/
/* global
* AudioSystem
* DOM
* DisplayContext
* DisplayGenerator
* WindowScroll
* apiAudioGetUri
* apiDefinitionAdd
* apiDefinitionsAddable
* apiForward
* apiKanjiFind
* apiNoteView
* apiOptionsGet
* apiScreenshotGet
* apiTermsFind
* docRangeFromPoint
* docSentenceExtract
*/
class Display {
constructor(spinner, container) {

View File

@ -16,7 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*global docRangeFromPoint, TextSourceRange, DOM*/
/* global
* DOM
* TextSourceRange
* docRangeFromPoint
*/
class TextScanner {
constructor(node, ignoreNodes, ignoreElements, ignorePoints) {

View File

@ -7,7 +7,7 @@
},
"scripts": {
"test": "npm run test-lint && npm run test-code",
"test-lint": "eslint .",
"test-lint": "eslint . && node ./test/lint/global-declarations.js",
"test-code": "node ./test/test-schema.js && node ./test/test-dictionary.js && node ./test/test-database.js && node ./test/test-document.js"
},
"repository": {

View File

@ -0,0 +1,105 @@
/*
* Copyright (C) 2020 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const {getAllFiles} = require('../yomichan-test');
function countOccurences(string, pattern) {
return (string.match(pattern) || []).length;
}
function getNewline(string) {
const count1 = countOccurences(string, /(?:^|[^\r])\n/g);
const count2 = countOccurences(string, /\r\n/g);
const count3 = countOccurences(string, /\r(?:[^\n]|$)/g);
if (count2 > count1) {
return (count3 > count2) ? '\r' : '\r\n';
} else {
return (count3 > count1) ? '\r' : '\n';
}
}
function validateGlobals(fileName, fix) {
const pattern = /\/\*\s*global\s+([\w\W]*?)\*\//g;
const trimPattern = /^[\s,*]+|[\s,*]+$/g;
const splitPattern = /[\s,*]+/;
const source = fs.readFileSync(fileName, {encoding: 'utf8'});
let match;
let first = true;
let endIndex = 0;
let newSource = '';
const newline = getNewline(source);
while ((match = pattern.exec(source)) !== null) {
if (!first) {
console.error(`Encountered more than one global declaration in ${fileName}`);
return false;
}
first = false;
const parts = match[1].replace(trimPattern, '').split(splitPattern);
parts.sort();
const actual = match[0];
const expected = `/* global${parts.map((v) => `${newline} * ${v}`).join('')}${newline} */`;
try {
assert.strictEqual(actual, expected);
} catch (e) {
console.error(`Global declaration error encountered in ${fileName}:`);
console.error(e.message);
if (!fix) {
return false;
}
}
newSource += source.substring(0, match.index);
newSource += expected;
endIndex = match.index + match[0].length;
}
newSource += source.substring(endIndex);
if (fix) {
fs.writeFileSync(fileName, newSource, {encoding: 'utf8'});
}
return true;
}
function main() {
const fix = (process.argv.length >= 2 && process.argv[2] === '--fix');
const directory = path.resolve(__dirname, '..', '..', 'ext');
const pattern = /\.js$/;
const ignorePattern = /[\\/]ext[\\/]mixed[\\/]lib[\\/]/;
const fileNames = getAllFiles(directory, (f) => pattern.test(f) && !ignorePattern.test(f));
for (const fileName of fileNames) {
if (!validateGlobals(fileName, fix)) {
process.exit(-1);
return;
}
}
process.exit(0);
}
if (require.main === module) { main(); }

View File

@ -50,8 +50,29 @@ function createTestDictionaryArchive(dictionary, dictionaryName) {
return archive;
}
function getAllFiles(baseDirectory, predicate=null) {
const results = [];
const directories = [path.resolve(baseDirectory)];
while (directories.length > 0) {
const directory = directories.shift();
for (const fileName of fs.readdirSync(directory)) {
const fullFileName = path.resolve(directory, fileName);
const stats = fs.statSync(fullFileName);
if (stats.isFile()) {
if (typeof predicate !== 'function' || predicate(fullFileName, directory, baseDirectory)) {
results.push(fullFileName);
}
} else if (stats.isDirectory()) {
directories.push(fullFileName);
}
}
}
return results;
}
module.exports = {
createTestDictionaryArchive,
getAllFiles,
get JSZip() { return getJSZip(); }
};