Skip to content

Commit 7c7ac61

Browse files
authored
Merge pull request #138 from yume-chan/master
Improve box model analysis
2 parents 3d13724 + 73ff3e5 commit 7c7ac61

File tree

4 files changed

+384
-61
lines changed

4 files changed

+384
-61
lines changed

src/services/lint.ts

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,13 @@
77
import * as languageFacts from './languageFacts';
88
import { Rules, LintConfigurationSettings, Rule, Settings } from './lintRules';
99
import * as nodes from '../parser/cssNodes';
10+
import calculateBoxModel, { Element } from './lintUtil';
11+
import { union } from '../utils/arrays';
1012

1113
import * as nls from 'vscode-nls';
1214
import { TextDocument } from 'vscode-languageserver-types';
1315
const localize = nls.loadMessageBundle();
1416

15-
class Element {
16-
17-
public name: string;
18-
public node: nodes.Declaration;
19-
20-
constructor(text: string, data: nodes.Declaration) {
21-
this.name = text;
22-
this.node = data;
23-
}
24-
}
25-
2617
class NodesByRootMap {
2718
public data: { [name: string]: { nodes: nodes.Node[]; names: string[] } } = {};
2819

@@ -58,7 +49,7 @@ export class LintVisitor implements nodes.IVisitor {
5849
private keyframes: NodesByRootMap;
5950
private documentText: string;
6051

61-
private validProperties : { [name: string]: boolean };
52+
private validProperties: { [name: string]: boolean };
6253

6354
private constructor(document: TextDocument, settings: LintConfigurationSettings) {
6455
this.settings = settings;
@@ -79,7 +70,7 @@ export class LintVisitor implements nodes.IVisitor {
7970
}
8071
}
8172

82-
private isValidPropertyDeclaration(decl: nodes.Declaration) : boolean {
73+
private isValidPropertyDeclaration(decl: nodes.Declaration): boolean {
8374
const propertyName = decl.getFullPropertyName().toLowerCase();
8475
return this.validProperties[propertyName];
8576
}
@@ -274,7 +265,6 @@ export class LintVisitor implements nodes.IVisitor {
274265
this.addEntry(node.getSelectors(), Rules.EmptyRuleSet);
275266
}
276267

277-
let self = this;
278268
let propertyTable: Element[] = [];
279269
for (let element of declarations.getChildren()) {
280270
if (element instanceof nodes.Declaration) {
@@ -290,46 +280,36 @@ export class LintVisitor implements nodes.IVisitor {
290280
// No error when box-sizing property is specified, as it assumes the user knows what he's doing.
291281
// see https://github.com/CSSLint/csslint/wiki/Beware-of-box-model-size
292282
/////////////////////////////////////////////////////////////
293-
if (this.fetch(propertyTable, 'box-sizing').length === 0) {
294-
let widthEntries = this.fetch(propertyTable, 'width');
295-
if (widthEntries.length > 0) {
296-
let problemDetected = false;
297-
for (let p of ['border', 'border-left', 'border-right', 'padding', 'padding-left', 'padding-right']) {
298-
let elements = this.fetch(propertyTable, p);
299-
for (let element of elements) {
300-
let value = element.node.getValue();
301-
if (value && !value.matches('none')) {
302-
this.addEntry(element.node, Rules.BewareOfBoxModelSize);
303-
problemDetected = true;
304-
}
305-
}
306-
}
307-
if (problemDetected) {
308-
for (let widthEntry of widthEntries) {
309-
this.addEntry(widthEntry.node, Rules.BewareOfBoxModelSize);
310-
}
311-
}
283+
const boxModel = calculateBoxModel(propertyTable);
284+
if (boxModel.width) {
285+
let properties: Element[] = [];
286+
if (boxModel.right.value) {
287+
properties = union(properties, boxModel.right.properties);
312288
}
313-
let heightEntries = this.fetch(propertyTable, 'height');
314-
if (heightEntries.length > 0) {
315-
let problemDetected = false;
316-
for (let p of ['border', 'border-top', 'border-bottom', 'padding', 'padding-top', 'padding-bottom']) {
317-
let elements = this.fetch(propertyTable, p);
318-
for (let element of elements) {
319-
let value = element.node.getValue();
320-
if (value && !value.matches('none')) {
321-
this.addEntry(element.node, Rules.BewareOfBoxModelSize);
322-
problemDetected = true;
323-
}
324-
}
289+
if (boxModel.left.value) {
290+
properties = union(properties, boxModel.left.properties);
291+
}
292+
if (properties.length !== 0) {
293+
for (const item of properties) {
294+
this.addEntry(item.node, Rules.BewareOfBoxModelSize);
325295
}
326-
if (problemDetected) {
327-
for (let heightEntry of heightEntries) {
328-
this.addEntry(heightEntry.node, Rules.BewareOfBoxModelSize);
329-
}
296+
this.addEntry(boxModel.width.node, Rules.BewareOfBoxModelSize);
297+
}
298+
}
299+
if (boxModel.height) {
300+
let properties: Element[] = [];
301+
if (boxModel.top.value) {
302+
properties = union(properties, boxModel.top.properties);
303+
}
304+
if (boxModel.bottom.value) {
305+
properties = union(properties, boxModel.bottom.properties);
306+
}
307+
if (properties.length !== 0) {
308+
for (const item of properties) {
309+
this.addEntry(item.node, Rules.BewareOfBoxModelSize);
330310
}
311+
this.addEntry(boxModel.height.node, Rules.BewareOfBoxModelSize);
331312
}
332-
333313
}
334314

335315
/////////////////////////////////////////////////////////////
@@ -340,14 +320,14 @@ export class LintVisitor implements nodes.IVisitor {
340320
let displayElems = this.fetchWithValue(propertyTable, 'display', 'inline');
341321
if (displayElems.length > 0) {
342322
for (let prop of ['width', 'height', 'margin-top', 'margin-bottom', 'float']) {
343-
let elem = self.fetch(propertyTable, prop);
323+
let elem = this.fetch(propertyTable, prop);
344324
for (let index = 0; index < elem.length; index++) {
345325
let node = elem[index].node;
346326
let value = node.getValue();
347327
if (prop === 'float' && (!value || value.matches('none'))) {
348328
continue;
349329
}
350-
self.addEntry(node, Rules.PropertyIgnoredDueToDisplay, localize('rule.propertyIgnoredDueToDisplayInline', "Property is ignored due to the display. With 'display: inline', the width, height, margin-top, margin-bottom, and float properties have no effect."));
330+
this.addEntry(node, Rules.PropertyIgnoredDueToDisplay, localize('rule.propertyIgnoredDueToDisplayInline', "Property is ignored due to the display. With 'display: inline', the width, height, margin-top, margin-bottom, and float properties have no effect."));
351331
}
352332
}
353333
}
@@ -502,7 +482,7 @@ export class LintVisitor implements nodes.IVisitor {
502482
return true;
503483
}
504484

505-
let decl = <nodes.Declaration> node.findParent(nodes.NodeType.Declaration);
485+
let decl = <nodes.Declaration>node.findParent(nodes.NodeType.Declaration);
506486
if (decl) {
507487
let declValue = decl.getValue();
508488
if (declValue) {
@@ -559,7 +539,7 @@ export class LintVisitor implements nodes.IVisitor {
559539
}
560540

561541
private visitHexColorValue(node: nodes.HexColorValue): boolean {
562-
// Rule: #eeff0011 or #eeff00 or #ef01 or #ef0
542+
// Rule: #eeff0011 or #eeff00 or #ef01 or #ef0
563543
let length = node.length;
564544
if (length !== 9 && length !== 7 && length !== 5 && length !== 4) {
565545
this.addEntry(node, Rules.HexColorLength);
@@ -601,5 +581,3 @@ export class LintVisitor implements nodes.IVisitor {
601581
return true;
602582
}
603583
}
604-
605-

0 commit comments

Comments
 (0)