Skip to content
Snippets Groups Projects
Unverified Commit 78947ddd authored by markrian's avatar markrian
Browse files

Remove no-op default exports

These no-op default exports have served one or both of these purposes:

1. Preventing `babel-plugin-rewire` from generating an invalid default
   during karma tests;
1. Working around the `import/prefer-default-export` ESLint rule for
   files which only have one named export.

As we recently finished migrating all relevant specs from Karma to Jest,
the first purpose is no longer necessary (with two exceptions, see
below).

The second purpose will become unnecessary once the [RFC][1] to prefer
named exports to default exports is implemented.

As such, this commit removes almost all no-op default exports, and adds
explicit `eslint-disable-next-line` directives (which can be removed
once the RFC is implemented).

---

This work was achieved in a few steps. First, the default exports
explicitly marked as `babel-plugin-rewire` workarounds were removed.

This was achieved via this command:

    rg --color=never -l0 "// prevent babel-plugin-rewire" \
        | xargs -0 \
        perl -0pi -e \
        's/\n+\/\/ prevent babel-plugin-rewire[^\n]*tests'\
    '(?:\nexport default \(\) => {};)?//mgs'

The documentation describing this workaround was then removed by hand.

Unfortunately, two files still participate in Karma tests, and still
need this workaround, so these were reverted manually. Those files (at
the time of writing) are:

    app/assets/javascripts/monitoring/stores/actions.js
    app/assets/javascripts/monitoring/stores/getters.js

Then, all additional no-op default exports were removed with this
command:

    rg --color=never -l0 -F "export default () => {};" \
        | xargs -0 perl -0pi -e 's/\n+export default \(\) => {};//mgs'

Since the `import/prefer-default-export` ESLint rule is still in place,
the files violating it have to disable it explicitly.

With the [RFC][1] to prefer named exports to default exports receiving
wide approval, it makes sense to mark these current violations
_explicitly_ with `eslint-disable-next-line` directives, rather than
_implicitly_ via the no-op default export hack.

The benefit of this approach is that when we disable the
`import/prefer-default-export` rule globally for the RFC, ESLint will
warn about all of these now-unnecessary directives. This will make it
much easier to address all of them (perhaps even automatically, via
`--fix`!).

[1]: https://gitlab.com/gitlab-org/frontend/rfcs/-/issues/20
parent 149d907c
No related branches found
No related tags found
No related merge requests found
Showing
with 4 additions and 68 deletions
Loading
Loading
@@ -28,6 +28,3 @@ export const saveSettings = ({ dispatch, state }) => {
)
.finally(() => dispatch('toggleLoading'));
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -32,6 +32,3 @@ export const fetchMergeRequests = ({ state, dispatch }) => {
createFlash(s__('Something went wrong while fetching related merge requests.'));
});
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -43,6 +43,3 @@ export const receiveReleasesError = ({ commit }) => {
commit(types.RECEIVE_RELEASES_ERROR);
createFlash(__('An error occurred while fetching the releases. Please try again.'));
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -74,6 +74,3 @@ export const receiveReportError = ({ commit, dispatch }) => {
commit(types.RECEIVE_REPORT_ERROR);
dispatch('stopPolling');
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -43,6 +43,3 @@ export const shouldRenderIssuesList = state =>
export const unresolvedIssues = state => state.report.existing_errors;
export const resolvedIssues = state => state.report.resolved_errors;
export const newIssues = state => state.report.new_errors;
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -85,6 +85,3 @@ export const openModal = ({ dispatch }, payload) => {
};
 
export const setModalData = ({ commit }, payload) => commit(types.SET_ISSUE_MODAL_DATA, payload);
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
import { LOADING, ERROR, SUCCESS, STATUS_FAILED } from '../constants';
 
// eslint-disable-next-line import/prefer-default-export
export const summaryStatus = state => {
if (state.isLoading) {
return LOADING;
Loading
Loading
@@ -11,6 +12,3 @@ export const summaryStatus = state => {
 
return SUCCESS;
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -123,6 +123,3 @@ export const fetchMetrics = ({ dispatch }, { metricsPath, hasPrometheus }) => {
createFlash(error);
});
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -5,6 +5,3 @@ export const hasPrometheusMissingData = state => state.hasPrometheus && !state.h
// Convert the function list into a k/v grouping based on the environment scope
 
export const getFunctions = state => translate(state.functions);
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -18,6 +18,3 @@ export const translate = functions =>
}),
{},
);
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -38,5 +38,3 @@ export const SnippetShowInit = () => {
export const SnippetEditInit = () => {
appFactory(document.getElementById('js-snippet-edit'), SnippetsEdit);
};
export default () => {};
Loading
Loading
@@ -2,6 +2,7 @@ import GetSnippetQuery from '../queries/snippet.query.graphql';
 
const blobsDefault = [];
 
// eslint-disable-next-line import/prefer-default-export
export const getSnippetMixin = {
apollo: {
snippet: {
Loading
Loading
@@ -39,5 +40,3 @@ export const getSnippetMixin = {
},
},
};
export default () => {};
Loading
Loading
@@ -69,6 +69,3 @@ export const receiveArtifactsSuccess = ({ commit }, response) => {
};
 
export const receiveArtifactsError = ({ commit }) => commit(types.RECEIVE_ARTIFACTS_ERROR);
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
import { s__, n__ } from '~/locale';
 
// eslint-disable-next-line import/prefer-default-export
export const title = state => {
if (state.isLoading) {
return s__('BuildArtifacts|Loading artifacts');
Loading
Loading
@@ -11,6 +12,3 @@ export const title = state => {
 
return n__('View exposed artifact', 'View %d exposed artifacts', state.artifacts.length);
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -56,6 +56,3 @@ export const createLabel = ({ state, dispatch }, label) => {
 
export const updateSelectedLabels = ({ commit }, labels) =>
commit(types.UPDATE_SELECTED_LABELS, { labels });
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -50,6 +50,3 @@ export const isDropdownVariantStandalone = state => state.variant === DropdownVa
* @param {object} state
*/
export const isDropdownVariantEmbedded = state => state.variant === DropdownVariant.Embedded;
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -9,6 +9,7 @@
* @param {string} root - the key of the state where to search fo they keys described in list
* @returns {Object} a dictionary with all the computed properties generated
*/
// eslint-disable-next-line import/prefer-default-export
export const mapComputed = (list, defaultUpdateFn, root) => {
const result = {};
list.forEach(item => {
Loading
Loading
@@ -32,5 +33,3 @@ export const mapComputed = (list, defaultUpdateFn, root) => {
});
return result;
};
export default () => {};
Loading
Loading
@@ -15,6 +15,3 @@ export const show = ({ commit }) => {
export const hide = ({ commit }) => {
commit(types.HIDE);
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
Loading
Loading
@@ -520,20 +520,6 @@ describe('component', () => {
});
```
 
#### Testing Vuex actions and getters
Because we're currently using [`babel-plugin-rewire`](https://github.com/speedskater/babel-plugin-rewire), you may encounter the following error when testing your Vuex actions and getters:
`[vuex] actions should be function or object with "handler" function`
To prevent this error from happening, you need to export an empty function as `default`:
```javascript
// getters.js or actions.js
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
```
### Two way data binding
 
When storing form data in Vuex, it is sometimes necessary to update the value stored. The store should never be mutated directly, and an action should be used instead.
Loading
Loading
Loading
Loading
@@ -62,6 +62,3 @@ export const setPage = ({ commit, dispatch }, data) => {
 
dispatch('fetchMergeRequests');
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment