olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.91k stars 546 forks source link

global is undefined in strict mode #324

Open stkevintan opened 6 years ago

stkevintan commented 6 years ago

I'm using lunr with es6 syntax which need babel to compile. but babel will always enable the strict mode which will make an error in following source code:

lunr.utils.warn = (function (global) {
  /* eslint-disable no-console */
  return function (message) {
    if (global.console && console.warn) {
      console.warn(message);
    }
  }
  /* eslint-enable no-console */
})(this);

this in strict mode will be undefined instead of the window object .

By the way, I don't understand why using global.console instead of console, is it necessary for some edge case?

olivernn commented 6 years ago

Sorry for the delay. I think the reason there is so much ceremony around a simple console.warn is to protect against environments that do not have access to console.

Without using global checking for the presence of console is difficult, e.g. just doing the following will lead to a ReferenceError:

if (console && console.warn) {
  // ..snip...
}

If there is a way to perform this check without being broken by strict mode?

millette commented 5 years ago

@olivernn

if ((typeof console !== 'undefined') && console.warn) {
  // ...
}
thealjey commented 5 years ago

will this be addressed any time soon?