I am trying to fix this Eslint error:
Expected an assignment or function call an instead saw an expression
Any idea on how I can accommodate the code so that it doesn't give me that error in following sample code?
window.matchMedia || (window.matchMedia = function() {
if (!styleMedia) {
const style = document.createElement('style');
const script = document.getElementsByTagName('script')[0];
let info = null;
style.type = 'text/css';
style.id = 'matchmediajs-test';
script.parentNode.insertBefore(style, script);
info = ('getComputedStyle' in window) && window.getComputedStyle(style, null) || style.currentStyle;
styleMedia = {
matchMedium: function(media) {
const text = '@media ' + media + '{ #matchmediajs-test { width: 1px; } }';
if (style.styleSheet) {
style.styleSheet.cssText = text;
} else {
style.textContent = text;
}
return info.width === '1px';
},
};
}
return function(media) {
return {
matches: styleMedia.matchMedium(media || 'all'),
media: media || 'all',
};
};
}());
Any idea why I am getting this lint error?
Thank you
Your first line (which encloses the whole script, actually)
window.matchMedia || (window.matchMedia = function() {
is an expression, not an assignment or function call. You instead need something like:
if (!window.matchMedia) {
window.matchMedia = function() {
// ..
}
instead of assigning inside the conditional.