Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chores/lint #1657

Merged
merged 7 commits into from
Nov 16, 2022
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
8 changes: 8 additions & 0 deletions frontend/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.idea
.husky
node_modules
reports
public
tests_output
web
e2e
87 changes: 52 additions & 35 deletions frontend/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,22 @@ module.exports = {
'react',
],
'rules': {
'indent': [
"error", 4,
{
SwitchCase: 1,
ignoredNodes: ['JSXElement', 'JSXElement > *', 'JSXAttribute', 'JSXIdentifier', 'JSXNamespacedName', 'JSXMemberExpression', 'JSXSpreadAttribute', 'JSXExpressionContainer', 'JSXOpeningElement', 'JSXClosingElement', 'JSXText', 'JSXEmptyExpression', 'JSXSpreadChild'],
},
],
'react/jsx-max-props-per-line': [1,
{
'maximum': 3
}
],
'array-callback-return': 0,
'camelcase': 0,
'consistent-return': 0,
'eol-last': 0,
'no-use-before-define': 0,
'global-require': 0,
'jsx-a11y/click-events-have-key-events': 0, // there are valid cases for this e.g. forms
'jsx-a11y/interactive-supports-focus': 0, // there are valid cases for this e.g. forms
Expand All @@ -28,36 +41,30 @@ module.exports = {
'jsx-a11y/mouse-events-have-key-events': 0,
'jsx-a11y/no-noninteractive-element-interactions': 0, // rule seems buggy, doesn't understand some htmlFor cases
'max-len': 0,
'react/jsx-one-expression-per-line': 0,
'no-multi-assign': 0,
'no-nested-ternary': 0,
'no-empty': 0,
'no-new': 0,
'no-param-reassign': 0, //Disabled to it not looking for global components
'no-plusplus': 0,
'no-restricted-globals': 'off',
'no-return-assign': 0,
'no-prototype-builtins': 0,
'no-shadow': 0,
'no-underscore-dangle': 0,
'no-unused-expressions': 0,
'no-use-before-define': 0,
'object-curly-newline': 0,
'prefer-destructuring': 0,
'quote-props': 0,
'radix': 0,
'indent': [
"error", 4,
{
SwitchCase: 1,
ignoredNodes: ['JSXElement', 'JSXElement > *', 'JSXAttribute', 'JSXIdentifier', 'JSXNamespacedName', 'JSXMemberExpression', 'JSXSpreadAttribute', 'JSXExpressionContainer', 'JSXOpeningElement', 'JSXClosingElement', 'JSXText', 'JSXEmptyExpression', 'JSXSpreadChild'],
},
],
'react/jsx-indent': ["error", 4],
'react/jsx-indent-props': ["error", 2],
'react/jsx-max-props-per-line': [1,
{
'maximum': 3
}
],
'react/destructuring-assignment': 0,
'react/forbid-prop-types': 0,
'react/jsx-filename-extension': 0,
'react/jsx-indent': ["error", 4],
'react/jsx-indent-props': ["error", 2],
'react/jsx-no-undef': 0,
'react/jsx-one-expression-per-line': 0,
'react/jsx-tag-spacing': 0, //Disabled to it not looking for global components
'react/no-access-state-in-setstate': 0,
'react/no-array-index-key': 0, // there are valid cases for this where a key can not be determined
Expand All @@ -70,46 +77,56 @@ module.exports = {
},
'globals': {
'$': true,
'_': true,
'__DEV__': true,
'$crisp': true,
'API': true,
'AccountStore': true,
'em': true,
'Actions': true,
'Any': true,
'API': true,
'AppActions': true,
'AsyncStorage': true,
'closeModal': true,
'Constants': true,
'Cookies': true,
'describe': true,
'Dispatcher': true,
'ES6Component': true,
'E2E': true,
'Format': true,
'ES6Component': true,
'FormGroup': true,
'ga': true,
'hot': true,
'isMobile': true,
'Format': true,
'Link': true,
'Loader': true,
'mixpanel': true,
'moment': true,
'openConfirm': true,
'openModal': true,
'OptionalBool': true,
'OptionalFunc': true,
'OptionalNode': true,
'Permission': true,
'Project': true,
'propTypes': true,
'Radio': true,
'React': true,
'ReactDOM': true,
'RequiredFunc': true,
'RequiredString': true,
'Row': true,
'routes': true,
'SENTRY_RELEASE_VERSION': true,
'Utils': true,
'_': true,
'__DEV__': true,
'closeModal': true,
'delighted': true,
'describe': true,
'em': true,
'flagsmith': true,
'ga': true,
'hot': true,
'isMobile': true,
'mixpanel': true,
'moment': true,
'openConfirm': true,
'openModal': true,
'pact': true,
'projectOverrides': true,
'propTypes': true,
'routes': true,
'testHelpers': true,
'toast': true,
'Utils': true,
'window': true
'window': true,
'zE': true
}
};
1 change: 1 addition & 0 deletions frontend/.husky/_/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
husky.sh
4 changes: 4 additions & 0 deletions frontend/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged --allow-empty
8 changes: 6 additions & 2 deletions frontend/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require('dotenv').config();
const exphbs = require('express-handlebars');
const express = require('express');
const bodyParser = require('body-parser');
const xFrameOptions = require('x-frame-options');
const spm = require('./middleware/single-page-middleware');

const app = express();
Expand All @@ -14,7 +15,6 @@ const postToSlack = process.env.VERCEL_ENV === 'production';

const isDev = process.env.NODE_ENV !== 'production';
const port = process.env.PORT || 8080;
const xFrameOptions = require('x-frame-options');

app.use(xFrameOptions());

Expand Down Expand Up @@ -91,6 +91,7 @@ if (process.env.FLAGSMITH_PROXY_API_URL) {
}

if (isDev) { // Serve files from src directory and use webpack-dev-server
// eslint-disable-next-line
console.log('Enabled Webpack Hot Reloading');
const webpackMiddleware = require('./middleware/webpack-middleware');
webpackMiddleware(app);
Expand All @@ -114,6 +115,7 @@ app.get('/robots.txt', (req, res) => {
});

app.get('/health', (req, res) => {
// eslint-disable-next-line
console.log('Healthcheck complete');
res.send('OK');
});
Expand All @@ -132,7 +134,7 @@ app.post('/api/event', (req, res) => {
const body = req.body;
const channel = body.tag ? `infra_${body.tag.replace(/ /g, '').toLowerCase()}` : process.env.EVENTS_SLACK_CHANNEL;
if (process.env.SLACK_TOKEN && channel && postToSlack && !body.event.includes('Bullet Train')) {
const match = body.event.match(/([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})/);
const match = body.event.match(/([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5})/);
let url = '';
if (match && match[0]) {
const urlMatch = match[0].split('@')[1];
Expand All @@ -147,6 +149,7 @@ app.post('/api/event', (req, res) => {
res.json({});
}
} catch (e) {
// eslint-disable-next-line
console.log(`Error posting to from /api/event:${e}`);
}
});
Expand All @@ -161,6 +164,7 @@ app.get('/', (req, res) => {
});

app.listen(port, () => {
// eslint-disable-next-line
console.log(`Server listening on: ${port}`);
if (!isDev && process.send) {
process.send({ done: true });
Expand Down
2 changes: 1 addition & 1 deletion frontend/api/middleware/single-page-middleware.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Redirect routes to index.html for the single page app
module.exports = function (req, res, next) {
module.exports = function singlePageMiddleware(req, res, next) {
const headers = req.headers;
let rewriteTarget = '/index.html';

Expand Down
4 changes: 3 additions & 1 deletion frontend/api/middleware/webpack-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const config = require('../../webpack/webpack.config.local');
const compiler = webpack(config);


module.exports = function (app) {
module.exports = function webpackMiddleware(app) {
const middleware = webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
stats: {
Expand All @@ -26,11 +26,13 @@ module.exports = function (app) {
app.use(middleware);
middleware.waitUntilValid(() => {
if (process.send) { // Running as child process (i.e. via tests)
// eslint-disable-next-line
console.log('Sending completion of bundle to parent process');
process.send({ done: true });
}
});
app.use(webpackHotMiddleware(compiler, {
// eslint-disable-next-line
log: console.log,
path: '/__webpack_hmr',
heartbeat: 10 * 1000,
Expand Down
12 changes: 7 additions & 5 deletions frontend/api/slack-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ if (!process.env.SLACK_TOKEN) {

const web = new WebClient(process.env.SLACK_TOKEN);

const toChannel = async function (message, channel) {
console.log("sending to channel: " + channel + " message: " + message);
async function toChannel(message, channel) {
// eslint-disable-next-line
console.log(`sending to channel: ${channel} message: ${message}`);
try {
await web.chat.postMessage({
channel: '#' + channel,
channel: `#${channel}`,
text: message,
});
} catch (error) {
console.log("Error posting to Slack:" + error);
// eslint-disable-next-line
console.log(`Error posting to Slack:${error}`);
}
};
}

module.exports = toChannel;
2 changes: 1 addition & 1 deletion frontend/bin/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const env = process.env.ENV || 'dev';
const src = path.resolve(__dirname, `../env/project_${env}.js`);
const target = path.resolve(__dirname, '../common/project.js');


// eslint-disable-next-line
console.log(`Using project_${env}.js`.green);

fs.copySync(src, target);
7 changes: 4 additions & 3 deletions frontend/bin/upload-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ const fs = require('fs');

const SLACK_TOKEN = process.env.SLACK_TOKEN;

module.exports = function (path) {
module.exports = function uploadFile(path) {
if (!SLACK_TOKEN) {
// eslint-disable-next-line
console.log('Slack token not specified, skipping upload');
return;
}

const parts = path.split('/');
const filename = parts[parts.length - 1]; // Required
const title = 'Test Run'; // Optional
const channelId = 'C0102JZRG3G'; // infra_tests channel ID

// eslint-disable-next-line
console.log(`Uploading ${path}`);

const slackClient = new WebClient(process.env.SLACK_TOKEN);

// Call the files.upload method using the WebClient
Expand Down
3 changes: 2 additions & 1 deletion frontend/common/Component.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = function (options) {
module.exports = function Component(options) {
// eslint-disable-next-line
return React.createClass(Object.assign({}, options, {
_listeners: [],
setTheme(theme) {
Expand Down
14 changes: 7 additions & 7 deletions frontend/common/ES6Component.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = function (context, onUnmount) {
module.exports = function es6Component(context, onUnmount) {
context._listeners = [];

context.listenTo = function (store, event, callback) {
context.listenTo = function listenTo(store, event, callback) {
this._listeners.push({
store,
event,
Expand All @@ -11,12 +11,12 @@ module.exports = function (context, onUnmount) {
return this._listeners.length;
};

context.stopListening = function (index) {
context.stopListening = function stopListening(index) {
const listener = this._listeners[index];
listener.store.off(listener.event, listener.callback);
};

context.setTimedState = function (path, val, cooldown) { // set a temporary state, useful for showing things for a set amount of time
context.setTimedState = function setTimedState(path, val, cooldown) { // set a temporary state, useful for showing things for a set amount of time
const original = this.state[path];
const state = {};
if (original !== val) {
Expand All @@ -29,23 +29,23 @@ module.exports = function (context, onUnmount) {
}
};

context.setPathState = function (path, e) {
context.setPathState = function setPathState(path, e) {
return _.partial(() => {
const newState = {};
newState[path] = Utils.safeParseEventValue(e);
this.setState(newState);
});
};

context.toggleState = function (path) {
context.toggleState = function toggleState(path) {
return _.partial(() => {
const newState = {};
newState[path] = !this.state[path];
this.setState(newState);
});
};

context.componentWillUnmount = function () {
context.componentWillUnmount = function componentWillUnmount() {
_.each(this._listeners, (listener, index) => {
if (listener) this.stopListening(index);
});
Expand Down
9 changes: 4 additions & 5 deletions frontend/common/base/utils-base.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
export default {
safeParseEventValue(e) { // safe attempt to parse form value
let target;
if (!e) {
return e;
}
if (typeof e === 'string') {
return e;
}
target = e || e.target;
const target = e || e.target;

if (target.getAttribute) {
return target.type == 'checkbox' || target.type == 'radio' ? target.getAttribute('checked')
return target.type === 'checkbox' || target.type === 'radio' ? target.getAttribute('checked')
: target.getAttribute('data-value') || target.getAttribute('value');
}

if (e && e.target && (e.target.type == 'checkbox' || e.target.type == 'radio')) {
if (e && e.target && (e.target.type === 'checkbox' || e.target.type === 'radio')) {
return e.target.checked;
}

if (target.type && target.type == 'span' && e.target.textContent) {
if (target.type && target.type === 'span' && e.target.textContent) {
return e.target.textContent;
}
return e && e.target ? e.target.value : e;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = (envId, { LIB_NAME, LIB_NAME_JAVA, FEATURE_NAME, FEATURE_NAME_ALT }, userId) => `// Identify/create user
module.exports = (envId, _, userId) => `// Identify/create user

curl 'https://edge.api.flagsmith.com/api/v1/identities/?identifier=${userId}'
-H 'x-environment-key: ${envId}'
Expand Down
Loading