Skip to content

Commit

Permalink
Merge 486fd3c into 8483ea7
Browse files Browse the repository at this point in the history
  • Loading branch information
JPeer264 committed Feb 12, 2020
2 parents 8483ea7 + 486fd3c commit caace85
Show file tree
Hide file tree
Showing 20 changed files with 73 additions and 37 deletions.
File renamed without changes.
23 changes: 16 additions & 7 deletions __tests__/baseLibrary.js → __tests__/baseLibrary.ts
Expand Up @@ -22,7 +22,8 @@ it('exclude | should just exclude once', () => {
});

it('exclude | should exclude nothing', () => {
rcs.baseLibrary.setExclude();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(rcs.baseLibrary as any).setExclude();

expect(rcs.baseLibrary.excludes.length).toBe(0);
});
Expand All @@ -44,7 +45,8 @@ it('reserved | should just reserve once', () => {
});

it('reserved | should support resetting', () => {
rcs.baseLibrary.setReserved();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(rcs.baseLibrary as any).setReserved();

expect(rcs.baseLibrary.reserved.length).toBe(0);
});
Expand Down Expand Up @@ -73,6 +75,8 @@ it('setMultiple | should set multiple values', () => {

it('setMultiple | should set nothing', () => {
rcs.baseLibrary.setMultiple();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rcs.baseLibrary.setMultiple([] as any);

expect(Object.keys(rcs.baseLibrary.values).length).toBe(0);
});
Expand All @@ -87,11 +91,13 @@ it('setPrefix', () => {

expect(rcs.baseLibrary.prefix).toBe('pre-');

rcs.baseLibrary.setPrefix(1);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rcs.baseLibrary.setPrefix(1 as any);

expect(rcs.baseLibrary.prefix).toBe('pre-');

rcs.baseLibrary.setPrefix({});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rcs.baseLibrary.setPrefix({} as any);

expect(rcs.baseLibrary.prefix).toBe('pre-');

Expand All @@ -110,11 +116,13 @@ it('setSuffix', () => {

expect(rcs.baseLibrary.suffix).toBe('-suf');

rcs.baseLibrary.setSuffix(1);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rcs.baseLibrary.setSuffix(1 as any);

expect(rcs.baseLibrary.suffix).toBe('-suf');

rcs.baseLibrary.setSuffix({});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rcs.baseLibrary.setSuffix({} as any);

expect(rcs.baseLibrary.suffix).toBe('-suf');

Expand Down Expand Up @@ -217,7 +225,8 @@ it('swap | functional swap', () => {
* SET *
* *** */
it('set | should do nothing', () => {
rcs.baseLibrary.set();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(rcs.baseLibrary as any).set();

expect(rcs.baseLibrary.values).toEqual({});
});
Expand Down
Expand Up @@ -113,7 +113,8 @@ it('get | should get the minified values', () => {
* SET *
* *** */
it('set | should do nothing', () => {
rcs.cssVariablesLibrary.set();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(rcs.cssVariablesLibrary as any).set();

expect(rcs.cssVariablesLibrary.cssVariables).toEqual({});
});
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions __tests__/replace.any.js → __tests__/replace.any.ts
Expand Up @@ -6,11 +6,11 @@ import rcs from '../lib';
const fixturesCwd = '__tests__/files/fixtures';
const resultsCwd = '__tests__/files/results';

function replaceAnyMacro(input, expected, fillLibrary = '') {
function replaceAnyMacro(input, expected, fillLibrary = ''): void {
rcs.selectorsLibrary.fillLibrary(fillLibrary);

expect(rcs.replace.any(input)).toBe(expected);
expect(rcs.replace.any(Buffer.from(input))).toBe(expected);
expect(rcs.replace.any(Buffer.from(input).toString())).toBe(expected);
}

beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/replace.css.js → __tests__/replace.css.ts
Expand Up @@ -6,14 +6,14 @@ import rcs from '../lib';
const fixturesCwd = '__tests__/files/fixtures';
const resultsCwd = '__tests__/files/results';

function replaceCssMacro(input, expected = input, options = {}) {
function replaceCssMacro(input, expected = input, options = {}): void {
rcs.fillLibraries(input, options);

expect(rcs.replace.css(input)).toBe(expected);
expect(rcs.replace.css(Buffer.from(input))).toBe(expected);
}

function replaceMultipleCssMacro(inputs, expects, options = {}) {
function replaceMultipleCssMacro(inputs, expects, options = {}): void {
inputs.forEach((input, i) => {
rcs.fillLibraries(input, options);

Expand Down
2 changes: 1 addition & 1 deletion __tests__/replace.html.js → __tests__/replace.html.ts
Expand Up @@ -7,7 +7,7 @@ import rcs from '../lib';
const fixturesCwd = '__tests__/files/fixtures';
const resultsCwd = '__tests__/files/results';

function replaceHtmlMacro(selectors, input, expected, options) {
function replaceHtmlMacro(selectors, input, expected?, options?): void {
const toExpect = expected || input;

rcs.selectorsLibrary.fillLibrary(selectors);
Expand Down
2 changes: 1 addition & 1 deletion __tests__/replace.js.js → __tests__/replace.js.ts
Expand Up @@ -6,7 +6,7 @@ import rcs from '../lib';
const fixturesCwd = '__tests__/files/fixtures';
const resultsCwd = '__tests__/files/results';

function replaceJsMacro(input, expected, fillLibrary = fs.readFileSync(path.join(fixturesCwd, '/css/style.css'), 'utf8')) {
function replaceJsMacro(input, expected, fillLibrary = fs.readFileSync(path.join(fixturesCwd, '/css/style.css'), 'utf8')): void {
rcs.selectorsLibrary.fillLibrary(fillLibrary);
rcs.cssVariablesLibrary.fillLibrary(fillLibrary);

Expand Down
2 changes: 1 addition & 1 deletion __tests__/replace.pug.js → __tests__/replace.pug.ts
Expand Up @@ -8,7 +8,7 @@ import rcs from '../lib';
const fixturesCwd = '__tests__/files/fixtures';
const resultsCwd = '__tests__/files/results';

function replacePugMacro(selectors, input, expected, options) {
function replacePugMacro(selectors, input, expected?, options?): void {
const setter = {};
const toExpect = expected || input;

Expand Down
@@ -1,6 +1,6 @@
import rcs from '../lib';

const getRegex = () => rcs.selectorsLibrary.getAllRegex();
const getRegex = (): RegExp => rcs.selectorsLibrary.getAllRegex();

beforeEach(() => {
rcs.selectorsLibrary.setAlphabet('#abcdefghijklmnopqrstuvwxyz');
Expand Down
19 changes: 12 additions & 7 deletions __tests__/selectorsLibrary.js → __tests__/selectorsLibrary.ts
@@ -1,7 +1,7 @@
import rcs from '../lib';
import attributeLibrary from '../lib/attributeLibrary';

const setSelectors = () => {
const setSelectors = (): void => {
rcs.selectorsLibrary.set([
'.test',
'#id',
Expand Down Expand Up @@ -299,11 +299,13 @@ it('getall | should get all setted classes', () => {
let array = rcs.selectorsLibrary.getClassSelector().getAll();

expect(typeof array).toBe('object');
expect(array.test).toBe('a');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((array as any).test).toBe('a');
expect(array['jp-selector']).toBe('b');

array = rcs.selectorsLibrary.getIdSelector().getAll();
expect(array.id).toBe('a');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((array as any).id).toBe('a');
});

it('getall | should get all setted compressed classes', () => {
Expand All @@ -314,21 +316,24 @@ it('getall | should get all setted compressed classes', () => {
});

expect(typeof array).toBe('object');
expect(array.a).toBe('test');
expect(array.b).toBe('jp-selector');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((array as any).a).toBe('test');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((array as any).b).toBe('jp-selector');

array = rcs.selectorsLibrary.getIdSelector().getAll({
getRenamedValues: true,
});

expect(array.a).toBe('id');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((array as any).a).toBe('id');
});

/* *** *
* SET *
* *** */
it('set | should set nothing', () => {
rcs.selectorsLibrary.set();
(rcs.selectorsLibrary as any).set();

expect(rcs.selectorsLibrary.getClassSelector().values).toEqual({});
expect(rcs.selectorsLibrary.getIdSelector().values).toEqual({});
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions lib/attributeLibrary.ts
Expand Up @@ -14,6 +14,7 @@ interface AttributeSelector {

export interface AttributeLibraryOptions extends BaseLibraryOptions {
regex?: boolean;
getRenamedValues?: boolean;
}

// This abstract class implements the attribute parsing and replacing logic
Expand Down
10 changes: 7 additions & 3 deletions lib/baseLibrary.ts
Expand Up @@ -6,9 +6,11 @@ import warnings, { Source } from './allWarnings';
export interface BaseLibraryOptions {
ignoreAttributeSelectors?: boolean;
source?: Source;
isOriginalValue?: boolean;
preventRandomName?: boolean;
addSelectorType?: boolean;
countStats?: boolean;
getRenamedValues?: boolean;
}

export class BaseLibrary {
Expand Down Expand Up @@ -98,11 +100,12 @@ export class BaseLibrary {
}

get(value: string, opts: BaseLibraryOptions = {}): string {
const optionsDefault = {
const optionsDefault: BaseLibraryOptions = {
isOriginalValue: true,
countStats: true,
};

// todo jpeer: remove merge
const options = merge({}, optionsDefault, opts);
// We need the selector's without its decoration (for example, "test" for input ".test")
const finalValue = this.prefetchValue(value);
Expand Down Expand Up @@ -235,7 +238,7 @@ export class BaseLibrary {
] = [val2, val1];
}

setMultiple(values: { [s: string]: string }, options: BaseLibraryOptions = {}): void {
setMultiple(values: { [s: string]: string } = {}, options: BaseLibraryOptions = {}): void {
if (Object.prototype.toString.call(values) !== '[object Object]') {
return;
}
Expand Down Expand Up @@ -280,10 +283,11 @@ export class BaseLibrary {
}
}

setReserved(toReserve: string): void {
setReserved(toReserve: string | string[]): void {
if (!toReserve) return;

this.reserved = [];

if (!Array.isArray(toReserve)) {
this.reserved.push(toReserve);
} else {
Expand Down
18 changes: 13 additions & 5 deletions lib/fillLibraries.ts
Expand Up @@ -4,9 +4,17 @@ import cssVariablesLibrary from './cssVariablesLibrary';
import extractFromHtml from './helpers/extractFromHtml';
import { BaseLibraryOptions } from './baseLibrary';

export default (code: string | Buffer, opts: BaseLibraryOptions = {}): void => {
const defaultOptions = {
codeType: 'css', // 'css' | 'html'
export interface FillLibrariesOptions extends BaseLibraryOptions {
codeType?: 'css' | 'html';
ignoreCssVariables?: boolean;
replaceKeyframes?: boolean;
prefix?: string;
suffix?: string;
}

export default (code: string | Buffer, opts: FillLibrariesOptions = {}): void => {
const defaultOptions: FillLibrariesOptions = {
codeType: 'css',
ignoreAttributeSelectors: false,
ignoreCssVariables: false,
replaceKeyframes: false,
Expand All @@ -30,8 +38,8 @@ export default (code: string | Buffer, opts: BaseLibraryOptions = {}): void => {

const data = cssCode.toString();

selectorsLibrary.setPrefix(options.prefix);
selectorsLibrary.setSuffix(options.suffix);
selectorsLibrary.setPrefix(options.prefix as string);
selectorsLibrary.setSuffix(options.suffix as string);

if (!options.ignoreAttributeSelectors) {
selectorsLibrary.setAttributeSelector(data);
Expand Down
8 changes: 6 additions & 2 deletions lib/keyframesLibrary.ts
@@ -1,6 +1,10 @@
import { BaseLibrary, BaseLibraryOptions } from './baseLibrary';
import regex from './replace/regex';

export interface KeyframesLibraryOptions extends BaseLibraryOptions {
origKeyframe?: boolean;
}

export class KeyframesLibrary extends BaseLibrary {
constructor() {
super('keyframe');
Expand All @@ -17,8 +21,8 @@ export class KeyframesLibrary extends BaseLibrary {
}
} // /fillLibrary

get(selector: string, opts: BaseLibraryOptions = {}): string {
const defaultOptions = {
get(selector: string, opts: KeyframesLibraryOptions = {}): string {
const defaultOptions: KeyframesLibraryOptions = {
origKeyframe: true,
...opts,
};
Expand Down
2 changes: 1 addition & 1 deletion lib/replace/js.ts
Expand Up @@ -27,7 +27,7 @@ const makeSource = (node: any, file: string): Source => {
return { file, line: currentLine, text: sourceLine };
};

const replaceJs = (code: string | Buffer, espreeOptions: EspreeOptions): string => {
const replaceJs = (code: string | Buffer, espreeOptions: EspreeOptions = {}): string => {
// We can only use the common regex if we don't care about specific class/id processing
const regex = selectorsLibrary.getAllRegex();

Expand Down
10 changes: 7 additions & 3 deletions lib/replace/string.ts
Expand Up @@ -12,7 +12,11 @@ export interface ReplaceStringOptions extends BaseLibraryOptions {
addSelectorType?: boolean;
}

const replaceString = (string: string, regex: RegExp, options: ReplaceStringOptions): string => {
const replaceString = (
string: string,
regex: RegExp,
options: ReplaceStringOptions = {},
): string => {
let result;
let tempString = string;

Expand All @@ -27,13 +31,13 @@ const replaceString = (string: string, regex: RegExp, options: ReplaceStringOpti
// detect if it's a selector
let surelySelector = tempString.match(regexp.likelySelector) !== null;

if (options && options.isJSX) {
if (options.isJSX) {
// with JSX, you can have code in JS that contains HTML's attribute directly so we
// can't say if it is a selector in case like 'if (something) return <div class="a b">;'
surelySelector = false;
}

if (options && options.classOnly !== undefined) {
if (options.classOnly !== undefined) {
surelySelector = false;
// if we know that it's a class or an id, let's only search those
selectorLib = options.classOnly
Expand Down

0 comments on commit caace85

Please sign in to comment.