@@ -13,8 +13,8 @@ export default lex;
13
13
* @param {String } css CSS
14
14
* @returns {Array } lexical tokens
15
15
*/
16
- function lex ( css ) {
17
- var start ; // Debug timer start.
16
+ function lex ( css : string ) {
17
+ var start = 0 ; // Debug timer start.
18
18
19
19
var buffer = '' ; // Character accumulator
20
20
var ch ; // Current character
@@ -24,11 +24,11 @@ function lex(css) {
24
24
var line = 1 ; // Current source line number
25
25
var state = 'before-selector' ; // Current state
26
26
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
29
29
30
30
// Supported @-rules, in roughly descending order of usage probability.
31
- var atRules = [
31
+ var atRules : any = [
32
32
'media' ,
33
33
'keyframes' ,
34
34
{ name : '-webkit-keyframes' , type : 'keyframes' , prefix : '-webkit-' } ,
@@ -65,7 +65,7 @@ function lex(css) {
65
65
* @param {Number } [index=0] Index to return.
66
66
* @returns {String } state
67
67
*/
68
- function getState ( index ) {
68
+ function getState ( index ?: number ) : string {
69
69
return index ? stack [ stack . length - 1 - index ] : state ;
70
70
}
71
71
@@ -76,7 +76,7 @@ function lex(css) {
76
76
* @param {String } str The string to look for.
77
77
* @returns {Boolean } Whether the string was found.
78
78
*/
79
- function isNextString ( str ) {
79
+ function isNextString ( str : string ) : boolean {
80
80
var start = cursor + 1 ;
81
81
return ( str === css . slice ( start , start + str . length ) ) ;
82
82
}
@@ -88,7 +88,7 @@ function lex(css) {
88
88
* @param {String } str The substring to look for.
89
89
* @returns {Number|false } The position, or `false` if not found.
90
90
*/
91
- function find ( str ) {
91
+ function find ( str : string ) : number | boolean {
92
92
var pos = css . slice ( cursor ) . indexOf ( str ) ;
93
93
94
94
return pos > 0 ? pos : false ;
@@ -100,7 +100,7 @@ function lex(css) {
100
100
* @param {String } ch Character.
101
101
* @returns {Boolean } Whether the character is next.
102
102
*/
103
- function isNextChar ( ch ) {
103
+ function isNextChar ( ch : string ) : boolean {
104
104
return ch === peek ( 1 ) ;
105
105
}
106
106
@@ -111,7 +111,7 @@ function lex(css) {
111
111
* @param {Number } [offset=1] Cursor offset.
112
112
* @returns {String } Character.
113
113
*/
114
- function peek ( offset ) {
114
+ function peek ( offset : number ) : string {
115
115
return css [ cursor + ( offset || 1 ) ] ;
116
116
}
117
117
@@ -120,7 +120,7 @@ function lex(css) {
120
120
*
121
121
* @returns {String } The removed state.
122
122
*/
123
- function popState ( ) {
123
+ function popState ( ) : string | undefined {
124
124
var removed = stack . pop ( ) ;
125
125
state = stack [ stack . length - 1 ] ;
126
126
@@ -133,7 +133,7 @@ function lex(css) {
133
133
* @param {String } newState The new state.
134
134
* @returns {Number } The new stack length.
135
135
*/
136
- function pushState ( newState ) {
136
+ function pushState ( newState : string ) : number {
137
137
state = newState ;
138
138
stack . push ( state ) ;
139
139
@@ -146,7 +146,7 @@ function lex(css) {
146
146
* @param {String } newState The new state.
147
147
* @returns {String } The replaced state.
148
148
*/
149
- function replaceState ( newState ) {
149
+ function replaceState ( newState : string ) : string {
150
150
var previousState = state ;
151
151
stack [ stack . length - 1 ] = state = newState ;
152
152
@@ -159,7 +159,7 @@ function lex(css) {
159
159
*
160
160
* @param {Number } [n=1] Number of characters to skip.
161
161
*/
162
- function skip ( n ) {
162
+ function skip ( n ?: number ) {
163
163
if ( ( n || 1 ) == 1 ) {
164
164
if ( css [ cursor ] == '\n' ) {
165
165
line ++ ;
@@ -169,13 +169,13 @@ function lex(css) {
169
169
}
170
170
cursor ++ ;
171
171
} else {
172
- var skipStr = css . slice ( cursor , cursor + n ) . split ( '\n' ) ;
172
+ var skipStr = css . slice ( cursor , cursor + ( n || 0 ) ) . split ( '\n' ) ;
173
173
if ( skipStr . length > 1 ) {
174
174
line += skipStr . length - 1 ;
175
175
column = 1 ;
176
176
}
177
177
column += skipStr [ skipStr . length - 1 ] . length ;
178
- cursor = cursor + n ;
178
+ cursor = cursor + ( n || 0 ) ;
179
179
}
180
180
}
181
181
@@ -201,7 +201,7 @@ function lex(css) {
201
201
*
202
202
* @param {String } type Token type.
203
203
*/
204
- function initializeToken ( type ) {
204
+ function initializeToken ( type : any ) {
205
205
token = {
206
206
type : type ,
207
207
start : {
@@ -530,7 +530,7 @@ function lex(css) {
530
530
// difficult to represent in the AST.
531
531
var pos = find ( '*/' ) ;
532
532
533
- if ( pos ) {
533
+ if ( pos && typeof pos !== "boolean" ) {
534
534
skip ( pos + 1 ) ;
535
535
}
536
536
} else {
0 commit comments