Skip to content

Commit

Permalink
Improvements to Flow coverage
Browse files Browse the repository at this point in the history
* Better flow coverage

* Doesn't look like the stubs actually belong to jQuery 3 so I'm reverting these changes

* Remove unnecessary var

* This name makes a lot more sense

* Remove unnecessary type annotations (clutter code & are obvious e.g. string literals)

* More unnecessary type annotations

* Remove more unnecessary annotations

* Prefer literal union type

* Add a type and remove one

* More fixes

* Fix persist choices

* Remove more redundant annotations

* Remove more unnecessary annotations

* Switch to Array<...> shortform

* Undo one of my 'fixes'

* Final flow fix

* Convert Object to {} shorthand

* Remove a couple more unnecessary annotations

* Some final cleanup

* Implement requested changes

* Add/Remove/Fix types

* Adapt config types

* Un-Flow strict.js

* Allow subtitle to be null

* Final tweaks
  • Loading branch information
sushain97 committed Dec 20, 2017
1 parent 10ae3d3 commit 950f9a1
Show file tree
Hide file tree
Showing 18 changed files with 3,664 additions and 293 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

# Install development dependencies
- run: sudo apt-get install python3 python3-pip
- run: sudo npm install -g jsonlint eslint flow flow-bin htmlhint sass-lint
- run: sudo npm install -g jsonlint eslint flow flow-bin flow-coverage-report htmlhint sass-lint
- run: sudo pip3 install flake8

# Run tests
Expand All @@ -17,6 +17,7 @@ jobs:
- run: htmlhint --config .htmlhintrc.json *.html
- run: sass-lint --config assets/css/.sass-lint.yml --verbose --no-exit --max-warnings 0
- run: flake8 --config tools/.flake8.ini *.py **/**.py **/**/*.py
- run: flow-coverage-report --config flow-coverage.json
- run: flow check

# Build project
Expand Down
1 change: 0 additions & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
[include]

[libs]
interfaces/

[options]
2 changes: 1 addition & 1 deletion assets/js/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
]
}
],
"complexity": ["warn", 32],
"complexity": ["warn", 40],
"no-use-before-define": ["warn", "nofunc"],
"no-magic-numbers": [
"warn",
Expand Down
60 changes: 33 additions & 27 deletions assets/js/analyzer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// @flow

var analyzers = {}, analyzerData = {};
var currentAnalyzerRequest;

/* exported getAnalyzers */
/* exported analyzerData, analyzers, getAnalyzers, populateAnalyzerList, populateSecondaryAnalyzerList */

/* global config, modeEnabled, persistChoices, restoreChoices, localizeInterface, readCache, ajaxSend, ajaxComplete,
cache, getLangByCode, filterLangList, allowedLang, sendEvent, callApy, apyRequestTimeout */
cache, getLangByCode, filterLangPairList, allowedLang, sendEvent, callApy, apyRequestTimeout */
/* global ENTER_KEY_CODE */

if(modeEnabled('analyzation')) {
Expand All @@ -23,7 +26,7 @@ if(modeEnabled('analyzation')) {
persistChoices('analyzer');
});

