Skip to content

Commit

Permalink
fix(error-messaging): add better error messaging when a transform or …
Browse files Browse the repository at this point in the history
…transformGroup does not exist (#264)
  • Loading branch information
chazzmoney authored and dbanksdesign committed Apr 23, 2019
1 parent 749db69 commit d5c0583
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 8 deletions.
10 changes: 8 additions & 2 deletions __tests__/buildPlatform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('buildPlatform', () => {
it('should throw if passed a platform that doesn\'t exist', () => {
expect(
StyleDictionaryExtended.buildPlatform.bind(test, 'foobar'),
).toThrow('Platform foobar doesn\'t exist');
).toThrow('Platform "foobar" does not exist');

expect(
function() {
Expand Down Expand Up @@ -164,9 +164,15 @@ describe('buildPlatform', () => {
}
}
});

let err = `
Unknown transformGroup "bar" found in platform "foo":
"bar" does not match the name of a registered transformGroup.
`;

expect(
StyleDictionaryExtended.buildPlatform.bind(StyleDictionaryExtended, 'foo'),
).toThrow('transformGroup bar doesn\'t exist');
).toThrow(err);
});

});
62 changes: 62 additions & 0 deletions __tests__/transform/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

var transformConfig = require('../../lib/transform/config');

const dictionary = {
transformGroup: {
fooTransformGroup: ['barTransform']
},
transform: {
fooTransform: {
type: 'attribute',
transformer: function() {
return {bar: 'foo'}
}
}
}
};

describe('transform', () => {
describe('config', () => {
it('Emits error when called with a transformGroup that does not exist in the dictionary', () => {
const noTransformGroupCfg = {
transformGroup: 'barTransformGroup'
};

let err = `
Unknown transformGroup "barTransformGroup" found in platform "test":
"barTransformGroup" does not match the name of a registered transformGroup.
`;

expect(
transformConfig.bind(null, noTransformGroupCfg, dictionary, 'test')
).toThrow(err);
});

it('Emits errors when called with a transform that does not exist', () => {
const noTransformCfg = {
transforms: ['fooTransform', 'barTransform', 'bazTransform']
};

let err = `
Unknown transforms "barTransform", "bazTransform" found in platform "test":
None of "barTransform", "bazTransform" match the name of a registered transform.
`;

expect(
transformConfig.bind(null, noTransformCfg, dictionary, 'test')
).toThrow(err);
});
});
});
4 changes: 2 additions & 2 deletions lib/buildPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ function buildPlatform(platform) {
console.log('\n' + platform);

if (!this.options || !_.has(this.options.platforms, platform)) {
throw new Error('Platform ' + platform + ' doesn\'t exist');
throw new Error(`Platform "${platform}" does not exist`);
}

var properties;
// We don't want to mutate the original object
var platformConfig = transformConfig(this.options.platforms[platform], this);
var platformConfig = transformConfig(this.options.platforms[platform], this, platform);

// We need to transform the object before we resolve the
// variable names because if a value contains concatenated
Expand Down
2 changes: 1 addition & 1 deletion lib/cleanPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function cleanPlatform(platform) {

var properties;
// We don't want to mutate the original object
var platformConfig = transformConfig(this.options.platforms[platform], this);
var platformConfig = transformConfig(this.options.platforms[platform], this, platform);

// We need to transform the object before we resolve the
// variable names because if a value contains concatenated
Expand Down
2 changes: 1 addition & 1 deletion lib/exportPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function exportPlatform(platform) {
}

// We don't want to mutate the original object
var platformConfig = transformConfig(this.options.platforms[platform], this);
var platformConfig = transformConfig(this.options.platforms[platform], this, platform);

// We need to transform the object before we resolve the
// variable names because if a value contains concatenated
Expand Down
37 changes: 35 additions & 2 deletions lib/transform/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var _ = require('lodash'),
GroupMessages = require('../utils/groupMessages');

var TEMPLATE_DEPRECATION_WARNINGS = GroupMessages.GROUP.TemplateDeprecationWarnings;
var MISSING_TRANSFORM_ERRORS = GroupMessages.GROUP.MissingRegisterTransformErrors;

/**
* Takes a platform config object and returns a new one
Expand All @@ -23,9 +24,10 @@ var TEMPLATE_DEPRECATION_WARNINGS = GroupMessages.GROUP.TemplateDeprecationWarni
* @private
* @param {Object} config
* @param {Object} dictionary
* @param {Object} platformName (only used for error messaging)
* @returns {Object}
*/
function transformConfig(config, dictionary) {
function transformConfig(config, dictionary, platformName) {
var to_ret = _.clone(config);

// The platform can define either a transformGroup or an array
Expand All @@ -40,17 +42,48 @@ function transformConfig(config, dictionary) {
if (dictionary.transformGroup[to_ret.transformGroup]) {
transforms = dictionary.transformGroup[to_ret.transformGroup];
} else {
throw new Error('transformGroup ' + to_ret.transformGroup + ' doesn\'t exist');
let err = `
Unknown transformGroup "${to_ret.transformGroup}" found in platform "${platformName}":
"${to_ret.transformGroup}" does not match the name of a registered transformGroup.
`;
throw new Error(err);
}
}

// Transforms are an array of strings that map to functions on
// the StyleDictionary module. We need to map the strings to
// the actual functions.
to_ret.transforms = _.map(transforms, function(name) {
if(!dictionary.transform[name]) {
GroupMessages.add(
MISSING_TRANSFORM_ERRORS,
`"${name}"`
);
}
return dictionary.transform[name];
});

let missingTransformCount = GroupMessages.count(MISSING_TRANSFORM_ERRORS);
if(missingTransformCount > 0) {
var transform_warnings = GroupMessages.flush(MISSING_TRANSFORM_ERRORS).join(', ');
let err;

if(missingTransformCount==1) {
err = `
Unknown transform ${transform_warnings} found in platform "${platformName}":
${transform_warnings} does not match the name of a registered transform.
`;
}
else {
err = `
Unknown transforms ${transform_warnings} found in platform "${platformName}":
None of ${transform_warnings} match the name of a registered transform.
`;
}

throw new Error(err);
}

to_ret.files = _.map(config.files, function(file) {
const ext = {};
if (file.filter) {
Expand Down
1 change: 1 addition & 0 deletions lib/utils/groupMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var GroupMessages = {
TemplateDeprecationWarnings: 'Template Deprecation Warnings',
RegisterTemplateDeprecationWarnings: 'Register Template Deprecation Warnings',
SassMapFormatDeprecationWarnings: 'Sass Map Format Deprecation Warnings',
MissingRegisterTransformErrors: 'Missing Register Transform Errors',
},

flush: function (messageGroup) {
Expand Down

0 comments on commit d5c0583

Please sign in to comment.