Skip to content

Commit 9542d50

Browse files
committed
.
1 parent a21dcfb commit 9542d50

File tree

1 file changed

+24
-71
lines changed

1 file changed

+24
-71
lines changed

index.js

Lines changed: 24 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ var single = {
1313
}
1414

1515
// Pair delimiters.
16-
// From common sense, and WikiPedia: <https://en.wikipedia.org/wiki/Quotation_mark>.
16+
// From common sense, and WikiPedia:
17+
// <https://en.wikipedia.org/wiki/Quotation_mark>.
1718
var pairs = {
1819
',': {
1920
',': true
@@ -102,9 +103,10 @@ function isLiteral(parent, index) {
102103
}
103104

104105
if (
105-
(!hasWordsBefore(parent, index) && nextDelimiter(parent, index, single)) ||
106-
(!hasWordsAfter(parent, index) &&
107-
previousDelimiter(parent, index, single)) ||
106+
(!containsWord(parent, -1, index) &&
107+
siblingDelimiter(parent, index, 1, single)) ||
108+
(!containsWord(parent, index, parent.children.length) &&
109+
siblingDelimiter(parent, index, -1, single)) ||
108110
isWrapped(parent, index, pairs)
109111
) {
110112
return true
@@ -116,92 +118,43 @@ function isLiteral(parent, index) {
116118
// Check if the node in `parent` at `position` is enclosed by matching
117119
// delimiters.
118120
function isWrapped(parent, position, delimiters) {
119-
var previous = previousDelimiter(parent, position, delimiters)
121+
var previous = siblingDelimiter(parent, position, -1, delimiters)
120122
var next
121123

122124
if (previous) {
123-
next = nextDelimiter(parent, position, delimiters[toString(previous)])
125+
next = siblingDelimiter(parent, position, 1, delimiters[toString(previous)])
124126
}
125127

126-
return Boolean(next)
128+
return next
127129
}
128130

129-
// Find the previous delimiter before `position` in `parent`.
131+
// Find the previous or next delimiter before or after `position` in `parent`.
130132
// Returns the delimiter node when found.
131-
function previousDelimiter(parent, position, delimiters) {
132-
var siblings = parent.children
133-
var index = position
134-
var result
133+
function siblingDelimiter(parent, position, step, delimiters) {
134+
var index = position + step
135+
var sibling
135136

136-
while (index--) {
137-
result = delimiterCheck(siblings[index], delimiters)
137+
while (index > -1 && index < parent.children.length) {
138+
sibling = parent.children[index]
138139

139-
if (result === null) {
140-
continue
140+
if (sibling.type === 'WordNode' || sibling.type === 'SourceNode') {
141+
return
141142
}
142143

143-
return result
144-
}
145-
146-
return null
147-
}
148-
149-
// Find the next delimiter after `position` in `parent`.
150-
// Returns the delimiter node when found.
151-
function nextDelimiter(parent, position, delimiters) {
152-
var siblings = parent.children
153-
var index = position
154-
var length = siblings.length
155-
var result
156-
157-
while (++index < length) {
158-
result = delimiterCheck(siblings[index], delimiters)
159-
160-
if (result === null) {
161-
continue
144+
if (sibling.type !== 'WhiteSpaceNode') {
145+
return toString(sibling) in delimiters && sibling
162146
}
163147

164-
return result
165-
}
166-
167-
return null
168-
}
169-
170-
// Check if `node` is in `delimiters`.
171-
function delimiterCheck(node, delimiters) {
172-
var type = node.type
173-
174-
if (type === 'WordNode' || type === 'SourceNode') {
175-
return false
176-
}
177-
178-
if (type === 'WhiteSpaceNode') {
179-
return null
148+
index += step
180149
}
181-
182-
return toString(node) in delimiters ? node : false
183-
}
184-
185-
// Check if there are word nodes before `position` in `parent`.
186-
function hasWordsBefore(parent, position) {
187-
return containsWord(parent, 0, position)
188150
}
189151

190-
// Check if there are word nodes before `position` in `parent`.
191-
function hasWordsAfter(parent, position) {
192-
return containsWord(parent, position + 1, parent.children.length)
193-
}
194-
195-
// Check if parent contains word-nodes between `start` and `end`.
152+
// Check if parent contains word-nodes between `start` and `end` (both
153+
// excluding).
196154
function containsWord(parent, start, end) {
197-
var siblings = parent.children
198-
var index = start - 1
199-
200-
while (++index < end) {
201-
if (siblings[index].type === 'WordNode') {
155+
while (++start < end) {
156+
if (parent.children[start].type === 'WordNode') {
202157
return true
203158
}
204159
}
205-
206-
return false
207160
}

0 commit comments

Comments
 (0)