Skip to content

Commit

Permalink
Fix single var statements not caught by automatic migration. (#305)
Browse files Browse the repository at this point in the history
* Fix single var statements in tests.

* Fix single var statements in src.

* Add prefer-const rule to eslintrc.

* Rename eslint.json to .eslintrc.

* Add eslint-plugin-standard.

* Fix tests to comply with prefer-const.

* Fix src to comply with prefer-const.

* Fix grunt eslint to use .eslintrc (i want to nuke grunt).
  • Loading branch information
tdeekens committed Aug 7, 2016
1 parent 9c0c3f7 commit b4b9280
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 296 deletions.
15 changes: 13 additions & 2 deletions eslint.json → .eslintrc
@@ -1,10 +1,20 @@
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 6,
"ecmaFeatures": {
},
"sourceType": "module"
},

"env": {
"browser": false
"es6": true,
"node": true
},

"plugins": [
"standard"
],

"globals": {
"require": 1,
"module": 1,
Expand All @@ -17,6 +27,7 @@
},

"rules": {
"prefer-const": 2,
"quotes": [2, "single"],
"eqeqeq": 0,
"strict": 0,
Expand Down
2 changes: 1 addition & 1 deletion grunt/tasks/eslint.js
@@ -1,7 +1,7 @@
/* globals module */
module.exports = {
options: {
configFile: 'eslint.json'
configFile: '.eslintrc'
},
target: [
'<%= paths.src.lint %>',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -11,7 +11,7 @@
},
"engineStrict": true,
"engines": {
"node": ">= 4"
"node": ">= 4"
},
"keywords": [
"grunt",
Expand Down Expand Up @@ -39,6 +39,7 @@
"chai-string": "1.2.0",
"es6-promise": "3.2.1",
"eslint": "3.2.2",
"eslint-plugin-standard": "^2.0.0",
"grunt": "1.0.1",
"grunt-cli": "1.2.0",
"grunt-contrib-clean": "1.0.0",
Expand Down
13 changes: 6 additions & 7 deletions src/cache.js
Expand Up @@ -42,7 +42,7 @@ export default class Cache {
return new Promise((resolve, reject) => {
if (!this.isSupported) { reject(); }

let _item = localStorage.getItem(`${this.cachePrefix}-${key}`);
const _item = localStorage.getItem(`${this.cachePrefix}-${key}`);

if (_item === null && defaultValue !== undefined) {
this.set(defaultValue, 'plain', key);
Expand Down Expand Up @@ -96,7 +96,7 @@ export default class Cache {
if (!this.isSupported) { return false; }
if (singularBy) { this.dedupe(singularBy); }

let cached = {
const cached = {
now: +new Date(),
url: key,
code: code,
Expand All @@ -115,7 +115,7 @@ export default class Cache {
flush() {
if (!this.isSupported) { return false; }

for (let key in localStorage) {
for (const key in localStorage) {
if (key.indexOf(this.cachePrefix) >= 0) {
this.log.log(`Removing item ${key} requested by flush.`);

Expand All @@ -127,7 +127,7 @@ export default class Cache {
}

supported() {
let item = '__dactylographsy__feature-detection';
const item = '__dactylographsy__feature-detection';

try {
localStorage.setItem(item, item);
Expand All @@ -142,13 +142,12 @@ export default class Cache {
}

dedupe(singularBy) {
for (let key in localStorage) {
for (const key in localStorage) {
const dactylographsyItem = key.indexOf(this.cachePrefix) >= 0;
let item;

if (!dactylographsyItem) { continue; }

item = JSON.parse(localStorage.getItem(key));
const item = JSON.parse(localStorage.getItem(key));

if (
( (typeof singularBy === 'string') && (typeof item.singularBy === 'string') ) &&
Expand Down
9 changes: 4 additions & 5 deletions src/dactylographsy.js
Expand Up @@ -5,9 +5,8 @@ import getUrlParam from './url';

export default class Dactylographsy {
constructor(options = {}) {
const
{ autorun = false } = options,
{ enableLogging = false } = options;
const { autorun = false } = options;
const { enableLogging = false } = options;

this.log = new Log(
getUrlParam('dactylographsy-enableLogging', enableLogging)
Expand Down Expand Up @@ -68,7 +67,7 @@ export default class Dactylographsy {
readAttrOnScript(attr) {
if (!this.executingScript) { return false; }

let _attr = this.executingScript.getAttribute('data-' + attr);
const _attr = this.executingScript.getAttribute('data-' + attr);

return _attr ? JSON.parse(_attr) : undefined;
}
Expand Down Expand Up @@ -108,7 +107,7 @@ export default class Dactylographsy {
this.config.cacheInLocalStorage === false
) ? this.refresh() : this.restore()
.then(injectedFromCache => {
let {
const {
refreshDelay = 5000
} = this.config;

Expand Down
28 changes: 14 additions & 14 deletions src/dom.js
Expand Up @@ -5,9 +5,11 @@ import getUrlParam from './url';

export class Js {
constructor(injectInto, config = {}) {
const {
verification = false
} = config;
let {
enableLogging = false,
verification = false,
cacheInLocalStorage = true
} = config;

Expand Down Expand Up @@ -37,7 +39,7 @@ export class Js {

injectWithText(text, url) {
return new Promise(resolve => {
let script = document.createElement('script');
const script = document.createElement('script');

this.log.info(`Creating <script />-tag with text for ${url}.`);

Expand All @@ -62,8 +64,8 @@ export class Js {
injectWithUrl(urls, whichUrl = 'printed') {
return new Promise(resolve => {
// Create script element and set its type
let script = document.createElement('script');
let url = urls[whichUrl];
const script = document.createElement('script');
const url = urls[whichUrl];

this.log.info(`Creating <script />-tag with url: ${url}.`);

Expand Down Expand Up @@ -123,7 +125,7 @@ export class Js {
return new Ajax()
.get(url)
.then(response => {
let { text: responseText } = response;
const { text: responseText } = response;

this.cache.set(responseText, 'js', url, singularBy);

Expand Down Expand Up @@ -159,9 +161,11 @@ export class Js {

export class Css {
constructor(injectInto, config = {}) {
const {
verification = false
} = config;
let {
enableLogging = false,
verification = false,
cacheInLocalStorage = true
} = config;

Expand Down Expand Up @@ -198,7 +202,7 @@ export class Css {
return new Ajax()
.get(url)
.then(response => {
let { text: responseText } = response;
const { text: responseText } = response;

this.cache.set(responseText, 'css', url, singularBy);

Expand All @@ -214,13 +218,11 @@ export class Css {

injectWithUrl(urls, whichUrl = 'printed') {
return new Promise(resolve => {
let link = document.createElement('link');
let url = urls[whichUrl];
const link = document.createElement('link');
const url = urls[whichUrl];

this.log.info(`Creating <link />-tag with url: ${url}.`);

link = document.createElement('link');

link.type = 'text/css';
link.rel = 'stylesheet';

Expand Down Expand Up @@ -250,12 +252,10 @@ export class Css {

injectWithText(text, url) {
return new Promise(resolve => {
let link = document.createElement('link');
const link = document.createElement('style');

this.log.info(`Creating <link />-tag with text for url: ${url}.`);

link = document.createElement('style');

link.setAttribute('data-dactylographsy-url', url);

link.textContent = text;
Expand Down
18 changes: 7 additions & 11 deletions src/injector.js
@@ -1,4 +1,4 @@
import {Css, Js} from './dom';
import { Css, Js } from './dom';
import Ajax from './ajax';
import Log from './log';
import getUrlParam from './url';
Expand All @@ -18,7 +18,7 @@ export class Manifest {
return new Ajax()
.get(this.url)
.then(response => {
let {
const {
text: responseText,
url: responseUrl
} = response;
Expand Down Expand Up @@ -101,13 +101,11 @@ export default class Injector {
}

injectManifest(manifest) {
let hashes = Object.keys(manifest.hashes);
const hashes = Object.keys(manifest.hashes);

return Promise.all(hashes.map(hash => {
let dependency = manifest.hashes[hash];
let rootUrl;

rootUrl = [manifest.rootUrl, manifest.packageUrl].filter(_url => {
const dependency = manifest.hashes[hash];
const rootUrl = [manifest.rootUrl, manifest.packageUrl].filter(_url => {
return (
_url !== undefined &&
_url !== null
Expand Down Expand Up @@ -147,12 +145,10 @@ export default class Injector {
}

urls(dependency, rootUrl = '') {
let basename = this.basename(dependency.file);
let url;

const basename = this.basename(dependency.file);
// Filter out potential null values
// passed in as various parts of an url.
url = [this.prefix, rootUrl, dependency.path].filter(_url => {
const url = [this.prefix, rootUrl, dependency.path].filter(_url => {
return (
_url !== undefined &&
_url !== null
Expand Down
4 changes: 1 addition & 3 deletions src/url.js
@@ -1,11 +1,9 @@
const getParams = function(url) {
const query = url;
const regex = /[?&;](.+?)=([^&;]+)/g;
let params;
const params = {};
let match;

params = {};

if (query) {
while (match = regex.exec(query)) {
params[match[1]] = decodeURIComponent(match[2]);
Expand Down
14 changes: 7 additions & 7 deletions test/src/ajax.spec.js
Expand Up @@ -12,48 +12,48 @@ chai.use(sinonChai);
chai.use(chaiAsPromised);
chai.use(chaiString);

var fixtureUrl = 'base/test/src/fixtures/response.json';
const fixtureUrl = 'base/test/src/fixtures/response.json';

describe('Ajax', () => {
describe('get', () => {
it('should resolve the promise when fetching a file', () => {
let request = new Ajax().get(fixtureUrl);
const request = new Ajax().get(fixtureUrl);

request.should.be.fulfilled;
});

it('should resolve the promise with properties xhr url and text', () => {
let request = new Ajax().get(fixtureUrl);
const request = new Ajax().get(fixtureUrl);

request.should.to.eventually.have.property('xhr');
request.should.to.eventually.have.property('url');
request.should.to.eventually.have.property('text');
});

it('should expose the url on resolving the promise', () => {
let request = new Ajax().get(fixtureUrl);
const request = new Ajax().get(fixtureUrl);

request.then(result => {
chai.expect(result.url).to.endsWith(fixtureUrl);
});
});

it('should expose the data on resolving the promise', () => {
let request = new Ajax().get(fixtureUrl);
const request = new Ajax().get(fixtureUrl);

request.then(result => {
chai.expect(result.text.indexOf('Bob')).to.be.at.least(1);
});
});

it('should reject then promise when not finding a file', () => {
let request = new Ajax().get(fixtureUrl + '--');
const request = new Ajax().get(fixtureUrl + '--');

request.should.be.rejected;
});

it('should set the withCredentials flag on the xhr', () => {
let request = new Ajax().get(fixtureUrl, {
const request = new Ajax().get(fixtureUrl, {
withCredentials: true
});

Expand Down

0 comments on commit b4b9280

Please sign in to comment.