From c49ca3ba841ea7763148a76d9a84e1bf7f5698ad Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Wed, 23 Aug 2017 09:38:42 +0200 Subject: [PATCH] move some helpers to ts (#2399) --- .../ProfileRulesSonarWayComparison.tsx | 2 +- .../main/js/apps/quality-profiles/utils.ts | 17 ++-- .../__tests__/{path-test.js => path-test.ts} | 0 .../__tests__/{urls-test.js => urls-test.ts} | 8 +- .../js/helpers/{constants.js => constants.ts} | 0 .../js/helpers/{cookies.js => cookies.ts} | 5 +- .../src/main/js/helpers/{csv.js => csv.ts} | 3 +- .../js/helpers/{latinize.js => latinize.ts} | 8 +- .../src/main/js/helpers/{path.js => path.ts} | 18 ++--- .../js/helpers/{ratings.js => ratings.ts} | 34 ++++---- .../js/helpers/{scrolling.js => scrolling.ts} | 42 +++++----- .../js/helpers/{storage.js => storage.ts} | 44 ++++++----- .../js/helpers/{testUtils.js => testUtils.ts} | 31 +++++--- .../src/main/js/helpers/{urls.js => urls.ts} | 77 +++++++++---------- 14 files changed, 152 insertions(+), 137 deletions(-) rename server/sonar-web/src/main/js/helpers/__tests__/{path-test.js => path-test.ts} (100%) rename server/sonar-web/src/main/js/helpers/__tests__/{urls-test.js => urls-test.ts} (96%) rename server/sonar-web/src/main/js/helpers/{constants.js => constants.ts} (100%) rename server/sonar-web/src/main/js/helpers/{cookies.js => cookies.ts} (91%) rename server/sonar-web/src/main/js/helpers/{csv.js => csv.ts} (92%) rename server/sonar-web/src/main/js/helpers/{latinize.js => latinize.ts} (98%) rename server/sonar-web/src/main/js/helpers/{path.js => path.ts} (84%) rename server/sonar-web/src/main/js/helpers/{ratings.js => ratings.ts} (77%) rename server/sonar-web/src/main/js/helpers/{scrolling.js => scrolling.ts} (75%) rename server/sonar-web/src/main/js/helpers/{storage.js => storage.ts} (74%) rename server/sonar-web/src/main/js/helpers/{testUtils.js => testUtils.ts} (68%) rename server/sonar-web/src/main/js/helpers/{urls.js => urls.ts} (61%) diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileRulesSonarWayComparison.tsx b/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileRulesSonarWayComparison.tsx index cc7470d93ab..f79240d88a0 100644 --- a/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileRulesSonarWayComparison.tsx +++ b/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileRulesSonarWayComparison.tsx @@ -35,7 +35,7 @@ export default function ProfileRulesSonarWayComparison(props: Props) { const url = getRulesUrl( { qprofile: props.profile, - activation: false, + activation: 'false', compareToProfile: props.sonarway, languages: props.language }, diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/utils.ts b/server/sonar-web/src/main/js/apps/quality-profiles/utils.ts index b23906f430f..cc7140c0fd4 100644 --- a/server/sonar-web/src/main/js/apps/quality-profiles/utils.ts +++ b/server/sonar-web/src/main/js/apps/quality-profiles/utils.ts @@ -69,15 +69,22 @@ export function isStagnant(profile: Profile) { return moment().diff(moment(profile.userUpdatedAt), 'years') >= 1; } -export const getProfilesPath = (organization: string | null) => +export const getProfilesPath = (organization: string | null | undefined) => organization ? `/organizations/${organization}/quality_profiles` : '/profiles'; -export const getProfilesForLanguagePath = (language: string, organization: string | null) => ({ +export const getProfilesForLanguagePath = ( + language: string, + organization: string | null | undefined +) => ({ pathname: getProfilesPath(organization), query: { language } }); -export const getProfilePath = (name: string, language: string, organization: string | null) => ({ +export const getProfilePath = ( + name: string, + language: string, + organization: string | null | undefined +) => ({ pathname: getProfilesPath(organization) + '/show', query: { name, language } }); @@ -85,7 +92,7 @@ export const getProfilePath = (name: string, language: string, organization: str export const getProfileComparePath = ( name: string, language: string, - organization: string | null, + organization: string | null | undefined, withKey?: string ) => { const query = { language, name }; @@ -101,7 +108,7 @@ export const getProfileComparePath = ( export const getProfileChangelogPath = ( name: string, language: string, - organization: string | null, + organization: string | null | undefined, filter?: { since?: string; to?: string } ) => { const query = { language, name }; diff --git a/server/sonar-web/src/main/js/helpers/__tests__/path-test.js b/server/sonar-web/src/main/js/helpers/__tests__/path-test.ts similarity index 100% rename from server/sonar-web/src/main/js/helpers/__tests__/path-test.js rename to server/sonar-web/src/main/js/helpers/__tests__/path-test.ts diff --git a/server/sonar-web/src/main/js/helpers/__tests__/urls-test.js b/server/sonar-web/src/main/js/helpers/__tests__/urls-test.ts similarity index 96% rename from server/sonar-web/src/main/js/helpers/__tests__/urls-test.js rename to server/sonar-web/src/main/js/helpers/__tests__/urls-test.ts index 9321a56db18..90f0417fd6a 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/urls-test.js +++ b/server/sonar-web/src/main/js/helpers/__tests__/urls-test.ts @@ -30,14 +30,14 @@ const COMPLEX_COMPONENT_KEY = 'org.sonarsource.sonarqube:sonarqube'; const COMPLEX_COMPONENT_KEY_ENCODED = encodeURIComponent(COMPLEX_COMPONENT_KEY); const METRIC = 'coverage'; -let oldBaseUrl; +let oldBaseUrl: string; beforeEach(() => { - oldBaseUrl = window.baseUrl; + oldBaseUrl = (window as any).baseUrl; }); afterEach(() => { - window.baseUrl = oldBaseUrl; + (window as any).baseUrl = oldBaseUrl; }); describe('#getComponentUrl', () => { @@ -52,7 +52,7 @@ describe('#getComponentUrl', () => { }); it('should take baseUrl into account', () => { - window.baseUrl = '/context'; + (window as any).baseUrl = '/context'; expect(getComponentUrl(COMPLEX_COMPONENT_KEY)).toBe( '/context/dashboard?id=' + COMPLEX_COMPONENT_KEY_ENCODED ); diff --git a/server/sonar-web/src/main/js/helpers/constants.js b/server/sonar-web/src/main/js/helpers/constants.ts similarity index 100% rename from server/sonar-web/src/main/js/helpers/constants.js rename to server/sonar-web/src/main/js/helpers/constants.ts diff --git a/server/sonar-web/src/main/js/helpers/cookies.js b/server/sonar-web/src/main/js/helpers/cookies.ts similarity index 91% rename from server/sonar-web/src/main/js/helpers/cookies.js rename to server/sonar-web/src/main/js/helpers/cookies.ts index 8255c5924da..b3442b76703 100644 --- a/server/sonar-web/src/main/js/helpers/cookies.js +++ b/server/sonar-web/src/main/js/helpers/cookies.ts @@ -17,10 +17,9 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -// @flow -let cookies; +let cookies: { [key: string]: string }; -export function getCookie(name /*: string */) { +export function getCookie(name: string): string | undefined { if (cookies) { return cookies[name]; } diff --git a/server/sonar-web/src/main/js/helpers/csv.js b/server/sonar-web/src/main/js/helpers/csv.ts similarity index 92% rename from server/sonar-web/src/main/js/helpers/csv.js rename to server/sonar-web/src/main/js/helpers/csv.ts index cc49b2f3ec0..6838ee14b40 100644 --- a/server/sonar-web/src/main/js/helpers/csv.js +++ b/server/sonar-web/src/main/js/helpers/csv.ts @@ -17,8 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -// @flow -export function csvEscape(value /*: string */) /*: string */ { +export function csvEscape(value: string): string { const escaped = value.replace(/"/g, '\\"'); return `"${escaped}"`; } diff --git a/server/sonar-web/src/main/js/helpers/latinize.js b/server/sonar-web/src/main/js/helpers/latinize.ts similarity index 98% rename from server/sonar-web/src/main/js/helpers/latinize.js rename to server/sonar-web/src/main/js/helpers/latinize.ts index 18b5cfe8de1..1397450acfc 100644 --- a/server/sonar-web/src/main/js/helpers/latinize.js +++ b/server/sonar-web/src/main/js/helpers/latinize.ts @@ -21,8 +21,6 @@ * Latinize string by removing all diacritics * From http://stackoverflow.com/questions/990904/javascript-remove-accents-in-strings */ -/* eslint max-len: 0 */ -/* jscs:disable maximumLineLength */ const defaultDiacriticsRemovalap = [ { base: 'A', @@ -395,7 +393,7 @@ const defaultDiacriticsRemovalap = [ } ]; -const diacriticsMap = {}; +const diacriticsMap: { [x: string]: string } = {}; for (let i = 0; i < defaultDiacriticsRemovalap.length; i++) { const letters = defaultDiacriticsRemovalap[i].letters.split(''); for (let j = 0; j < letters.length; j++) { @@ -404,8 +402,6 @@ for (let i = 0; i < defaultDiacriticsRemovalap.length; i++) { } // "what?" version ... http://jsperf.com/diacritics/12 -function removeDiacritics(str) { +export default function removeDiacritics(str: string): string { return str.replace(/[^\u0000-\u007E]/g, a => diacriticsMap[a] || a); } - -export default removeDiacritics; diff --git a/server/sonar-web/src/main/js/helpers/path.js b/server/sonar-web/src/main/js/helpers/path.ts similarity index 84% rename from server/sonar-web/src/main/js/helpers/path.js rename to server/sonar-web/src/main/js/helpers/path.ts index f1400210753..2ea8d4f5963 100644 --- a/server/sonar-web/src/main/js/helpers/path.js +++ b/server/sonar-web/src/main/js/helpers/path.ts @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -export function collapsePath(path, limit = 30) { +export function collapsePath(path: string, limit = 30): string { if (typeof path !== 'string') { return ''; } @@ -38,7 +38,7 @@ export function collapsePath(path, limit = 30) { cut = true; } - const body = [].concat(head, cut ? ['...'] : [], middle, tail); + const body = [head, ...(cut ? ['...'] : []), ...middle, tail]; return body.join('/'); } @@ -47,10 +47,8 @@ export function collapsePath(path, limit = 30) { * @example * // returns 'src/.../js/components/navigator/app/models/' * collapsedDirFromPath('src/main/js/components/navigator/app/models/state.js') - * @param {string} path - * @returns {string|null} */ -export function collapsedDirFromPath(path) { +export function collapsedDirFromPath(path: string | null): string | null { const limit = 30; if (typeof path === 'string') { const tokens = path.split('/').slice(0, -1); @@ -64,7 +62,7 @@ export function collapsedDirFromPath(path) { middle.shift(); cut = true; } - const body = [].concat(head, cut ? ['...'] : [], middle, tail); + const body = [head, ...(cut ? ['...'] : []), ...middle, tail]; return body.join('/') + '/'; } else { return tokens.join('/') + '/'; @@ -79,10 +77,8 @@ export function collapsedDirFromPath(path) { * * @example * // returns 'state.js' * collapsedDirFromPath('src/main/js/components/navigator/app/models/state.js') - * @param {string} path - * @returns {string|null} */ -export function fileFromPath(path) { +export function fileFromPath(path: string | null): string | null { if (typeof path === 'string') { const tokens = path.split('/'); return tokens[tokens.length - 1]; @@ -91,7 +87,7 @@ export function fileFromPath(path) { } } -export function splitPath(path) { +export function splitPath(path: string): { head: string; tail: string } | null { if (typeof path === 'string') { const tokens = path.split('/'); return { @@ -103,7 +99,7 @@ export function splitPath(path) { } } -export function limitComponentName(str, limit = 30) { +export function limitComponentName(str: string, limit = 30): string { if (typeof str === 'string') { return str.length > limit ? str.substr(0, limit) + '...' : str; } else { diff --git a/server/sonar-web/src/main/js/helpers/ratings.js b/server/sonar-web/src/main/js/helpers/ratings.ts similarity index 77% rename from server/sonar-web/src/main/js/helpers/ratings.js rename to server/sonar-web/src/main/js/helpers/ratings.ts index 2bcce15b3df..c1880e3aa4b 100644 --- a/server/sonar-web/src/main/js/helpers/ratings.js +++ b/server/sonar-web/src/main/js/helpers/ratings.ts @@ -17,40 +17,44 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -const checkNumberRating = coverageRating => { +function checkNumberRating(coverageRating: number): void { if (!(typeof coverageRating === 'number' && coverageRating > 0 && coverageRating < 6)) { throw new Error(`Unknown number rating: "${coverageRating}"`); } -}; +} -export const getCoverageRatingLabel = rating => { +export function getCoverageRatingLabel(rating: number): string { checkNumberRating(rating); - const mapping = ['≥ 80%', '< 80%', '< 70%', '< 50%', '< 30%']; return mapping[rating - 1]; -}; -export const getCoverageRatingAverageValue = rating => { +} + +export function getCoverageRatingAverageValue(rating: number): number { checkNumberRating(rating); const mapping = [90, 75, 60, 40, 15]; return mapping[rating - 1]; -}; -export const getDuplicationsRatingLabel = rating => { +} + +export function getDuplicationsRatingLabel(rating: number): string { checkNumberRating(rating); const mapping = ['< 3%', '≥ 3%', '> 5%', '> 10%', '> 20%']; return mapping[rating - 1]; -}; -export const getDuplicationsRatingAverageValue = rating => { +} + +export function getDuplicationsRatingAverageValue(rating: number): number { checkNumberRating(rating); const mapping = [1.5, 4, 7.5, 15, 30]; return mapping[rating - 1]; -}; -export const getSizeRatingLabel = rating => { +} + +export function getSizeRatingLabel(rating: number): string { checkNumberRating(rating); const mapping = ['< 1k', '≥ 1k', '> 10k', '> 100k', '> 500k']; return mapping[rating - 1]; -}; -export const getSizeRatingAverageValue = rating => { +} + +export function getSizeRatingAverageValue(rating: number): number { checkNumberRating(rating); const mapping = [500, 5000, 50000, 250000, 750000]; return mapping[rating - 1]; -}; +} diff --git a/server/sonar-web/src/main/js/helpers/scrolling.js b/server/sonar-web/src/main/js/helpers/scrolling.ts similarity index 75% rename from server/sonar-web/src/main/js/helpers/scrolling.js rename to server/sonar-web/src/main/js/helpers/scrolling.ts index 7e1fd53c926..a36c6ef0789 100644 --- a/server/sonar-web/src/main/js/helpers/scrolling.js +++ b/server/sonar-web/src/main/js/helpers/scrolling.ts @@ -17,26 +17,29 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -// @flow import { debounce } from 'lodash'; const SCROLLING_DURATION = 100; const SCROLLING_INTERVAL = 10; const SCROLLING_STEPS = SCROLLING_DURATION / SCROLLING_INTERVAL; -function getScrollPosition(element /*: HTMLElement */) /*: number */ { - return element === window ? window.scrollY : element.scrollTop; +function isWindow(element: HTMLElement | Window): element is Window { + return element === window; } -function scrollElement(element /*: HTMLElement */, position /*: number */) { - if (element === window) { +function getScrollPosition(element: HTMLElement | Window): number { + return isWindow(element) ? window.scrollY : element.scrollTop; +} + +function scrollElement(element: HTMLElement | Window, position: number): void { + if (isWindow(element)) { window.scrollTo(0, position); } else { element.scrollTop = position; } } -let smoothScrollTop = (y /*: number */, parent) => { +let smoothScrollTop = (y: number, parent: HTMLElement | Window) => { let scrollTop = getScrollPosition(parent); const scrollingDown = y > scrollTop; const step = Math.ceil(Math.abs(y - scrollTop) / SCROLLING_STEPS); @@ -61,15 +64,15 @@ let smoothScrollTop = (y /*: number */, parent) => { smoothScrollTop = debounce(smoothScrollTop, SCROLLING_DURATION, { leading: true }); -export const scrollToElement = ( - element /*: HTMLElement */, - options /*: { - topOffset?: number, - bottomOffset?: number, - parent?: HTMLElement, - smooth?: boolean - } */ -) => { +export function scrollToElement( + element: HTMLElement, + options: { + topOffset?: number; + bottomOffset?: number; + parent?: HTMLElement; + smooth?: boolean; + } +): void { const opts = { topOffset: 0, bottomOffset: 0, parent: window, smooth: true, ...options }; const { parent } = opts; @@ -77,10 +80,11 @@ export const scrollToElement = ( const scrollTop = getScrollPosition(parent); - const height /*: number */ = - parent === window ? window.innerHeight : parent.getBoundingClientRect().height; + const height: number = isWindow(parent) + ? window.innerHeight + : parent.getBoundingClientRect().height; - const parentTop = parent === window ? 0 : parent.getBoundingClientRect().top; + const parentTop = isWindow(parent) ? 0 : parent.getBoundingClientRect().top; if (top - parentTop < opts.topOffset) { const goal = scrollTop - opts.topOffset + top - parentTop; @@ -99,4 +103,4 @@ export const scrollToElement = ( scrollElement(parent, goal); } } -}; +} diff --git a/server/sonar-web/src/main/js/helpers/storage.js b/server/sonar-web/src/main/js/helpers/storage.ts similarity index 74% rename from server/sonar-web/src/main/js/helpers/storage.js rename to server/sonar-web/src/main/js/helpers/storage.ts index 404daf59a67..783a7c5d580 100644 --- a/server/sonar-web/src/main/js/helpers/storage.js +++ b/server/sonar-web/src/main/js/helpers/storage.ts @@ -17,7 +17,6 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -// @flow const PROJECTS_DEFAULT_FILTER = 'sonarqube.projects.default'; const PROJECTS_FAVORITE = 'favorite'; const PROJECTS_ALL = 'all'; @@ -29,7 +28,7 @@ const PROJECTS_SORT = 'sonarqube.projects.sort'; const PROJECT_ACTIVITY_GRAPH = 'sonarqube.project_activity.graph'; const PROJECT_ACTIVITY_GRAPH_CUSTOM = 'sonarqube.project_activity.graph.custom'; -const save = (key /*: string */, value /*: ?string */) => { +function save(key: string, value?: string): void { try { if (value) { window.localStorage.setItem(key, value); @@ -40,56 +39,63 @@ const save = (key /*: string */, value /*: ?string */) => { // usually that means the storage is full // just do nothing in this case } -}; +} -export function saveFavorite() { +export function saveFavorite(): void { save(PROJECTS_DEFAULT_FILTER, PROJECTS_FAVORITE); } -export function isFavoriteSet() /*: boolean */ { + +export function isFavoriteSet(): boolean { const setting = window.localStorage.getItem(PROJECTS_DEFAULT_FILTER); return setting === PROJECTS_FAVORITE; } -export function saveAll() { +export function saveAll(): void { save(PROJECTS_DEFAULT_FILTER, PROJECTS_ALL); } -export function isAllSet() /*: boolean */ { + +export function isAllSet(): boolean { const setting = window.localStorage.getItem(PROJECTS_DEFAULT_FILTER); return setting === PROJECTS_ALL; } -export function saveView(view /*: ?string */) { +export function saveView(view?: string): void { save(PROJECTS_VIEW, view); } -export function getView() { + +export function getView(): string | null { return window.localStorage.getItem(PROJECTS_VIEW); } -export function saveVisualization(visualization /*: ?string */) { +export function saveVisualization(visualization?: string): void { save(PROJECTS_VISUALIZATION, visualization); } -export function getVisualization() { - window.localStorage.getItem(PROJECTS_VISUALIZATION); + +export function getVisualization(): string | null { + return window.localStorage.getItem(PROJECTS_VISUALIZATION); } -export function saveSort(sort /*: ?string */) { +export function saveSort(sort?: string): void { save(PROJECTS_SORT, sort); } -export function getSort() { - window.localStorage.getItem(PROJECTS_SORT); + +export function getSort(): string | null { + return window.localStorage.getItem(PROJECTS_SORT); } -export function saveCustomGraph(metrics /*: ?Array */) { +export function saveCustomGraph(metrics?: string[]): void { save(PROJECT_ACTIVITY_GRAPH_CUSTOM, metrics ? metrics.join(',') : ''); } -export function getCustomGraph() /*: Array */ { + +export function getCustomGraph(): string[] { const customGraphs = window.localStorage.getItem(PROJECT_ACTIVITY_GRAPH_CUSTOM); return customGraphs ? customGraphs.split(',') : []; } -export function saveGraph(graph /*: ?string */) { +export function saveGraph(graph?: string): void { save(PROJECT_ACTIVITY_GRAPH, graph); } -export function getGraph() /*: string */ { + +export function getGraph(): string { return window.localStorage.getItem(PROJECT_ACTIVITY_GRAPH) || 'issues'; } diff --git a/server/sonar-web/src/main/js/helpers/testUtils.js b/server/sonar-web/src/main/js/helpers/testUtils.ts similarity index 68% rename from server/sonar-web/src/main/js/helpers/testUtils.js rename to server/sonar-web/src/main/js/helpers/testUtils.ts index a69169ceeae..deed3501e74 100644 --- a/server/sonar-web/src/main/js/helpers/testUtils.js +++ b/server/sonar-web/src/main/js/helpers/testUtils.ts @@ -17,6 +17,8 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { ShallowWrapper } from 'enzyme'; + export const mockEvent = { target: { blur() {} }, currentTarget: { blur() {} }, @@ -24,41 +26,46 @@ export const mockEvent = { stopPropagation() {} }; -export const click = (element, event = {}) => element.simulate('click', { ...mockEvent, ...event }); +export function click(element: ShallowWrapper, event = {}): void { + element.simulate('click', { ...mockEvent, ...event }); +} -export const clickOutside = (event = {}) => { +export function clickOutside(event = {}): void { const dispatchedEvent = new MouseEvent('click', event); window.dispatchEvent(dispatchedEvent); -}; +} -export const submit = element => +export function submit(element: ShallowWrapper): void { element.simulate('submit', { preventDefault() {} }); +} -export const change = (element, value) => +export function change(element: ShallowWrapper, value: string): void { element.simulate('change', { target: { value }, currentTarget: { value } }); +} -export const keydown = keyCode => { - const event = new KeyboardEvent('keydown', { keyCode }); +export function keydown(keyCode: number): void { + const event = new KeyboardEvent('keydown', { keyCode } as KeyboardEventInit); document.dispatchEvent(event); -}; +} -export const elementKeydown = (element, keyCode) => { +export function elementKeydown(element: ShallowWrapper, keyCode: number): void { element.simulate('keydown', { currentTarget: { element }, keyCode, preventDefault() {} }); -}; +} -export const doAsync = fn => - new Promise(resolve => { +export function doAsync(fn: Function): Promise { + return new Promise(resolve => { setTimeout(() => { fn(); resolve(); }, 0); }); +} diff --git a/server/sonar-web/src/main/js/helpers/urls.js b/server/sonar-web/src/main/js/helpers/urls.ts similarity index 61% rename from server/sonar-web/src/main/js/helpers/urls.js rename to server/sonar-web/src/main/js/helpers/urls.ts index 52fe92dd365..3cf8cfbb6fd 100644 --- a/server/sonar-web/src/main/js/helpers/urls.js +++ b/server/sonar-web/src/main/js/helpers/urls.ts @@ -20,58 +20,56 @@ import { stringify } from 'querystring'; import { getProfilePath } from '../apps/quality-profiles/utils'; +interface Query { + [x: string]: string; +} + +interface Location { + pathname: string; + query?: Query; +} + /** * Generate URL for a component's home page - * @param {string} componentKey - * @returns {string} */ -export function getComponentUrl(componentKey) { - return window.baseUrl + '/dashboard?id=' + encodeURIComponent(componentKey); +export function getComponentUrl(componentKey: string): string { + return (window as any).baseUrl + '/dashboard?id=' + encodeURIComponent(componentKey); } -export function getProjectUrl(key) { - return { - pathname: '/dashboard', - query: { id: key } - }; +export function getProjectUrl(key: string): Location { + return { pathname: '/dashboard', query: { id: key } }; } /** * Generate URL for a global issues page */ -export function getIssuesUrl(query) { +export function getIssuesUrl(query: Query): Location { return { pathname: '/issues', query }; } /** * Generate URL for a component's issues page */ -export function getComponentIssuesUrl(componentKey, query) { - return { pathname: '/project/issues', query: { ...query, id: componentKey } }; +export function getComponentIssuesUrl(componentKey: string, query?: Query): Location { + return { pathname: '/project/issues', query: { ...query || {}, id: componentKey } }; } -export function getComponentIssuesUrlAsString(componentKey, query) { +export function getComponentIssuesUrlAsString(componentKey: string, query?: Query): string { const path = getComponentIssuesUrl(componentKey, query); - return `${window.baseUrl}${path.pathname}?${stringify(path.query)}`; + return `${(window as any).baseUrl}${path.pathname}?${stringify(path.query)}`; } /** * Generate URL for a component's drilldown page - * @param {string} componentKey - * @param {string} metric - * @returns {Object} */ -export function getComponentDrilldownUrl(componentKey, metric) { +export function getComponentDrilldownUrl(componentKey: string, metric: string): Location { return { pathname: '/component_measures', query: { id: componentKey, metric } }; } /** * Generate URL for a component's measure history - * @param {string} componentKey - * @param {string} metric - * @returns {Object} */ -export function getComponentMeasureHistory(componentKey, metric) { +export function getComponentMeasureHistory(componentKey: string, metric: string): Location { return { pathname: '/project/activity', query: { id: componentKey, graph: 'custom', custom_metrics: metric } @@ -80,30 +78,29 @@ export function getComponentMeasureHistory(componentKey, metric) { /** * Generate URL for a component's permissions page - * @param {string} componentKey - * @returns {Object} */ -export function getComponentPermissionsUrl(componentKey) { - return { - pathname: '/project_roles', - query: { id: componentKey } - }; +export function getComponentPermissionsUrl(componentKey: string): Location { + return { pathname: '/project_roles', query: { id: componentKey } }; } /** * Generate URL for a quality profile */ -export function getQualityProfileUrl(name, language, organization) { +export function getQualityProfileUrl( + name: string, + language: string, + organization?: string | null +): Location { return getProfilePath(name, language, organization); } -export function getQualityGateUrl(key /*: string */, organization /*: ?string */) { +export function getQualityGateUrl(key: string, organization?: string | null): Location { return { pathname: getQualityGatesUrl(organization).pathname + '/show/' + encodeURIComponent(key) }; } -export function getQualityGatesUrl(organization /*: ?string */) { +export function getQualityGatesUrl(organization?: string | null): Location { return { pathname: (organization ? '/organizations/' + encodeURIComponent(organization) : '') + '/quality_gates' @@ -112,10 +109,8 @@ export function getQualityGatesUrl(organization /*: ?string */) { /** * Generate URL for the rules page - * @param {object} query - * @returns {string} */ -export function getRulesUrl(query, organization /*: ?string */) { +export function getRulesUrl(query: { [x: string]: string }, organization?: string | null): string { const path = organization ? `/organizations/${organization}/rules` : '/coding_rules'; if (query) { @@ -132,14 +127,16 @@ export function getRulesUrl(query, organization /*: ?string */) { /** * Generate URL for the rules page filtering only active deprecated rules - * @param {object} query - * @returns {string} */ -export function getDeprecatedActiveRulesUrl(query = {}, organization /*: ?string */) { +export function getDeprecatedActiveRulesUrl(query = {}, organization?: string | null): string { const baseQuery = { activation: 'true', statuses: 'DEPRECATED' }; return getRulesUrl({ ...query, ...baseQuery }, organization); } -export const getProjectsUrl = () => window.baseUrl + '/projects'; +export function getProjectsUrl(): string { + return (window as any).baseUrl + '/projects'; +} -export const getMarkdownHelpUrl = () => window.baseUrl + '/markdown/help'; +export function getMarkdownHelpUrl(): string { + return (window as any).baseUrl + '/markdown/help'; +} -- 2.39.5