Repository: webpack-contrib/css-loader
Pull request #1006 (2019-12-06 14:07:45)

fix: do not crash when an assert return `null` or `undefined`
<!--
  HOLY CRAP a Pull Request. We ❤️ those!

  If you remove or skip this template, you'll make the 🐼 sad and the mighty god
  of Github will appear and pile-drive the close button from a great height
  while making animal noises.

  Please place an x (no spaces!) in all [ ] that apply
-->

This PR contains a:

- [x] **bugfix**
- [ ] new **feature**
- [ ] **code refactor**
- [x] **test update** <!-- if bug or feature is checked, this should be too -->
- [ ] **typo fix**
- [ ] **metadata update**

### Motivation / Use-Case

fixes #1005

### Breaking Changes

No

### Additional Info

No

codecov[bot]: # [Codecov](https://codecov.io/gh/webpack-contrib/css-loader/pull/1006?src=pr&el=h1) Report
> Merging [#1006](https://codecov.io/gh/webpack-contrib/css-loader/pull/1006?src=pr&el=desc) into [master](https://codecov.io/gh/webpack-contrib/css-loader/commit/80e966280f2477c5c0e4553d3be3a04511fea381?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/webpack-contrib/css-loader/pull/1006/graphs/tree.svg?width=650&token=DlN3OQ0JnO&height=150&src=pr)](https://codecov.io/gh/webpack-contrib/css-loader/pull/1006?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master    #1006   +/-   ##
=======================================
  Coverage   99.54%   99.54%           
=======================================
  Files          10       10           
  Lines         435      435           
  Branches      127      126    -1     
=======================================
  Hits          433      433           
  Misses          2        2
```


| [Impacted Files](https://codecov.io/gh/webpack-contrib/css-loader/pull/1006?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/runtime/getUrl.js](https://codecov.io/gh/webpack-contrib/css-loader/pull/1006/diff?src=pr&el=tree#diff-c3JjL3J1bnRpbWUvZ2V0VXJsLmpz) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/webpack-contrib/css-loader/pull/1006?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/webpack-contrib/css-loader/pull/1006?src=pr&el=footer). Last update [80e9662...c39258c](https://codecov.io/gh/webpack-contrib/css-loader/pull/1006?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/runtime/getUrl.js b/src/runtime/getUrl.js
index c2e3d50..1ae687a 100644
--- a/src/runtime/getUrl.js
+++ b/src/runtime/getUrl.js
@@ -5,7 +5,7 @@ module.exports = (url, options) => {
   }
 
   // eslint-disable-next-line no-underscore-dangle, no-param-reassign
-  url = url.__esModule ? url.default : url;
+  url = url && url.__esModule ? url.default : url;
 
   if (typeof url !== 'string') {
     return url;


Test patch:
diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap
index 4057209..fcc8126 100644
--- a/test/__snapshots__/loader.test.js.snap
+++ b/test/__snapshots__/loader.test.js.snap
@@ -109,7 +109,7 @@ exports[`loader should compile with \`css\` entry point (with \`modules\` and sc
   }
 
   // eslint-disable-next-line no-underscore-dangle, no-param-reassign
-  url = url.__esModule ? url.default : url;
+  url = url && url.__esModule ? url.default : url;
 
   if (typeof url !== 'string') {
     return url;
@@ -414,7 +414,7 @@ exports[`loader should compile with \`css\` entry point (with \`modules\` and sc
   }
 
   // eslint-disable-next-line no-underscore-dangle, no-param-reassign
-  url = url.__esModule ? url.default : url;
+  url = url && url.__esModule ? url.default : url;
 
   if (typeof url !== 'string') {
     return url;
@@ -743,7 +743,7 @@ exports[`loader should compile with \`css\` entry point: escape 1`] = `
   }
 
   // eslint-disable-next-line no-underscore-dangle, no-param-reassign
-  url = url.__esModule ? url.default : url;
+  url = url && url.__esModule ? url.default : url;
 
   if (typeof url !== 'string') {
     return url;
@@ -1048,7 +1048,7 @@ exports[`loader should compile with \`js\` entry point: escape 1`] = `
   }
 
   // eslint-disable-next-line no-underscore-dangle, no-param-reassign
-  url = url.__esModule ? url.default : url;
+  url = url && url.__esModule ? url.default : url;
 
   if (typeof url !== 'string') {
     return url;
@@ -1547,7 +1547,7 @@ module.exports = (url, options) => {
   }
 
   // eslint-disable-next-line no-underscore-dangle, no-param-reassign
-  url = url.__esModule ? url.default : url;
+  url = url && url.__esModule ? url.default : url;
 
   if (typeof url !== 'string') {
     return url;
@@ -1824,7 +1824,7 @@ module.exports = (url, options) => {
   }
 
   // eslint-disable-next-line no-underscore-dangle, no-param-reassign
-  url = url.__esModule ? url.default : url;
+  url = url && url.__esModule ? url.default : url;
 
   if (typeof url !== 'string') {
     return url;
diff --git a/test/runtime/__snapshots__/getUrl.test.js.snap b/test/runtime/__snapshots__/getUrl.test.js.snap
index d3f84ed..58b3bb2 100644
--- a/test/runtime/__snapshots__/getUrl.test.js.snap
+++ b/test/runtime/__snapshots__/getUrl.test.js.snap
@@ -2,73 +2,73 @@
 
 exports[`escape should escape url 1`] = `true`;
 
-exports[`escape should escape url 2`] = `"image.png"`;
+exports[`escape should escape url 2`] = `null`;
 
-exports[`escape should escape url 3`] = `"image.png"`;
+exports[`escape should escape url 3`] = `undefined`;
 
 exports[`escape should escape url 4`] = `"image.png"`;
 
-exports[`escape should escape url 5`] = `"\\"image other.png\\""`;
+exports[`escape should escape url 5`] = `"image.png"`;
 
-exports[`escape should escape url 6`] = `"\\"image other.png\\""`;
+exports[`escape should escape url 6`] = `"image.png"`;
 
 exports[`escape should escape url 7`] = `"\\"image other.png\\""`;
 
-exports[`escape should escape url 8`] = `"\\"image\\\\\\"other.png\\""`;
+exports[`escape should escape url 8`] = `"\\"image other.png\\""`;
 
