Fix: linting and update current tests

pull/6824/head
raivaibhav 5 years ago
parent 11630b9f4c
commit 1fc4b87836

@ -97,13 +97,25 @@ export default class Selector {
const { name } = indentifier; const { name } = indentifier;
const value_start = node.value && node.value.start; const value_start = node.value && node.value.start;
const content = `${char_at_start}${name}${format && value_start ? ' ' : ''}${matcher}${!value_start ? `${flags}${char_at_end}` : ''}`; const content = `${char_at_start}${name}${format && value_start ? ' ' : ''}${matcher}${!value_start ? `${flags}${char_at_end}` : ''}`;
code.overwrite(node.start, value_start || node.end , content); code.overwrite(node.start, value_start || node.end , content, { contentOnly: true });
if (value_start) { if (value_start) {
code.overwrite(value_start, node.end, `${node.value.value || ''}${flags}${char_at_end}`); let value = '';
switch (node.value.type) {
case 'String':
value = node.value.value;
break;
case 'Identifier':
value = node.value.name;
break;
default:
value = '';
} }
code.overwrite(value_start, node.end, `${value}${flags}${char_at_end}`, { contentOnly: true});
} }
} }
} }
}
}); });
} }

@ -45,7 +45,7 @@ function minify_declarations(
declarations.forEach((declaration, i) => { declarations.forEach((declaration, i) => {
const default_separator = i > 0 ? ';' : ''; const default_separator = i > 0 ? ';' : '';
// it will add \n and prepend string before every declaration // Adds \n and the prepend format before every declaration
const separator = format ? `${default_separator}\n${prepend_format}` : default_separator; const separator = format ? `${default_separator}\n${prepend_format}` : default_separator;
if (format) { if (format) {
overwrite(code, c, declaration.node.start, separator); overwrite(code, c, declaration.node.start, separator);
@ -69,10 +69,7 @@ class Rule {
this.node = node; this.node = node;
this.parent = parent; this.parent = parent;
this.selectors = node.prelude.children.map((node: CssNode) => new Selector(node, stylesheet)); this.selectors = node.prelude.children.map((node: CssNode) => new Selector(node, stylesheet));
this.declarations = node.block.children this.declarations = node.block.children.map((node: CssNode) => new Declaration(node));
.filter(node => node.type === 'Declaration')
.map((node: CssNode) => new Declaration(node));
this.childrens = [];
} }
apply(node: Element) { apply(node: Element) {
@ -88,13 +85,12 @@ class Rule {
minify(code: MagicString, _dev: boolean, format: boolean = false, prepend_format: string = '') { minify(code: MagicString, _dev: boolean, format: boolean = false, prepend_format: string = '') {
let c = this.node.start; let c = this.node.start;
let started = false; let started = false;
// if (prepend_format && format) {
// code.appendLeft(c, prepend_format);
// }
this.selectors.forEach((selector) => { this.selectors.forEach((selector) => {
if (selector.used) { if (selector.used) {
const intial_separator = started ? ',' : ''; const intial_separator = started ? ',' : '';
//this will add \n after every selector in same rule
//Adds \n after every selector in same rule
const separator = intial_separator && format ? `${intial_separator}\n` : intial_separator; const separator = intial_separator && format ? `${intial_separator}\n` : intial_separator;
if ((selector.node.start - c) > intial_separator.length) { if ((selector.node.start - c) > intial_separator.length) {
overwrite(code, c, selector.node.start, separator); overwrite(code, c, selector.node.start, separator);
@ -107,15 +103,16 @@ class Rule {
}); });
if (format) { if (format) {
// this is to add a space before the rule start or say before { // Adds a space before the rule start or say before {
overwrite(code, c, this.node.block.start, ' '); overwrite(code, c, this.node.block.start, ' ');
} else { } else {
code.remove(c, this.node.block.start); code.remove(c, this.node.block.start);
} }
c = this.node.block.start + 1; c = this.node.block.start + 1;
c = minify_declarations(code, c, this.declarations, format, prepend_format); c = minify_declarations(code, c, this.declarations, format, prepend_format);
if (format) { if (format) {
//add the next line just before rule end or say before } // Adds the next line just before rule end or say before }
overwrite(code, c, this.node.block.end - 1, `\n${prepend_format.replace('\t\t', '')}`); overwrite(code, c, this.node.block.end - 1, `\n${prepend_format.replace('\t\t', '')}`);
} else { } else {
code.remove(c, this.node.block.end - 1); code.remove(c, this.node.block.end - 1);
@ -182,7 +179,7 @@ class Declaration {
while (/\s/.test(code.original[start])) start += 1; while (/\s/.test(code.original[start])) start += 1;
if (format) { if (format) {
// this will add the space before the decalaration value; // Adds the space before the decalaration value;
overwrite(code, c, start, ': '); overwrite(code, c, start, ': ');
} else if (start - c > 1) { } else if (start - c > 1) {
code.overwrite(c, start, ':'); code.overwrite(c, start, ':');
@ -224,13 +221,21 @@ class Atrule {
const expression_char = code.original[this.node.prelude.start]; const expression_char = code.original[this.node.prelude.start];
let c = this.node.start + (expression_char === '(' ? 6 : 7); let c = this.node.start + (expression_char === '(' ? 6 : 7);
code.overwrite(this.node.start, this.node.prelude.start, '@media '); // code.overwrite(this.node.start, this.node.prelude.start, '@media ');
if (format) {
// Add space before the prelude
overwrite(code, c, this.node.prelude.start, ' ');
} else if (this.node.prelude.start > c) {
code.remove(c, this.node.prelude.start);
}
this.node.prelude.children.forEach((query: CssNode) => { this.node.prelude.children.forEach((query: CssNode) => {
// TODO minify queries // TODO minify queries
c = query.end; c = query.end;
}); });
if (format) { if (format) {
// Adds space before the prelude
overwrite(code, c, this.node.block.start, ' '); overwrite(code, c, this.node.block.start, ' ');
} else { } else {
code.remove(c, this.node.block.start); code.remove(c, this.node.block.start);
@ -239,6 +244,7 @@ class Atrule {
} else if (this.node.name === 'supports') { } else if (this.node.name === 'supports') {
let c = this.node.start + 9; let c = this.node.start + 9;
if (format) { if (format) {
// Add space before the prelude
overwrite(code, c, this.node.prelude.start, ' '); overwrite(code, c, this.node.prelude.start, ' ');
} else if (this.node.prelude.start - c > 1) { } else if (this.node.prelude.start - c > 1) {
code.overwrite(c, this.node.prelude.start, ' '); code.overwrite(c, this.node.prelude.start, ' ');
@ -249,6 +255,7 @@ class Atrule {
}); });
if (format) { if (format) {
// Add space before the prelude
overwrite(code, c, this.node.block.start, ' '); overwrite(code, c, this.node.block.start, ' ');
} else { } else {
code.remove(c, this.node.block.start); code.remove(c, this.node.block.start);
@ -257,6 +264,7 @@ class Atrule {
let c = this.node.start + this.node.name.length + 1; let c = this.node.start + this.node.name.length + 1;
if (this.node.prelude) { if (this.node.prelude) {
if (format) { if (format) {
// Add space before the prelude
overwrite(code, c, this.node.prelude.start, ' '); overwrite(code, c, this.node.prelude.start, ' ');
} else if (this.node.prelude.start - c > 1) { } else if (this.node.prelude.start - c > 1) {
code.overwrite(c, this.node.prelude.start, ' '); code.overwrite(c, this.node.prelude.start, ' ');
@ -265,6 +273,7 @@ class Atrule {
} }
if (this.node.block) { if (this.node.block) {
if (format) { if (format) {
// Add space before the block
overwrite(code, c, this.node.block.start, ' '); overwrite(code, c, this.node.block.start, ' ');
} else if (this.node.block.start - c > 0) { } else if (this.node.block.start - c > 0) {
code.remove(c, this.node.block.start); code.remove(c, this.node.block.start);
@ -284,12 +293,11 @@ class Atrule {
this.children.forEach(child => { this.children.forEach(child => {
if (child.is_used(dev)) { if (child.is_used(dev)) {
if (format && prepend_format) { if (format) {
// Add \n at the opening of atrule ie.e, after {
// this will add \n opening of atrule ie.e, after {
overwrite(code, c, child.node.start, '\n'); overwrite(code, c, child.node.start, '\n');
// this will add the necessary prepend space for case of nested child // Add the necessary prepend space for case of nested child
code.appendLeft(child.node.start, prepend_format); code.appendLeft(child.node.start, prepend_format);
} else { } else {
code.remove(c, child.node.start); code.remove(c, child.node.start);
@ -298,14 +306,17 @@ class Atrule {
c = child.node.end; c = child.node.end;
} }
}); });
if (format && prepend_format) { if (format) {
// add \n and prepend string before the closing of the atrule or say before the } // add \n and prepend string before the closing of the atrule or say before the }
// replace is a shortcut to ignore the first iteration // and replace is a shortcut to ignore the first iteration
overwrite(code, c, this.node.block.end - 1, `\n${prepend_format.replace('\t\t', '')}`); overwrite(code, c, this.node.block.end - 1, `\n${prepend_format.replace('\t\t', '')}`);
} else { } else {
code.remove(c, this.node.block.end - 1); code.remove(c, this.node.block.end - 1);
} }
} }
// for cssFormat, it add space before the media feature name
// and the node value, it will also minify the MediaFeature node
if (this.node.prelude) { if (this.node.prelude) {
walk(this.node.prelude.children as any, { walk(this.node.prelude.children as any, {
enter: (node: any, parent: any) => { enter: (node: any, parent: any) => {
@ -313,7 +324,7 @@ class Atrule {
const char_at_start = code.original[parent.start]; const char_at_start = code.original[parent.start];
const char_at_end = code.original[parent.end - 1]; const char_at_end = code.original[parent.end - 1];
if (node) { if (node) {
code.overwrite(parent.start, node.start , `${char_at_start}${parent.name}:${format ? ' ' : ''}`); code.overwrite(parent.start, node.start , `${char_at_start}${parent.name}:${format ? ' ' : ''}`, { contentOnly: true});
let value = ''; let value = '';
switch (node.type) { switch (node.type) {
case 'Number' : case 'Number' :
@ -331,9 +342,9 @@ class Atrule {
default: default:
value = ''; value = '';
} }
code.overwrite(node.start, parent.end, `${value}${char_at_end}`); code.overwrite(node.start, parent.end, `${value}${char_at_end}`, { contentOnly: true });
} else { } else {
code.overwrite(parent.start, parent.end , `${char_at_start}${parent.name}${char_at_end}`); code.overwrite(parent.start, parent.end , `${char_at_start}${parent.name}${char_at_end}`, { contentOnly: true });
} }
} }
} }
@ -358,7 +369,6 @@ class Atrule {
} }
}); });
} }
this.children.forEach(child => { this.children.forEach(child => {
child.transform(code, id, keyframes, max_amount_class_specificity_increased); child.transform(code, id, keyframes, max_amount_class_specificity_increased);
}); });
@ -440,7 +450,7 @@ export default class Stylesheet {
let current_atrule: Atrule = null; let current_atrule: Atrule = null;
walk(ast.css as any, { walk(ast.css as any, {
enter: (node: any, parent: any) => { enter: (node: any) => {
if (node.type === 'Atrule') { if (node.type === 'Atrule') {
const atrule = new Atrule(node); const atrule = new Atrule(node);
stack.push(atrule); stack.push(atrule);

Loading…
Cancel
Save