Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 17 additions & 29 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module.exports = {
'packages/kbn-test/**/*',
'packages/kbn-eslint-import-resolver-kibana/**/*',
'x-pack/plugins/apm/**/*',
'x-pack/plugins/canvas/**/*',
],
plugins: ['prettier'],
rules: Object.assign(
Expand Down Expand Up @@ -366,43 +367,30 @@ module.exports = {
*/
{
files: ['x-pack/plugins/canvas/**/*'],
plugins: ['prettier'],
rules: {
// preferences
'comma-dangle': [2, 'always-multiline'],
'no-multiple-empty-lines': [2, { max: 1, maxEOF: 1 }],
'no-multi-spaces': 2,
radix: 2,
curly: [2, 'multi-or-nest', 'consistent'],

// annoying rules that conflict with prettier
'space-before-function-paren': 0,
indent: 0,
'wrap-iife': 0,
'max-len': 0,
radix: 'error',
curly: ['error', 'all'],

// module importing
'import/order': [
2,
{ groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'] },
'error',
{
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'],
},
],
'import/extensions': [2, 'never', { json: 'always', less: 'always', svg: 'always' }],

// prettier
'prettier/prettier': 2,
'import/extensions': ['error', 'never', { json: 'always', less: 'always', svg: 'always' }],

// react
'jsx-quotes': 2,
'react/no-did-mount-set-state': 2,
'react/no-did-update-set-state': 2,
'react/no-multi-comp': [2, { ignoreStateless: true }],
'react/self-closing-comp': 2,
'react/sort-comp': 2,
'react/jsx-boolean-value': 2,
'react/jsx-wrap-multilines': 2,
'react/no-unescaped-entities': [2, { forbid: ['>', '}'] }],
'react/no-did-mount-set-state': 'error',
'react/no-did-update-set-state': 'error',
'react/no-multi-comp': ['error', { ignoreStateless: true }],
'react/self-closing-comp': 'error',
'react/sort-comp': 'error',
'react/jsx-boolean-value': 'error',
'react/jsx-wrap-multilines': 'error',
'react/no-unescaped-entities': ['error', { forbid: ['>', '}'] }],
'react/forbid-elements': [
2,
'error',
{
forbid: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ describe('sort', () => {
const fn = functionWrapper(sort);

const isSorted = (rows, column, reverse) => {
if (reverse) return !rows.some((row, i) => rows[i + 1] && row[column] < rows[i + 1][column]);
if (reverse) {
return !rows.some((row, i) => rows[i + 1] && row[column] < rows[i + 1][column]);
}
return !rows.some((row, i) => rows[i + 1] && row[column] > rows[i + 1][column]);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@ export const alterColumn = () => ({
},
},
fn: (context, args) => {
if (!args.column || (!args.type && !args.name)) return context;
if (!args.column || (!args.type && !args.name)) {
return context;
}

const column = context.columns.find(col => col.name === args.column);
if (!column) throw new Error(`Column not found: '${args.column}'`);
if (!column) {
throw new Error(`Column not found: '${args.column}'`);
}

const name = args.name || column.name;
const type = args.type || column.type;

const columns = context.columns.reduce((all, col) => {
if (col.name !== args.name) {
if (col.name !== column.name) all.push(col);
else all.push({ name, type });
if (col.name !== column.name) {
all.push(col);
} else {
all.push({ name, type });
}
}
return all;
}, []);
Expand All @@ -54,7 +61,9 @@ export const alterColumn = () => ({
handler = (function getHandler() {
switch (type) {
case 'string':
if (column.type === 'date') return v => new Date(v).toISOString();
if (column.type === 'date') {
return v => new Date(v).toISOString();
}
return String;
case 'number':
return Number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export const axisConfig = () => ({
},
fn: (context, args) => {
const positions = ['top', 'bottom', 'left', 'right', ''];
if (!positions.includes(args.position)) throw new Error(`Invalid position ${args.position}`);
if (!positions.includes(args.position)) {
throw new Error(`Invalid position ${args.position}`);
}

const min = typeof args.min === 'string' ? moment.utc(args.min).valueOf() : args.min;
const max = typeof args.max === 'string' ? moment.utc(args.max).valueOf() : args.max;
Expand Down
12 changes: 9 additions & 3 deletions x-pack/plugins/canvas/canvas_plugin_src/functions/common/case.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ export const caseFn = () => ({
});

async function doesMatch(context, args) {
if (typeof args.if !== 'undefined') return args.if;
if (typeof args.when !== 'undefined') return (await args.when()) === context;
if (typeof args.if !== 'undefined') {
return args.if;
}
if (typeof args.when !== 'undefined') {
return (await args.when()) === context;
}
return true;
}

async function getResult(context, args) {
if (typeof args.then !== 'undefined') return await args.then();
if (typeof args.then !== 'undefined') {
return await args.then();
}
return context;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export const columns = () => ({
const columns = [];
fields.forEach(field => {
const column = find(result.columns, { name: field });
if (column) columns.push(column);
if (column) {
columns.push(column);
}
});
const rows = columns.length > 0 ? result.rows.map(row => pick(row, fields)) : [];
result = { ...result, rows, columns };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,24 @@ export const compare = () => ({
case 'ne':
return a !== b;
case 'lt':
if (typesMatch) return a < b;
if (typesMatch) {
return a < b;
}
return false;
case 'lte':
if (typesMatch) return a <= b;
if (typesMatch) {
return a <= b;
}
return false;
case 'gt':
if (typesMatch) return a > b;
if (typesMatch) {
return a > b;
}
return false;
case 'gte':
if (typesMatch) return a >= b;
if (typesMatch) {
return a >= b;
}
return false;
default:
throw new Error('Invalid compare operator. Use eq, ne, lt, gt, lte, or gte.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ export const containerStyle = () => ({
};

if (backgroundImage) {
if (!isValidUrl(backgroundImage))
if (!isValidUrl(backgroundImage)) {
throw new Error('Invalid backgroundImage. Please provide an asset or a URL.');
}
style.backgroundImage = `url(${backgroundImage})`;
style.backgroundSize = backgroundSize;
style.backgroundRepeat = backgroundRepeat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ export const csv = () => ({
},
};

if (delimiter != null) config.delimiter = delimiter;
if (newline != null) config.newline = newline;
if (delimiter != null) {
config.delimiter = delimiter;
}
if (newline != null) {
config.newline = newline;
}

// TODO: handle errors, check output.errors
const output = Papa.parse(csvString, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import moment from 'moment';

const getInputDate = input => {
// return current date if no input
if (!input) return new Date();
if (!input) {
return new Date();
}

// return the input
return input;
Expand Down Expand Up @@ -40,7 +42,9 @@ export const date = () => ({
const useMoment = date && format;
const outputDate = useMoment ? moment.utc(date, format).toDate() : new Date(getInputDate(date));

if (isNaN(outputDate.getTime())) throw new Error(`Invalid date input: ${date}`);
if (isNaN(outputDate.getTime())) {
throw new Error(`Invalid date input: ${date}`);
}

return outputDate.valueOf();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export const dropdownControl = () => ({
},
fn: (context, { valueColumn, filterColumn }) => {
let choices = [];
if (context.rows[0][valueColumn])
if (context.rows[0][valueColumn]) {
choices = uniq(context.rows.map(row => row[valueColumn])).sort();
}

const column = filterColumn || valueColumn;

Expand Down
12 changes: 9 additions & 3 deletions x-pack/plugins/canvas/canvas_plugin_src/functions/common/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ export const font = () => ({
},
},
fn: (context, args) => {
if (!weights.includes(args.weight)) throw new Error(`Invalid font weight: ${args.weight}`);
if (!alignments.includes(args.align)) throw new Error(`Invalid text alignment: ${args.align}`);
if (!weights.includes(args.weight)) {
throw new Error(`Invalid font weight: ${args.weight}`);
}
if (!alignments.includes(args.align)) {
throw new Error(`Invalid text alignment: ${args.align}`);
}

// the line height shouldn't ever be lower than the size
const lineHeight = args.lHeight ? `${args.lHeight}px` : 1;
Expand All @@ -96,7 +100,9 @@ export const font = () => ({
};

// conditionally apply styles based on input
if (args.color) spec.color = args.color;
if (args.color) {
spec.color = args.color;
}

return {
type: 'style',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export const formatdate = () => ({
},
},
fn: (context, args) => {
if (!args.format) return moment.utc(new Date(context)).toISOString();
if (!args.format) {
return moment.utc(new Date(context)).toISOString();
}
return moment.utc(new Date(context)).format(args.format);
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export const formatnumber = () => ({
},
},
fn: (context, args) => {
if (!args.format) return String(context);
if (!args.format) {
return String(context);
}
return numeral(context).format(args.format);
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ export const getCell = () => ({
},
fn: (context, args) => {
const row = context.rows[args.row];
if (!row) throw new Error(`Row not found: ${args.row}`);
if (!row) {
throw new Error(`Row not found: ${args.row}`);
}

const { column = context.columns[0].name } = args;
const value = row[column];

if (typeof value === 'undefined') throw new Error(`Column not found: ${column}`);
if (typeof value === 'undefined') {
throw new Error(`Column not found: ${column}`);
}

return value;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export const gt = () => ({
},
},
fn: (context, args) => {
if (typeof context !== typeof args.value) return false;
if (typeof context !== typeof args.value) {
return false;
}
return context > args.value;
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export const gte = () => ({
},
},
fn: (context, args) => {
if (typeof context !== typeof args.value) return false;
if (typeof context !== typeof args.value) {
return false;
}
return context >= args.value;
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ export const ifFn = () => ({
},
fn: async (context, args) => {
if (args.condition) {
if (typeof args.then === 'undefined') return context;
if (typeof args.then === 'undefined') {
return context;
}
return await args.then();
} else {
if (typeof args.else === 'undefined') return context;
if (typeof args.else === 'undefined') {
return context;
}
return await args.else();
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export const image = () => ({
},
},
fn: (context, { dataurl, mode }) => {
if (!modes.includes(mode)) throw '"mode" must be "contain", "cover", or "stretch"';
if (!modes.includes(mode)) {
throw '"mode" must be "contain", "cover", or "stretch"';
}

const modeStyle = mode === 'stretch' ? '100% 100%' : mode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export const lt = () => ({
},
},
fn: (context, args) => {
if (typeof context !== typeof args.value) return false;
if (typeof context !== typeof args.value) {
return false;
}
return context < args.value;
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export const lte = () => ({
},
},
fn: (context, args) => {
if (typeof context !== typeof args.value) return false;
if (typeof context !== typeof args.value) {
return false;
}
return context <= args.value;
},
});
Loading