Skip to content
This repository was archived by the owner on Dec 22, 2020. It is now read-only.
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
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/

import util from 'util';

import JSON5 from 'json5';

import { getOptions } from 'loader-utils';
Expand All @@ -25,5 +22,11 @@ export default function loader(source) {
this.emitError(error);
}

return `module.exports = ${util.inspect(value, { depth: null })}`;
value = value
? JSON5.stringify(value, null)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029')
: source;

return `module.exports = ${value}`;
}
13 changes: 13 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`loader should handle valid JSON5 1`] = `"module.exports = {unquoted:'and you can quote me on that',singleQuotes:'I can use \\"double quotes\\" here',lineBreaks:\\"Look, Mom! No \\\\\\\\n's!\\",hexadecimal:912559,leadingDecimalPoint:0.8675309,andTrailing:8675309,positiveSign:1,trailingComma:'in objects',andIn:['arrays'],backwardsCompatible:'with JSON'}"`;

exports[`loader should invalid characters 1`] = `"module.exports = {foo:'\\\\u2028\\\\u2029'}"`;

exports[`loader should preserve -Infinity 1`] = `"module.exports = {to:-Infinity}"`;

exports[`loader should preserve Infinity 1`] = `"module.exports = {to:Infinity}"`;

exports[`loader should preserve NaN 1`] = `"module.exports = {nan:NaN}"`;

exports[`loader should preserve null 1`] = `"module.exports = {null:null}"`;
126 changes: 109 additions & 17 deletions test/loader.test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,134 @@
import loader from '../src';
import JSON5 from 'json5';

const staticJson5 = "{name: 'test'}";
import loader from '../src';

describe('loader', () => {
test('should export the loader', (done) => {
it('should export the loader', (done) => {
expect(loader).toBeInstanceOf(Function);

done();
});

test('should convert to requires', (done) => {
const content = loader.call({}, staticJson5);
expect(content).toBe("module.exports = { name: 'test' }");
it('should handle valid JSON5', (done) => {
const json5 = `{
// comments
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! \\
No \\\\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}`;
const emitError = jest.fn();
const content = loader.call(
{
emitError,
},
json5
);

expect(content).toMatchSnapshot();

// eslint-disable-next-line no-eval
const js = eval(content);

expect(JSON5.stringify(js)).toEqual(JSON5.stringify(JSON5.parse(json5)));

done();
});

test('should handle invalid JSON5', (done) => {
const brokenJson5 = '{broken: json5}';
it('should invalid characters', (done) => {
const json5 = `{foo: '\u2028\u2029'}`;
const emitError = jest.fn();

loader.call(
const content = loader.call(
{
emitError,
},
brokenJson5
json5
);
expect(emitError).toHaveBeenCalledWith(expect.any(SyntaxError));

expect(content).toMatchSnapshot();

// eslint-disable-next-line no-eval
const js = eval(content);

expect(JSON5.stringify(js)).toEqual(JSON5.stringify(JSON5.parse(json5)));

done();
});

it('should preserve Infinity', (done) => {
const json5 = '{to : Infinity}';
const content = loader.call({}, json5);

expect(content).toMatchSnapshot();

// eslint-disable-next-line no-eval
const js = eval(content);

expect(JSON5.stringify(js)).toEqual(JSON5.stringify(JSON5.parse(json5)));

done();
});

it('should preserve -Infinity', (done) => {
const json5 = '{to : -Infinity}';
const content = loader.call({}, json5);

expect(content).toMatchSnapshot();

// eslint-disable-next-line no-eval
const js = eval(content);

expect(JSON5.stringify(js)).toEqual(JSON5.stringify(JSON5.parse(json5)));

done();
});

test('should preserve Infinity', (done) => {
const content = loader.call({}, '{to : Infinity}');
expect(content).toBe('module.exports = { to: Infinity }');
it('should preserve null', (done) => {
const json5 = '{null : null}';
const content = loader.call({}, json5);

expect(content).toMatchSnapshot();

// eslint-disable-next-line no-eval
const js = eval(content);

expect(JSON5.stringify(js)).toEqual(JSON5.stringify(JSON5.parse(json5)));

done();
});

test('should preserve NaN', (done) => {
it('should preserve NaN', (done) => {
const json5 = '{nan : NaN}';
const content = loader.call({}, '{nan : NaN}');
expect(content).toBe('module.exports = { nan: NaN }');

expect(content).toMatchSnapshot();

// eslint-disable-next-line no-eval
const js = eval(content);

expect(JSON5.stringify(js)).toEqual(JSON5.stringify(JSON5.parse(json5)));

done();
});

it('should handle invalid JSON5', (done) => {
const brokenJson5 = '{broken: json5}';
const emitError = jest.fn();

loader.call(
{
emitError,
},
brokenJson5
);

expect(emitError).toHaveBeenCalledWith(expect.any(SyntaxError));

done();
});
});