Skip to content

Commit

Permalink
ES6 / AirBnB config for es-lint (#180)
Browse files Browse the repository at this point in the history
Fixes: #153, #154

* apply air-bnb-base config
* use let/const
* use arrow functions
* use object shorten forms
* use templates
  • Loading branch information
vvscode authored and dbanksdesign committed Oct 26, 2018
1 parent 6b74d26 commit 44520a1
Show file tree
Hide file tree
Showing 78 changed files with 1,904 additions and 1,717 deletions.
21 changes: 17 additions & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@
"Buffer": true,
"escape": true
},
"extends": ["eslint:recommended", "prettier"],
"extends": ["airbnb-base", "prettier"],
"plugins": ["prettier"],
"rules": {
"no-console": 0,
"no-unused-vars": 1
}
"no-console": 0
},
"overrides": [
{
"files": ["__tests__/**//*.js"],
"globals": {
"expect": true,
"beforeAll": true,
"afterAll": true
},
"rules": {
"global-require": "off",
"import/no-unresolved": "off"
}
}
]
}
21 changes: 8 additions & 13 deletions __tests__/__helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,34 @@
* and limitations under the License.
*/

var fs = require('fs-extra');
const fs = require('fs-extra');

module.exports = {
clearOutput: function() {
fs.emptyDirSync('__tests__/__output');
},
clearOutput: () => fs.emptyDirSync('__tests__/__output'),

fileToJSON: function(path) {
return fs.readJsonSync(path);
},
fileToJSON: path => fs.readJsonSync(path),

fileExists: function(filePath) {
fileExists(filePath) {
try {
return fs.statSync(filePath).isFile();
} catch (err) {
return false;
}
},

pathDoesNotExist: function(path) {
pathDoesNotExist(path) {
try {
return !fs.existsSync(path);
} catch (err) {
return false;
}
},

dirDoesNotExist: function(dirPath) {
dirDoesNotExist(dirPath) {
return this.pathDoesNotExist(dirPath);
},

fileDoesNotExist: function(filePath) {
fileDoesNotExist(filePath) {
return this.pathDoesNotExist(filePath);

}
},
};
8 changes: 4 additions & 4 deletions __tests__/buildAllPlatforms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* and limitations under the License.
*/

var helpers = require('./__helpers');
var StyleDictionary = require('../index');
const helpers = require('./__helpers');
const StyleDictionary = require('../index');

describe('buildAllPlatforms', () => {

Expand All @@ -25,14 +25,14 @@ describe('buildAllPlatforms', () => {
});

it('should work with json config', () => {
var StyleDictionaryExtended = StyleDictionary.extend(__dirname + '/__configs/test.json');
const StyleDictionaryExtended = StyleDictionary.extend(`${__dirname}/__configs/test.json`);
StyleDictionaryExtended.buildAllPlatforms();
expect(helpers.fileExists('./__tests__/__output/web/_icons.css')).toBeTruthy();
expect(helpers.fileExists('./__tests__/__output/android/colors.xml')).toBeTruthy();
});

it('should work with js config', () => {
var StyleDictionaryExtended = StyleDictionary.extend(__dirname + '/__configs/test.js');
const StyleDictionaryExtended = StyleDictionary.extend(`${__dirname}/__configs/test.js`);
StyleDictionaryExtended.buildAllPlatforms();
expect(helpers.fileExists('./__tests__/__output/web/_icons.css')).toBeTruthy();
expect(helpers.fileExists('./__tests__/__output/android/colors.xml')).toBeTruthy();
Expand Down
4 changes: 2 additions & 2 deletions __tests__/buildFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* and limitations under the License.
*/

var buildFile = require('../lib/buildFile');
var helpers = require('./__helpers');
const buildFile = require('../lib/buildFile');
const helpers = require('./__helpers');

function format() {
return "hi";
Expand Down
96 changes: 40 additions & 56 deletions __tests__/buildFiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,117 +11,101 @@
* and limitations under the License.
*/

var buildFiles = require('../lib/buildFiles');
var helpers = require('./__helpers');
var _ = require('lodash');
const _ = require('lodash');
const buildFiles = require('../lib/buildFiles');
const helpers = require('./__helpers');

var dictionary = {
const dictionary = {
properties: {
foo: { value: 'bar' },
bingo: { value: 'bango' }
}
bingo: { value: 'bango' },
},
};

var platform = {
const platform = {
files: [
{
destination: '__tests__/__output/test.json',
format: function(dictionary) {
return JSON.stringify(dictionary.properties)
}
}
]
format: ({ properties }) => JSON.stringify(properties),
},
],
};

var platformWithBuildPath = {
const platformWithBuildPath = {
buildPath: '__tests__/__output/',
files: [
{
destination: 'test.json',
format: function(dictionary) {
return JSON.stringify(dictionary.properties)
}
}
]
format: ({ properties }) => JSON.stringify(properties),
},
],
};

var platformWithFilter = {
const platformWithFilter = {
buildPath: '__tests__/__output/',
files: [
{
destination: 'test.json',
filter: function(property) {
return property.value === "bango"
filter({ value }) {
return value === 'bango';
},
format: function(dictionary) {
return JSON.stringify(dictionary.properties)
},
}
]
format: ({ properties }) => JSON.stringify(properties),
},
],
};

var platformWithoutFormatter = {
const platformWithoutFormatter = {
buildPath: '__tests__/__output/',
files: [
{
destination: 'test.json',
}
]
},
],
};

var platformWithBadBuildPath = {
const platformWithBadBuildPath = {
buildPath: '__tests__/__output',
files: [
{
destination: 'test.json',
format: function(dictionary) {
return JSON.stringify(dictionary.properties)
}
}
]
format: ({ properties }) => JSON.stringify(properties),
},
],
};

describe('buildFiles', () => {
beforeEach(() => helpers.clearOutput());

beforeEach(() => {
helpers.clearOutput();
});
afterEach(() => helpers.clearOutput());

afterEach(() => {
helpers.clearOutput();
});

it('should throw if build path doesn\'t have a trailing slash', () => {
expect(
buildFiles.bind(null, dictionary, platformWithBadBuildPath),
).toThrow('Build path must end in a trailing slash or you will get weird file names.');
it("should throw if build path doesn't have a trailing slash", () => {
expect(buildFiles.bind(null, dictionary, platformWithBadBuildPath)).toThrow(
'Build path must end in a trailing slash or you will get weird file names.'
);
});

it('should throw if template or formatter missing', () => {
expect(
buildFiles.bind(null, dictionary, platformWithoutFormatter),
).toThrow('Please supply a template or formatter');
expect(buildFiles.bind(null, dictionary, platformWithoutFormatter)).toThrow(
'Please supply a template or formatter'
);
});

it('should work without buildPath', () => {
buildFiles( dictionary, platform );
buildFiles(dictionary, platform);
expect(helpers.fileExists('./__tests__/__output/test.json')).toBeTruthy();
});

it('should work with buildPath', () => {
buildFiles( dictionary, platformWithBuildPath );
buildFiles(dictionary, platformWithBuildPath);
expect(helpers.fileExists('./__tests__/__output/test.json')).toBeTruthy();
});

it('should work with a filter', () => {
buildFiles(dictionary, platformWithFilter);
expect(helpers.fileExists('./__tests__/__output/test.json')).toBeTruthy();
var output = require("./__output/test.json")
const output = require('./__output/test.json');
expect(output).toHaveProperty('bingo');
expect(output).not.toHaveProperty('foo');
_.each(output, function(property) {
expect(property.value).toBe('bango');
});
_.each(output, ({ value }) => expect(value).toBe('bango'));
});

});
28 changes: 14 additions & 14 deletions __tests__/buildPlatform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
* and limitations under the License.
*/

var helpers = require('./__helpers');
var config = helpers.fileToJSON(__dirname + '/__configs/test.json');
var StyleDictionary = require('../index');
var StyleDictionaryExtended = StyleDictionary.extend(config);
const helpers = require('./__helpers');
const config = helpers.fileToJSON(`${__dirname}/__configs/test.json`);
const StyleDictionary = require('../index');
const StyleDictionaryExtended = StyleDictionary.extend(config);

describe('buildPlatform', () => {

Expand All @@ -28,7 +28,7 @@ describe('buildPlatform', () => {
).toThrow('Platform foobar doesn\'t exist');

expect(
function() {
() => {
StyleDictionaryExtended.buildPlatform('web');
}
).not.toThrow();
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('buildPlatform', () => {
});

it('should handle non-string values in properties', () => {
var StyleDictionaryExtended = StyleDictionary.extend({
const StyleDictionaryExtended = StyleDictionary.extend({
source: ['__tests__/__properties/nonString.json'],
platforms: {
test: {
Expand All @@ -87,7 +87,7 @@ describe('buildPlatform', () => {
StyleDictionaryExtended.buildPlatform('test');
expect(helpers.fileExists('./__tests__/__output/output.json')).toBeTruthy();
// var input = helpers.fileToJSON('./__tests__/__properties/nonString.json');
var output = helpers.fileToJSON('./__tests__/__output/output.json');
const output = helpers.fileToJSON('./__tests__/__output/output.json');

// Make sure transforms run on non-string values as they normally would
expect(output).toHaveProperty('color.red.value', output.color.otherRed.value);
Expand All @@ -104,7 +104,7 @@ describe('buildPlatform', () => {
});

it('should handle non-property nodes', () => {
var StyleDictionaryExtended = StyleDictionary.extend({
const StyleDictionaryExtended = StyleDictionary.extend({
source: ['__tests__/__properties/nonPropertyNode.json'],
platforms: {
test: {
Expand All @@ -121,15 +121,15 @@ describe('buildPlatform', () => {
});
StyleDictionaryExtended.buildPlatform('test');
expect(helpers.fileExists('./__tests__/__output/output.json')).toBeTruthy();
var input = helpers.fileToJSON('./__tests__/__properties/nonPropertyNode.json');
var output = helpers.fileToJSON('./__tests__/__output/output.json');
const input = helpers.fileToJSON('./__tests__/__properties/nonPropertyNode.json');
const output = helpers.fileToJSON('./__tests__/__output/output.json');
expect(output.color.key1).toEqual(input.color.key1);
expect(output.color.base.red.key2).toEqual(input.color.base.red.key2);
expect(output.color.base.attributes.key3).toEqual(input.color.base.attributes.key3);
});

it('should handle comments', () => {
var StyleDictionaryExtended = StyleDictionary.extend({
const StyleDictionaryExtended = StyleDictionary.extend({
source: ['__tests__/__properties/comment.json'],
platforms: {
test: {
Expand All @@ -146,13 +146,13 @@ describe('buildPlatform', () => {
});
StyleDictionaryExtended.buildPlatform('test');
expect(helpers.fileExists('./__tests__/__output/output.json')).toBeTruthy();
var input = helpers.fileToJSON('./__tests__/__properties/comment.json');
var output = helpers.fileToJSON('./__tests__/__output/output.json');
const input = helpers.fileToJSON('./__tests__/__properties/comment.json');
const output = helpers.fileToJSON('./__tests__/__output/output.json');
expect(output.size.large.comment).toEqual(input.size.large.comment);
});

it('should throw an error if given a transformGroup that doesn\'t exist', () => {
var StyleDictionaryExtended = StyleDictionary.extend({
const StyleDictionaryExtended = StyleDictionary.extend({
source: ['__properties/**/*.json'],
platforms: {
foo: {
Expand Down
Loading

0 comments on commit 44520a1

Please sign in to comment.