Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
Implement tslint, fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
drublic committed Mar 21, 2018
1 parent a02b8c4 commit 47cfc12
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 78 deletions.
2 changes: 2 additions & 0 deletions __tests__/data/css.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default `
@charset "utf8";
/*! normalize.css v8.0.0 | MIT License | github.com/necolas/normalize.css */
/* Document
Expand Down
8 changes: 4 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { default as Output } from './lib/interfaces/Output'
export { default as Output } from "./lib/interfaces/Output";

import StaticStyles from './lib/StaticStyles'
import StaticStyles from "./lib/StaticStyles";

export default (html: string, css: string) => {
return new StaticStyles(html, css).get()
}
return new StaticStyles(html, css).get();
};
88 changes: 44 additions & 44 deletions lib/StaticStyles.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,89 @@
import * as Css from 'css'
import * as Css from "css";

import Context from './interfaces/Context'
import Output from './interfaces/Output'
import Context from "./interfaces/Context";
import Output from "./interfaces/Output";

import isPseudo from './isPseudo'
import convertToDom from './convertToDom'
import humanSize from './humanSize'
import convertToDom from "./convertToDom";
import humanSize from "./humanSize";
import isPseudo from "./isPseudo";

export default class StaticStyles {
private startTime: [number, number]
private context: Context
private startTime: [number, number];
private context: Context;

constructor(
private html: string,
private css: string
private css: string,
) {
this.startTime = process.hrtime()
this.startTime = process.hrtime();

this.context = convertToDom(html)
this.context = convertToDom(html);
}

public get(): Output {
const ast = Css.parse(this.css);

ast.stylesheet.rules = this.filterStyles(ast.stylesheet.rules);

const compressedCss: string = Css.stringify(ast, {
compress: true,
});

return {
css: compressedCss,
stats: {
efficiency: 1 - (compressedCss.length / this.css.length),
input: humanSize(this.css.length),
output: humanSize(compressedCss.length),
timeSpent: this.getTimeSpend(),
},
};
}

private filterStyles(rules: any[]): any[] {
return rules.map((rule) => {

if (rule.selectors) {
const matches = rule.selectors.map((selector: string): boolean => {
const pseudo = isPseudo(selector)
const pseudo = isPseudo(selector);
let matchingElements: NodeList;
let matchingElementsArray: Node[] = [];

if (pseudo) {
const selectorSet = selector.split(':')
const selectorSet = selector.split(":");

if (selectorSet[0] && selectorSet[0].length > 0) {
selector = selectorSet[0];
} else {
return true
return true;
}
}

try {
matchingElements = this.context.document.querySelectorAll(selector)
matchingElementsArray = Array.from(matchingElements)
matchingElements = this.context.document.querySelectorAll(selector);
matchingElementsArray = Array.from(matchingElements);
} catch (error) {}

return matchingElementsArray.length > 0
})
return matchingElementsArray.length > 0;
});

if (matches.indexOf(true) === -1) {
rule.declarations = []
rule.declarations = [];
}
} else if (rule.rules) {
rule.rules = this.filterStyles(rule.rules);
}

return rule
})
return rule;
});
}

private getTimeSpend(): [number, number] {
const currentTime: [number, number] = process.hrtime()
const currentTime: [number, number] = process.hrtime();

return [
currentTime[0] - this.startTime[0],
(currentTime[1] - this.startTime[1]) / 1000000 // get milli sec
]
}

public get(): Output {
const ast = Css.parse(this.css)

ast.stylesheet.rules = this.filterStyles(ast.stylesheet.rules)

const compressedCss: string = Css.stringify(ast, {
compress: true,
})

return {
stats: {
efficiency: 1 - (compressedCss.length / this.css.length),
timeSpent: this.getTimeSpend(),
input: humanSize(this.css.length),
output: humanSize(compressedCss.length),
},
css: compressedCss,
}
(currentTime[1] - this.startTime[1]) / 1000000, // get milli sec
];
}
}
16 changes: 8 additions & 8 deletions lib/convertToDom.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { JSDOM } from 'jsdom'
import { JSDOM } from "jsdom";

import Context from './interfaces/Context'
import Context from "./interfaces/Context";

const convertToDom = (html: string): Context => {
if (!html || html.length === 0) {
throw new Error('HTML not set')
throw new Error("HTML not set");
}

let jsdom: JSDOM = new JSDOM(html)
const jsdom: JSDOM = new JSDOM(html);

return {
document: jsdom.window.document,
jsdom,
window: jsdom.window,
document: jsdom.window.document
}
}
};
};

export default convertToDom
export default convertToDom;
8 changes: 4 additions & 4 deletions lib/humanSize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default (size: number): string => {
const i = Math.floor(Math.log(size) / Math.log(1024));
const number: string = (size / Math.pow(1024, i)).toFixed(2)
const sizes: string[] = ['B', 'kB', 'MB', 'GB', 'TB']
const sizeNumber: string = (size / Math.pow(1024, i)).toFixed(2);
const sizes: string[] = ["B", "kB", "MB", "GB", "TB"];

return `${number} ${sizes[i]}`
}
return `${sizeNumber} ${sizes[i]}`;
};
4 changes: 2 additions & 2 deletions lib/interfaces/Output.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default interface Output {
stats: object,
css: string,
stats: object;
css: string;
}
24 changes: 12 additions & 12 deletions lib/isPseudo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@
export default (selector: string): boolean => {
const ignoredPseudos: string[] = [
/* link */
':link', ':visited',
":link", ":visited",
/* user action */
':hover', ':active', ':focus', ':focus-within',
":hover", ":active", ":focus", ":focus-within",
/* UI element states */
':enabled', ':disabled', ':checked', ':indeterminate',
":enabled", ":disabled", ":checked", ":indeterminate",
/* form validation */
':required', ':invalid', ':valid',
":required", ":invalid", ":valid",
/* pseudo elements */
'::first-line', '::first-letter', '::selection', '::before', '::after',
"::first-line", "::first-letter", "::selection", "::before", "::after",
/* pseudo classes */
':target',
":target",
/* CSS2 pseudo elements */
':before', ':after',
":before", ":after",
/* Vendor-specific pseudo-elements:
* https://developer.mozilla.org/ja/docs/Glossary/Vendor_Prefix
*/
'::?-(?:moz|ms|webkit|o)-[a-z0-9-]+'
"::?-(?:moz|ms|webkit|o)-[a-z0-9-]+",
];

// Actual regex is of the format: /^(:hover|:focus|...)$/i
const pseudosRegex: RegExp = new RegExp('(' + ignoredPseudos.join('|') + ')', 'i');
const matches: RegExpMatchArray | null = selector.match(pseudosRegex)
const pseudosRegex: RegExp = new RegExp("(" + ignoredPseudos.join("|") + ")", "i");
const matches: RegExpMatchArray | null = selector.match(pseudosRegex);

return !!(matches && matches.length > 0)
}
return !!(matches && matches.length > 0);
};
26 changes: 26 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {
"variable-name": [
true,
"ban-keywords",
"check-format"
],
"interface-name": {
"options": [
"never-prefix"
]
}
},
"rulesDirectory": [],
"linterOptions": {
"exclude": [
"dist/**/*",
"node_modules/**/*"
]
}
}
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -725,10 +725,6 @@ tr46@^1.0.0:
dependencies:
punycode "^2.1.0"

tsc@^1.20150623.0:
version "1.20150623.0"
resolved "https://registry.yarnpkg.com/tsc/-/tsc-1.20150623.0.tgz#4ebc3c774e169148cbc768a7342533f082c7a6e5"

tslib@^1.8.0, tslib@^1.8.1:
version "1.9.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.0.tgz#e37a86fda8cbbaf23a057f473c9f4dc64e5fc2e8"
Expand Down

0 comments on commit 47cfc12

Please sign in to comment.