Skip to content

Commit

Permalink
refactor(types): more types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Apr 28, 2023
1 parent b71c780 commit a126050
Showing 1 changed file with 56 additions and 14 deletions.
70 changes: 56 additions & 14 deletions lib/css/walkCssTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ const CC_HYPHEN_MINUS = "-".charCodeAt(0);
const CC_LESS_THAN_SIGN = "<".charCodeAt(0);
const CC_GREATER_THAN_SIGN = ">".charCodeAt(0);

/**
* @param {number} cc char code
* @returns {boolean} true, if cc is a newline
*/
const _isNewLine = cc => {
return (
cc === CC_LINE_FEED || cc === CC_CARRIAGE_RETURN || cc === CC_FORM_FEED
Expand All @@ -84,6 +88,7 @@ const _isNewLine = cc => {

/** @type {CharHandler} */
const consumeSpace = (input, pos, callbacks) => {
/** @type {number} */
let cc;
do {
pos++;
Expand All @@ -92,6 +97,10 @@ const consumeSpace = (input, pos, callbacks) => {
return pos;
};

/**
* @param {number} cc char code
* @returns {boolean} true, if cc is a whitespace
*/
const _isWhiteSpace = cc => {
return (
cc === CC_LINE_FEED ||
Expand All @@ -102,6 +111,10 @@ const _isWhiteSpace = cc => {
);
};

/**
* @param {number} cc char code
* @returns {boolean} true, if cc is a start code point of an identifier
*/
const _isIdentStartCodePoint = cc => {
return (
(cc >= CC_LOWER_A && cc <= CC_LOWER_Z) ||
Expand Down Expand Up @@ -145,21 +158,27 @@ const consumeComments = (input, pos, callbacks) => {
};

/** @type {function(number): CharHandler} */
const consumeString = end => (input, pos, callbacks) => {
const consumeString = quote_cc => (input, pos, callbacks) => {
const start = pos;
pos = _consumeString(input, pos, end);
pos = _consumeString(input, pos, quote_cc);
if (callbacks.string !== undefined) {
pos = callbacks.string(input, start, pos);
}
return pos;
};

const _consumeString = (input, pos, end) => {
/**
* @param {string} input input
* @param {number} pos position
* @param {number} quote_cc quote char code
* @returns {number} new position
*/
const _consumeString = (input, pos, quote_cc) => {
pos++;
for (;;) {
if (pos === input.length) return pos;
const cc = input.charCodeAt(pos);
if (cc === end) return pos + 1;
if (cc === quote_cc) return pos + 1;
if (_isNewLine(cc)) {
// bad string
return pos;
Expand All @@ -176,6 +195,10 @@ const _consumeString = (input, pos, end) => {
}
};

/**
* @param {number} cc char code
* @returns {boolean} is identifier start code
*/
const _isIdentifierStartCode = cc => {
return (
cc === CC_LOW_LINE ||
Expand All @@ -185,16 +208,30 @@ const _isIdentifierStartCode = cc => {
);
};

/**
* @param {number} first first code point
* @param {number} second second code point
* @returns {boolean} true if two code points are a valid escape
*/
const _isTwoCodePointsAreValidEscape = (first, second) => {
if (first !== CC_REVERSE_SOLIDUS) return false;
if (_isNewLine(second)) return false;
return true;
};

/**
* @param {number} cc char code
* @returns {boolean} is digit
*/
const _isDigit = cc => {
return cc >= CC_0 && cc <= CC_9;
};

/**
* @param {string} input input
* @param {number} pos position
* @returns {boolean} true, if input at pos starts an identifier
*/
const _startsIdentifier = (input, pos) => {
const cc = input.charCodeAt(pos);
if (cc === CC_HYPHEN_MINUS) {
Expand All @@ -220,7 +257,7 @@ const consumeNumberSign = (input, pos, callbacks) => {
pos++;
if (pos === input.length) return pos;
if (callbacks.isSelector(input, pos) && _startsIdentifier(input, pos)) {
pos = _consumeIdentifier(input, pos);
pos = _consumeIdentifier(input, pos, callbacks);
if (callbacks.id !== undefined) {
return callbacks.id(input, start, pos);
}
Expand All @@ -244,7 +281,7 @@ const consumeMinus = (input, pos, callbacks) => {
if (cc === CC_GREATER_THAN_SIGN) {
return pos + 1;
} else {
pos = _consumeIdentifier(input, pos);
pos = _consumeIdentifier(input, pos, callbacks);
if (callbacks.identifier !== undefined) {
return callbacks.identifier(input, start, pos);
}
Expand All @@ -253,7 +290,7 @@ const consumeMinus = (input, pos, callbacks) => {
if (pos + 1 === input.length) return pos;
const cc = input.charCodeAt(pos + 1);
if (_isNewLine(cc)) return pos;
pos = _consumeIdentifier(input, pos);
pos = _consumeIdentifier(input, pos, callbacks);
if (callbacks.identifier !== undefined) {
return callbacks.identifier(input, start, pos);
}
Expand All @@ -272,16 +309,17 @@ const consumeDot = (input, pos, callbacks) => {
if (_isDigit(cc)) return consumeNumericToken(input, pos - 2, callbacks);
if (!callbacks.isSelector(input, pos) || !_startsIdentifier(input, pos))
return pos;
pos = _consumeIdentifier(input, pos);
pos = _consumeIdentifier(input, pos, callbacks);
if (callbacks.class !== undefined) return callbacks.class(input, start, pos);
return pos;
};

/** @type {CharHandler} */
const consumeNumericToken = (input, pos, callbacks) => {
pos = _consumeNumber(input, pos);
pos = _consumeNumber(input, pos, callbacks);
if (pos === input.length) return pos;
if (_startsIdentifier(input, pos)) return _consumeIdentifier(input, pos);
if (_startsIdentifier(input, pos))
return _consumeIdentifier(input, pos, callbacks);
const cc = input.charCodeAt(pos);
if (cc === CC_PERCENTAGE) return pos + 1;
return pos;
Expand All @@ -290,7 +328,7 @@ const consumeNumericToken = (input, pos, callbacks) => {
/** @type {CharHandler} */
const consumeOtherIdentifier = (input, pos, callbacks) => {
const start = pos;
pos = _consumeIdentifier(input, pos);
pos = _consumeIdentifier(input, pos, callbacks);
if (
pos !== input.length &&
!callbacks.isSelector(input, pos) &&
Expand All @@ -311,7 +349,7 @@ const consumeOtherIdentifier = (input, pos, callbacks) => {
/** @type {CharHandler} */
const consumePotentialUrl = (input, pos, callbacks) => {
const start = pos;
pos = _consumeIdentifier(input, pos);
pos = _consumeIdentifier(input, pos, callbacks);
const nextPos = pos + 1;
if (
pos === start + 3 &&
Expand All @@ -331,6 +369,7 @@ const consumePotentialUrl = (input, pos, callbacks) => {
return nextPos;
} else {
const contentStart = pos;
/** @type {number} */
let contentEnd;
for (;;) {
if (cc === CC_REVERSE_SOLIDUS) {
Expand Down Expand Up @@ -380,7 +419,7 @@ const consumePotentialPseudo = (input, pos, callbacks) => {
pos++;
if (!callbacks.isSelector(input, pos) || !_startsIdentifier(input, pos))
return pos;
pos = _consumeIdentifier(input, pos);
pos = _consumeIdentifier(input, pos, callbacks);
let cc = input.charCodeAt(pos);
if (cc === CC_LEFT_PARENTHESIS) {
pos++;
Expand Down Expand Up @@ -449,6 +488,7 @@ const consumeComma = (input, pos, callbacks) => {
return pos;
};

/** @type {CharHandler} */
const _consumeIdentifier = (input, pos) => {
for (;;) {
const cc = input.charCodeAt(pos);
Expand All @@ -468,6 +508,7 @@ const _consumeIdentifier = (input, pos) => {
}
};

/** @type {CharHandler} */
const _consumeNumber = (input, pos) => {
pos++;
if (pos === input.length) return pos;
Expand Down Expand Up @@ -526,12 +567,13 @@ const consumeLessThan = (input, pos, callbacks) => {
return pos + 1;
};

/** @type {CharHandler} */
const consumeAt = (input, pos, callbacks) => {
const start = pos;
pos++;
if (pos === input.length) return pos;
if (_startsIdentifier(input, pos)) {
pos = _consumeIdentifier(input, pos);
pos = _consumeIdentifier(input, pos, callbacks);
if (callbacks.atKeyword !== undefined) {
pos = callbacks.atKeyword(input, start, pos);
}
Expand Down

0 comments on commit a126050

Please sign in to comment.