$('#morphAnalyzerInput').keydown(function (e) {
$('#morphAnalyzerInput').keydown(function (e /*: JQueryKeyEventObject */) {
if(e.keyCode === ENTER_KEY_CODE && !e.shiftKey) {
e.preventDefault();
analyze();
Expand All @@ -40,7 +43,7 @@ if(modeEnabled('analyzation')) {
});
}

function getAnalyzers() {
function getAnalyzers() /*: JQueryPromise<any> */ {
var deferred = $.Deferred();

if(config.ANALYZERS) {
Expand All @@ -56,7 +59,7 @@ function getAnalyzers() {
deferred.resolve();
}
else {
console.warn('Analyzers cache ' + (analyzers === null ? 'stale' : 'miss') + ', retrieving from server');
console.warn('Analyzers cache ' + (analyzers ? 'miss' : 'stale') + ', retrieving from server');
$.jsonp({
url: config.APY_URL + '/list?q=analyzers',
beforeSend: ajaxSend,
Expand All @@ -79,10 +82,10 @@ function getAnalyzers() {
return deferred.promise();
}

function populateAnalyzerList(data) {
function populateAnalyzerList(data /*: {} */) {
$('.analyzerMode').empty();

analyzers = {};
analyzers = ({} /*: {[string]: string[] } */);
for(var lang in data) {
var analyzerLang = lang.indexOf('-') !== -1 ? lang.split('-')[0] : lang;
var group = analyzers[analyzerLang];
Expand All @@ -94,11 +97,11 @@ function populateAnalyzerList(data) {
}
}

var analyzersArray = [];
$.each(analyzers, function (analyzerLang, lang) {
var analyzersArray /*: [string, string][] */ = [];
$.each(analyzers, function (analyzerLang /*: string */, lang /*: string */) {
analyzersArray.push([analyzerLang, lang]);
});
analyzersArray = filterLangList(analyzersArray, function (analyzer) {
analyzersArray = filterLangPairList(analyzersArray, function (analyzer) {
return allowedLang(analyzer[0]);
});
analyzersArray.sort(function (a, b) {
Expand All @@ -114,7 +117,7 @@ function populateAnalyzerList(data) {
}

function populateSecondaryAnalyzerList() {
var group = analyzers[$('#primaryAnalyzerMode').val()];
var group /*: string[] */ = analyzers[$('#primaryAnalyzerMode').val()];
$('#secondaryAnalyzerMode').empty();

if(group) {
Expand Down Expand Up @@ -143,13 +146,13 @@ function populateSecondaryAnalyzerList() {
}

function analyze() {
var input = $('#morphAnalyzerInput').val();
var input /*: string */ = $('#morphAnalyzerInput').val();

if($('#primaryAnalyzerMode').val() === null || input.trim() === '') {
if(!$('#primaryAnalyzerMode').val() || input.trim() === '') {
return;
}

var analyzerMode = analyzers[$('#primaryAnalyzerMode').val()].length > 1
var analyzerMode /*: string */ = analyzers[$('#primaryAnalyzerMode').val()].length > 1
? $('#secondaryAnalyzerMode').val()
: analyzers[$('#primaryAnalyzerMode').val()][0];
sendEvent('analyzer', 'analyze', analyzerMode, $('#morphAnalyzerInput').val().length);
Expand All @@ -170,12 +173,12 @@ function analyze() {
error: handleAnalyzeErrorResponse,
complete: function () {
ajaxComplete();
currentAnalyzerRequest = undefined;
currentAnalyzerRequest = null;
}
}, '/analyze');
}

function handleAnalyzeSuccessResponse(data) {
function handleAnalyzeSuccessResponse(data /*: string[][] */) {
var regex = /([^<]*)((<[^>]+>)*)/g;
$('#morphAnalyzerOutput').empty();
for(var i = 0; i < data.length; i++) {
Expand All @@ -197,8 +200,9 @@ function handleAnalyzeSuccessResponse(data) {
var joinedMorphemes = {};
for(var j = 1; j < splitUnit.length; j++) {
var unit = splitUnit[j];
if(unit.match(regex).length > 2) {
var matches = unit.match(regex);
var matches = unit.match(regex);

if(matches && matches.length > 2) {
for(var k = 1; k < matches.length - 1; k++) {
if(joinedMorphemes[matches[k]]) {
joinedMorphemes[matches[k]].push(unit);
Expand All @@ -225,10 +229,10 @@ function handleAnalyzeSuccessResponse(data) {
}
}

function formatUnit(unit) {
var tagRegex = /<([^>]+)>/g, arrow = '&nbsp;&nbsp;&#8612;&nbsp;&nbsp;', tags = [];
var tagMatch = tagRegex.exec(unit);
while(tagMatch !== null) {
function formatUnit(unit /*: string */) {
var tagRegex = /<([^>]+)>/g, arrow = '&nbsp;&nbsp;&#8612;&nbsp;&nbsp;', tags /*: string[] */ = [];
var tagMatch /*: ?string[] */ = tagRegex.exec(unit);
while(tagMatch) {
tags.push(tagMatch[1]);
tagMatch = tagRegex.exec(unit);
}
Expand All @@ -237,12 +241,14 @@ function formatUnit(unit) {
(tags.length > 0 ? arrow + tags.join(' &#8901; ') : '');
}

function handleAnalyzeErrorResponse(xOptions, error) {
function handleAnalyzeErrorResponse(xOptions, error /*: string */) {
$('#morphAnalyzerOutput').text(error).removeClass('blurred');
}

/*:: import {config} from "./config.js" */
/*:: import {modeEnabled, ajaxSend, ajaxComplete, filterLangList, allowedLang, sendEvent, callApy, apyRequestTimeout} from "./util.js" */
/*:: export {analyzerData, analyzers, getAnalyzers, populateAnalyzerList, populateSecondaryAnalyzerList} */

/*:: import {ajaxComplete, ajaxSend, allowedLang, apyRequestTimeout, callApy, filterLangPairList,
modeEnabled, sendEvent} from "./util.js" */
/*:: import {ENTER_KEY_CODE} from "./util.js" */
/*:: import {localizeInterface, getLangByCode} from "./localization.js" */
/*:: import {persistChoices, restoreChoices, readCache, cache} from "./persistence.js" */
/*:: import {getLangByCode, localizeInterface} from "./localization.js" */
/*:: import {cache, persistChoices, readCache, restoreChoices} from "./persistence.js" */
2 changes: 1 addition & 1 deletion assets/js/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ if (!Object.keys) {

// From: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf
if (!Array.prototype.lastIndexOf) {
Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/) {
Array.prototype.lastIndexOf = function(searchElement /*, fromIndex */) {
'use strict';

if (this === void 0 || this === null) {
Expand Down
40 changes: 26 additions & 14 deletions assets/js/generator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
var generators = {}, generatorData = {};
// @flow

var generators = ({} /*: {[string]: string[]} */), generatorData = {};
var currentGeneratorRequest;

/* exported getGenerators */
/* global config, modeEnabled, persistChoices, readCache, ajaxSend, ajaxComplete, filterLangList, allowedLang, analyzers, cache,
/* exported generatorData, generators, getGenerators, populateGeneratorList, populateSecondaryGeneratorList */

/* global config, modeEnabled, persistChoices, readCache, ajaxSend, ajaxComplete, filterLangPairList, allowedLang, analyzers, cache,
localizeInterface, getLangByCode, sendEvent, restoreChoices, callApy, apyRequestTimeout */
/* global ENTER_KEY_CODE */

Expand All @@ -23,7 +26,7 @@ if(modeEnabled('generation')) {
persistChoices('generator');
});

$('#morphGeneratorInput').keydown(function (e) {
$('#morphGeneratorInput').keydown(function (e /*: JQueryKeyEventObject */) {
if(e.keyCode === ENTER_KEY_CODE && !e.shiftKey) {
e.preventDefault();
generate();
Expand All @@ -40,7 +43,7 @@ if(modeEnabled('generation')) {
});
}

function getGenerators() {
function getGenerators() /*: JQueryPromise<any> */ {
var deferred = $.Deferred();

if(config.GENERATORS) {
Expand All @@ -56,7 +59,7 @@ function getGenerators() {
deferred.resolve();
}
else {
console.warn('Generators cache ' + (analyzers === null ? 'stale' : 'miss') + ', retrieving from server');
console.warn('Generators cache ' + (analyzers ? 'miss' : 'stale') + ', retrieving from server');
$.jsonp({
url: config.APY_URL + '/list?q=generators',
beforeSend: ajaxSend,
Expand All @@ -79,7 +82,7 @@ function getGenerators() {
return deferred.promise();
}

function populateGeneratorList(data) {
function populateGeneratorList(data /*: {} */) {
$('.generatorMode').empty();

generators = {};
Expand All @@ -94,11 +97,11 @@ function populateGeneratorList(data) {
}
}

var generatorArray = [];
$.each(generators, function (generatorLang, lang) {
var generatorArray /*: [string, string][] */ = [];
$.each(generators, function (generatorLang /*: string */, lang /*: string */) {
generatorArray.push([generatorLang, lang]);
});
generatorArray = filterLangList(generatorArray, function (generator) {
generatorArray = filterLangPairList(generatorArray, function (generator /*: [string, string] */) {
return allowedLang(generator[0]);
});
generatorArray.sort(function (a, b) {
Expand Down Expand Up @@ -143,13 +146,13 @@ function populateSecondaryGeneratorList() {
}

function generate() {
var input = $('#morphGeneratorInput').val();
var input /*: string */ = $('#morphGeneratorInput').val();

if($('#primaryGeneratorMode').val() === null || input.trim() === '') {
if(!$('#primaryGeneratorMode').val() || input.trim() === '') {
return;
}

var generatorMode = generators[$('#primaryGeneratorMode').val()].length > 1
var generatorMode /*: string */ = generators[$('#primaryGeneratorMode').val()].length > 1
? $('#secondaryGeneratorMode').val()
: generators[$('#primaryGeneratorMode').val()][0];
sendEvent('generator', 'generate', generatorMode, $('#morphGeneratorInput').val().length);
Expand All @@ -170,7 +173,7 @@ function generate() {
error: handleGenerateErrorResponse,
complete: function () {
ajaxComplete();
currentGeneratorRequest = undefined;
currentGeneratorRequest = null;
}
}, '/generate');
}
Expand All @@ -191,3 +194,12 @@ function handleGenerateErrorResponse(xOptions, error) {
$('#morphGenOutput').text(error);
$('#morphGenOutput').removeClass('blurred');
}

/*:: export {generatorData, generators, getGenerators, populateGeneratorList, populateSecondaryGeneratorList} */

/*:: import {ajaxComplete, ajaxSend, allowedLang, apyRequestTimeout, callApy, ENTER_KEY_CODE, filterLangPairList, modeEnabled,
sendEvent} from "./util.js" */
/*:: import {cache, persistChoices, readCache, restoreChoices} from "./persistence.js" */
/*:: import {getLangByCode, localizeInterface} from "./localization.js" */
/*:: import {getPairs} from "./translator.js" */
/*:: import {analyzers} from "./analyzer.js" */
29 changes: 19 additions & 10 deletions assets/js/init.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// @flow

/* exported _paq */

/* global config, persistChoices, iso639Codes, iso639CodesInverse, populateTranslationList, showTranslateWebpageInterface */
/* global ajaxSend, ajaxComplete, debounce, resizeFooter, synchronizeTextareaHeights */

Expand All @@ -17,33 +21,35 @@ $(document).ready(function () {
$('body > .container').css('margin-top', '0px');

if(config.SUBTITLE) {
var subtitle = config.SUBTITLE,
subtitleColor = config.SUBTITLE_COLOR;
$('.apertiumSubLogo')
.text(config.SUBTITLE)
.text(subtitle)
.show();
if(config.SUBTITLE_COLOR) {
$('.apertiumSubLogo').css('color', config.SUBTITLE_COLOR);
if(subtitleColor) {
$('.apertiumSubLogo').css('color', subtitleColor);
}
}
else {
$('.apertiumSubLogo').hide();
}

if(config.SHOW_NAVBAR) {
if(config.ENABLED_MODES === null) {
$('.nav a').removeClass('hide');
}
else {
if(config.ENABLED_MODES) {
$.each(config.ENABLED_MODES, function () {
$('.nav a[data-mode=' + this + ']').removeClass('hide');
});
}
else {
$('.nav a').removeClass('hide');
}
}
else {
$('.navbar-default .navbar-toggle').hide();
$('.navbar-default .nav').hide();
}

var hash = parent.location.hash;
var hash /*: string */ = parent.location.hash;

try {
if(!hash || !$(hash + 'Container')) {
Expand Down Expand Up @@ -91,7 +97,10 @@ $(document).ready(function () {

resizeFooter();
$(window)
.on('hashchange', persistChoices)
.on('hashchange', function () {
var mode /*: string */ = parent.location.hash.substring(1);
persistChoices(mode);
})
.resize(debounce(function () {
populateTranslationList();
resizeFooter();
Expand Down Expand Up @@ -174,7 +183,7 @@ if(config.PIWIK_SITEID && config.PIWIK_URL) {
}

/*:: export {_paq} */
/*:: import {config} from "./config.js" */

/*:: import {persistChoices} from "./persistence.js" */
/*:: import {iso639Codes, iso639CodesInverse} from "./localization.js" */
/*:: import {populateTranslationList, showTranslateWebpageInterface} from "./translator.js" */
Expand Down
Loading

0 comments on commit 950f9a1

Please sign in to comment.