Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error when setting non-serializable macro config #1083

Merged
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
43 changes: 43 additions & 0 deletions packages/macros/src/macros-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ export default class MacrosConfig {
);
}

if (!isSerializable(config)) {
throw new Error(
`[Embroider:MacrosConfig] the given config from '${fromPath}' for packageName '${packageName}' is not JSON serializable.`
);
}

let targetPackage = this.resolvePackage(fromPath, packageName);
let peers = getOrCreate(this.configs, targetPackage.root, () => []);
peers.push(config);
Expand Down Expand Up @@ -421,3 +427,40 @@ function defaultMergerFor(pkgRoot: string) {
return Object.assign({}, ...ownConfigs, ...otherConfigs);
};
}

function isSerializable(obj: object): boolean {
if (isScalar(obj)) {
return true;
}

if (Array.isArray(obj)) {
return !obj.some((arrayItem: any) => !isSerializable(arrayItem));
}

if (isPlainObject(obj)) {
for (let property in obj) {
let value = obj[property] as any;
if (!isSerializable(value)) {
return false;
}
}

return true;
}

return false;
}

function isScalar(val: any): boolean {
return (
typeof val === 'undefined' ||
typeof val === 'string' ||
typeof val === 'boolean' ||
typeof val === 'number' ||
val === null
);
}

function isPlainObject(obj: any): obj is Record<string, any> {
return typeof obj === 'object' && obj.constructor === Object && obj.toString() === '[object Object]';
}
42 changes: 42 additions & 0 deletions packages/macros/tests/babel/set-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { MacrosConfig } from '../../src/node';
import { allBabelVersions } from './helpers';

describe(`setConfig`, function () {
allBabelVersions(function () {
test('works with empty config', () => {
let config = {};

let macroConfig = MacrosConfig.for({}, __dirname);
macroConfig.setConfig(__filename, 'qunit', config);
});

test('works with POJO config', () => {
let config = {
str: 'yes',
num: 10,
bool: true,
undef: undefined,
nil: null,
arr: ['yes', 10, true, undefined, null, { inArr: true }],
obj: { nested: true },
};

let macroConfig = MacrosConfig.for({}, __dirname);
macroConfig.setConfig(__filename, 'qunit', config);
});

test('throws for non-serializable config', () => {
let config = {
obj: {
regex: /regex/,
},
};

let macroConfig = MacrosConfig.for({}, __dirname);

expect(() => macroConfig.setConfig(__filename, 'qunit', config)).toThrow(
`[Embroider:MacrosConfig] the given config from '${__filename}' for packageName 'qunit' is not JSON serializable.`
);
});
});
});