Skip to content

Commit cece432

Browse files
Philip von Bargentonyganch
authored andcommitted
Only call functions on existing sibling nodes
1 parent 4bbdac3 commit cece432

8 files changed

+32
-17
lines changed

src/options/remove-empty-rulesets.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ module.exports = (function() {
2525
function mergeAdjacentWhitespace(node) {
2626
var i = node.content.length - 1;
2727
while (i-- > 0) {
28-
if (node.get(i).is('space') && node.get(i + 1).is('space')) {
28+
if (node.get(i).is('space') &&
29+
node.get(i + 1) && node.get(i + 1).is('space')) {
2930
node.get(i).content += node.get(i + 1).content;
3031
node.removeChild(i + 1);
3132
}

src/options/space-after-colon.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = {
2727
return null;
2828

2929
// Remove any spaces after colon:
30-
if (parent.get(i + 1).is('space'))
30+
if (parent.get(i + 1) && parent.get(i + 1).is('space'))
3131
parent.removeChild(i + 1);
3232
// If the value set in config is not empty, add spaces:
3333
if (value !== '') {
@@ -51,8 +51,10 @@ module.exports = {
5151
let detected = [];
5252

5353
ast.traverseByType('propertyDelimiter', function(delimiter, i, parent) {
54-
if (parent.get(i + 1).is('space')) {
55-
detected.push(parent.get(i + 1).content);
54+
var nextNode = parent.get(i + 1);
55+
56+
if (nextNode.is('space')) {
57+
detected.push(nextNode.content);
5658
} else {
5759
detected.push('');
5860
}

src/options/space-after-combinator.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ module.exports = {
2323
let value = this.value;
2424

2525
ast.traverseByType('combinator', function(combinator, i, parent) {
26-
if (parent.get(i + 1).is('space')) {
27-
parent.get(i + 1).content = value;
26+
var nextNode = parent.get(i + 1);
27+
28+
if (nextNode && nextNode.is('space')) {
29+
nextNode.content = value;
2830
} else {
2931
var space = gonzales.createNode({
3032
type: 'space',
@@ -44,8 +46,10 @@ module.exports = {
4446
let detected = [];
4547

4648
ast.traverseByType('combinator', function(combinator, i, parent) {
47-
if (parent.get(i + 1).is('space')) {
48-
detected.push(parent.get(i + 1).content);
49+
var nextNode = parent.get(i + 1);
50+
51+
if (nextNode.is('space')) {
52+
detected.push(nextNode.content);
4953
} else {
5054
detected.push('');
5155
}

src/options/space-after-selector-delimiter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = {
2626
if (parent.is('arguments')) return;
2727

2828
var nextNode = parent.get(i + 1);
29+
if (!nextNode) return;
2930

3031
if (nextNode.is('space')) {
3132
nextNode.content = value;

src/options/space-before-colon.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ module.exports = {
5151
let detected = [];
5252

5353
ast.traverseByType('propertyDelimiter', function(delimiter, i, parent) {
54-
if (parent.get(i - 1).is('space')) {
55-
detected.push(parent.get(i - 1).content);
54+
var previousNode = parent.get(i - 1);
55+
56+
if (previousNode && previousNode.is('space')) {
57+
detected.push(previousNode.content);
5658
} else {
5759
detected.push('');
5860
}

src/options/space-before-combinator.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ module.exports = {
2323
let value = this.value;
2424

2525
ast.traverseByType('combinator', function(combinator, i, parent) {
26-
if (parent.get(i - 1).is('space')) {
27-
parent.get(i - 1).content = value;
26+
var previousNode = parent.get(i - 1);
27+
if (previousNode && previousNode.is('space')) {
28+
previousNode.content = value;
2829
} else {
2930
var space = gonzales.createNode({
3031
type: 'space',
@@ -44,8 +45,10 @@ module.exports = {
4445
let detected = [];
4546

4647
ast.traverseByType('combinator', function(combinator, i, parent) {
47-
if (parent.get(i - 1).is('space')) {
48-
detected.push(parent.get(i - 1).content);
48+
var previousNode = parent.get(i - 1);
49+
50+
if (previousNode && previousNode.is('space')) {
51+
detected.push(previousNode.content);
4952
} else {
5053
detected.push('');
5154
}

src/options/space-before-selector-delimiter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ module.exports = {
2626
if (parent.is('arguments')) return;
2727

2828
var previousNode = parent.get(i - 1);
29-
if (previousNode.is('space')) {
29+
30+
if (previousNode && previousNode.is('space')) {
3031
previousNode.content = value;
3132
} else {
3233
var space = gonzales.createNode({
@@ -50,7 +51,8 @@ module.exports = {
5051
if (parent.is('arguments')) return;
5152

5253
var previousNode = parent.get(i - 1);
53-
if (previousNode.is('space')) {
54+
55+
if (previousNode && previousNode.is('space')) {
5456
detected.push(previousNode.content);
5557
} else {
5658
detected.push('');

src/options/space-between-declarations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module.exports = (function() {
8282
}
8383

8484
var nextNode = parent.get(i + 1);
85-
if (nextNode.is('space')) {
85+
if (nextNode && nextNode.is('space')) {
8686
nextNode.content = value;
8787
} else {
8888
var space = gonzales.createNode({

0 commit comments

Comments
 (0)