@@ -13,8 +13,8 @@ export default lex;
1313 * @param {String } css CSS
1414 * @returns {Array } lexical tokens
1515 */
16- function lex ( css ) {
17- var start ; // Debug timer start.
16+ function lex ( css : string ) {
17+ var start = 0 ; // Debug timer start.
1818
1919 var buffer = '' ; // Character accumulator
2020 var ch ; // Current character
@@ -24,11 +24,11 @@ function lex(css) {
2424 var line = 1 ; // Current source line number
2525 var state = 'before-selector' ; // Current state
2626 var stack = [ state ] ; // State stack
27- var token = { } ; // Current token
28- var tokens = [ ] ; // Token accumulator
27+ var token : any = { } ; // Current token
28+ var tokens : any [ ] = [ ] ; // Token accumulator
2929
3030 // Supported @-rules, in roughly descending order of usage probability.
31- var atRules = [
31+ var atRules : any = [
3232 'media' ,
3333 'keyframes' ,
3434 { name : '-webkit-keyframes' , type : 'keyframes' , prefix : '-webkit-' } ,
@@ -65,7 +65,7 @@ function lex(css) {
6565 * @param {Number } [index=0] Index to return.
6666 * @returns {String } state
6767 */
68- function getState ( index ) {
68+ function getState ( index ?: number ) : string {
6969 return index ? stack [ stack . length - 1 - index ] : state ;
7070 }
7171
@@ -76,7 +76,7 @@ function lex(css) {
7676 * @param {String } str The string to look for.
7777 * @returns {Boolean } Whether the string was found.
7878 */
79- function isNextString ( str ) {
79+ function isNextString ( str : string ) : boolean {
8080 var start = cursor + 1 ;
8181 return ( str === css . slice ( start , start + str . length ) ) ;
8282 }
@@ -88,7 +88,7 @@ function lex(css) {
8888 * @param {String } str The substring to look for.
8989 * @returns {Number|false } The position, or `false` if not found.
9090 */
91- function find ( str ) {
91+ function find ( str : string ) : number | boolean {
9292 var pos = css . slice ( cursor ) . indexOf ( str ) ;
9393
9494 return pos > 0 ? pos : false ;
@@ -100,7 +100,7 @@ function lex(css) {
100100 * @param {String } ch Character.
101101 * @returns {Boolean } Whether the character is next.
102102 */
103- function isNextChar ( ch ) {
103+ function isNextChar ( ch : string ) : boolean {
104104 return ch === peek ( 1 ) ;
105105 }
106106
@@ -111,7 +111,7 @@ function lex(css) {
111111 * @param {Number } [offset=1] Cursor offset.
112112 * @returns {String } Character.
113113 */
114- function peek ( offset ) {
114+ function peek ( offset : number ) : string {
115115 return css [ cursor + ( offset || 1 ) ] ;
116116 }
117117
@@ -120,7 +120,7 @@ function lex(css) {
120120 *
121121 * @returns {String } The removed state.
122122 */
123- function popState ( ) {
123+ function popState ( ) : string | undefined {
124124 var removed = stack . pop ( ) ;
125125 state = stack [ stack . length - 1 ] ;
126126
@@ -133,7 +133,7 @@ function lex(css) {
133133 * @param {String } newState The new state.
134134 * @returns {Number } The new stack length.
135135 */
136- function pushState ( newState ) {
136+ function pushState ( newState : string ) : number {
137137 state = newState ;
138138 stack . push ( state ) ;
139139
@@ -146,7 +146,7 @@ function lex(css) {
146146 * @param {String } newState The new state.
147147 * @returns {String } The replaced state.
148148 */
149- function replaceState ( newState ) {
149+ function replaceState ( newState : string ) : string {
150150 var previousState = state ;
151151 stack [ stack . length - 1 ] = state = newState ;
152152
@@ -159,7 +159,7 @@ function lex(css) {
159159 *
160160 * @param {Number } [n=1] Number of characters to skip.
161161 */
162- function skip ( n ) {
162+ function skip ( n ?: number ) {
163163 if ( ( n || 1 ) == 1 ) {
164164 if ( css [ cursor ] == '\n' ) {
165165 line ++ ;
@@ -169,13 +169,13 @@ function lex(css) {
169169 }
170170 cursor ++ ;
171171 } else {
172- var skipStr = css . slice ( cursor , cursor + n ) . split ( '\n' ) ;
172+ var skipStr = css . slice ( cursor , cursor + ( n || 0 ) ) . split ( '\n' ) ;
173173 if ( skipStr . length > 1 ) {
174174 line += skipStr . length - 1 ;
175175 column = 1 ;
176176 }
177177 column += skipStr [ skipStr . length - 1 ] . length ;
178- cursor = cursor + n ;
178+ cursor = cursor + ( n || 0 ) ;
179179 }
180180 }
181181
@@ -201,7 +201,7 @@ function lex(css) {
201201 *
202202 * @param {String } type Token type.
203203 */
204- function initializeToken ( type ) {
204+ function initializeToken ( type : any ) {
205205 token = {
206206 type : type ,
207207 start : {
@@ -530,7 +530,7 @@ function lex(css) {
530530 // difficult to represent in the AST.
531531 var pos = find ( '*/' ) ;
532532
533- if ( pos ) {
533+ if ( pos && typeof pos !== "boolean" ) {
534534 skip ( pos + 1 ) ;
535535 }
536536 } else {
0 commit comments