-exports[`escape should escape url 9`] = `"\\"image\\\\nother.png\\""`;
+exports[`escape should escape url 9`] = `"\\"image other.png\\""`;
 
-exports[`escape should escape url 10`] = `"image.png#hash"`;
+exports[`escape should escape url 10`] = `"\\"image\\\\\\"other.png\\""`;
 
-exports[`escape should escape url 11`] = `"image.png#hash"`;
+exports[`escape should escape url 11`] = `"\\"image\\\\nother.png\\""`;
 
 exports[`escape should escape url 12`] = `"image.png#hash"`;
 
-exports[`escape should escape url 13`] = `"\\"image other.png#hash\\""`;
+exports[`escape should escape url 13`] = `"image.png#hash"`;
 
-exports[`escape should escape url 14`] = `"\\"image other.png#hash\\""`;
+exports[`escape should escape url 14`] = `"image.png#hash"`;
 
 exports[`escape should escape url 15`] = `"\\"image other.png#hash\\""`;
 
-exports[`escape should escape url 16`] = `"\\"image other.png\\""`;
+exports[`escape should escape url 16`] = `"\\"image other.png#hash\\""`;
 
-exports[`escape should escape url 17`] = `"\\"image other.png\\""`;
+exports[`escape should escape url 17`] = `"\\"image other.png#hash\\""`;
 
 exports[`escape should escape url 18`] = `"\\"image other.png\\""`;
 
-exports[`escape should escape url 19`] = `"image.png"`;
+exports[`escape should escape url 19`] = `"\\"image other.png\\""`;
 
-exports[`escape should escape url 20`] = `"image.png"`;
+exports[`escape should escape url 20`] = `"\\"image other.png\\""`;
 
 exports[`escape should escape url 21`] = `"image.png"`;
 
-exports[`escape should escape url 22`] = `"\\"image other.png\\""`;
+exports[`escape should escape url 22`] = `"image.png"`;
 
-exports[`escape should escape url 23`] = `"\\"image other.png\\""`;
+exports[`escape should escape url 23`] = `"image.png"`;
 
 exports[`escape should escape url 24`] = `"\\"image other.png\\""`;
 
-exports[`escape should escape url 25`] = `"\\"image\\\\\\"other.png\\""`;
+exports[`escape should escape url 25`] = `"\\"image other.png\\""`;
 
-exports[`escape should escape url 26`] = `"\\"image\\\\nother.png\\""`;
+exports[`escape should escape url 26`] = `"\\"image other.png\\""`;
 
-exports[`escape should escape url 27`] = `"image.png#hash"`;
+exports[`escape should escape url 27`] = `"\\"image\\\\\\"other.png\\""`;
 
-exports[`escape should escape url 28`] = `"image.png#hash"`;
+exports[`escape should escape url 28`] = `"\\"image\\\\nother.png\\""`;
 
 exports[`escape should escape url 29`] = `"image.png#hash"`;
 
-exports[`escape should escape url 30`] = `"\\"image other.png#hash\\""`;
+exports[`escape should escape url 30`] = `"image.png#hash"`;
 
-exports[`escape should escape url 31`] = `"\\"image other.png\\""`;
+exports[`escape should escape url 31`] = `"image.png#hash"`;
 
-exports[`escape should escape url 32`] = `"\\"image other.png\\""`;
+exports[`escape should escape url 32`] = `"\\"image other.png#hash\\""`;
 
 exports[`escape should escape url 33`] = `"\\"image other.png\\""`;
 
-exports[`escape should escape url 34`] = `"\\"image other.png#hash\\""`;
+exports[`escape should escape url 34`] = `"\\"image other.png\\""`;
 
-exports[`escape should escape url 35`] = `"\\"image other.png#hash\\""`;
+exports[`escape should escape url 35`] = `"\\"image other.png\\""`;
 
 exports[`escape should escape url 36`] = `"\\"image other.png#hash\\""`;
 
@@ -81,3 +81,7 @@ exports[`escape should escape url 39`] = `"\\"image other.png#hash\\""`;
 exports[`escape should escape url 40`] = `"\\"image other.png#hash\\""`;
 
 exports[`escape should escape url 41`] = `"\\"image other.png#hash\\""`;
+
+exports[`escape should escape url 42`] = `"\\"image other.png#hash\\""`;
+
+exports[`escape should escape url 43`] = `"\\"image other.png#hash\\""`;
diff --git a/test/runtime/getUrl.test.js b/test/runtime/getUrl.test.js
index aecf8d2..14ca22a 100644
--- a/test/runtime/getUrl.test.js
+++ b/test/runtime/getUrl.test.js
@@ -3,6 +3,9 @@ const getUrl = require('../../src/runtime/getUrl');
 describe('escape', () => {
   it('should escape url', () => {
     expect(getUrl(true)).toMatchSnapshot();
+    expect(getUrl(null)).toMatchSnapshot();
+    // eslint-disable-next-line no-undefined
+    expect(getUrl(undefined)).toMatchSnapshot();
     expect(getUrl('image.png')).toMatchSnapshot();
     expect(getUrl('"image.png"')).toMatchSnapshot();
     expect(getUrl("'image.png'")).toMatchSnapshot();