Skip to content

Commit bd9dd01

Browse files
committed
feat: parser types
1 parent edb241f commit bd9dd01

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

parser.js renamed to parser.ts

+25-24
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ var TIMER = false; // `true` to time calls to `parse()` and print the results.
55
import dbg from './debug.js'
66
let debug = dbg('parse');
77

8-
import lex from './lexer.js';
8+
import lex from './lexer.ts';
99

1010
export default parse;
1111

12-
var _comments; // Whether comments are allowed.
13-
var _depth; // Current block nesting depth.
14-
var _position; // Whether to include line/column position.
15-
var _tokens; // Array of lexical tokens.
12+
var _comments: boolean; // Whether comments are allowed.
13+
var _depth: number; // Current block nesting depth.
14+
var _position: any; // Whether to include line/column position.
15+
var _tokens: any[]; // Array of lexical tokens.
1616

1717
/**
1818
* Convert a CSS string or array of lexical tokens into a `stringify`-able AST.
@@ -22,8 +22,8 @@ var _tokens; // Array of lexical tokens.
2222
* @param {Boolean} [options.comments=false] allow comment nodes in the AST
2323
* @returns {Object} `stringify`-able AST
2424
*/
25-
function parse(css, options) {
26-
var start; // Debug timer start.
25+
function parse(css: string | any[], options: any) {
26+
var start = 0; // Debug timer start.
2727

2828
options || (options = {});
2929
_comments = !!options.comments;
@@ -65,12 +65,12 @@ function parse(css, options) {
6565
* already in the token, or that will be added to the token.
6666
* @returns {Object} AST node
6767
*/
68-
function astNode(token, override) {
69-
override || (override = {});
68+
function astNode(token: any, overrd?: any): any {
69+
let override: any = overrd || {};
7070

7171
var key;
7272
var keys = ['type', 'name', 'value'];
73-
var node = {};
73+
var node: any = {};
7474

7575
// Avoiding [].forEach for performance reasons.
7676
for (var i = 0; i < keys.length; ++i) {
@@ -122,12 +122,12 @@ function next() {
122122
* @param {Object} token @-group lexical token
123123
* @returns {Object} @-group AST node
124124
*/
125-
function parseAtGroup(token) {
125+
function parseAtGroup(token: any): any {
126126
_depth = _depth + 1;
127127

128128
// As the @-group token is assembled, relevant token values are captured here
129129
// temporarily. They will later be used as `tokenize()` overrides.
130-
var overrides = {};
130+
var overrides: any = {};
131131

132132
switch (token.type) {
133133
case 'font-face':
@@ -154,7 +154,7 @@ function parseAtGroup(token) {
154154
* @param {Object} token @import lexical token
155155
* @returns {Object} @import AST node
156156
*/
157-
function parseAtImport(token) {
157+
function parseAtImport(token: any): any {
158158
return astNode(token);
159159
}
160160

@@ -164,7 +164,7 @@ function parseAtImport(token) {
164164
* @param {Object} token @charset lexical token
165165
* @returns {Object} @charset node
166166
*/
167-
function parseCharset(token) {
167+
function parseCharset(token: any): any {
168168
return astNode(token);
169169
}
170170

@@ -174,11 +174,11 @@ function parseCharset(token) {
174174
* @param {Object} token comment lexical token
175175
* @returns {Object} comment node
176176
*/
177-
function parseComment(token) {
177+
function parseComment(token: any): any {
178178
return astNode(token, {text: token.text});
179179
}
180180

181-
function parseNamespace(token) {
181+
function parseNamespace(token: any): any {
182182
return astNode(token);
183183
}
184184

@@ -187,7 +187,7 @@ function parseNamespace(token) {
187187
*
188188
* @returns {Object} property node
189189
*/
190-
function parseProperty(token) {
190+
function parseProperty(token: any): any {
191191
return astNode(token);
192192
}
193193

@@ -197,15 +197,16 @@ function parseProperty(token) {
197197
* @param {Object} token selector lexical token
198198
* @returns {Object} selector node
199199
*/
200-
function parseSelector(token) {
201-
function trim(str) {
200+
function parseSelector(token: any): any {
201+
function trim(str: string) {
202202
return str.trim();
203203
}
204204

205205
return astNode(token, {
206206
type: 'rule',
207207
selectors: token.text.split(',').map(trim),
208-
declarations: parseDeclarations(token)
208+
// parseDeclarations(token)
209+
declarations: parseDeclarations()
209210
});
210211
}
211212

@@ -214,7 +215,7 @@ function parseSelector(token) {
214215
*
215216
* @returns {Object|undefined} AST node
216217
*/
217-
function parseToken(token) {
218+
function parseToken(token: any): any {
218219
switch (token.type) {
219220
// Cases are listed in roughly descending order of probability.
220221
case 'property': return parseProperty(token);
@@ -255,7 +256,7 @@ function parseToken(token) {
255256
* @returns {Boolean} `true` if the token should be parsed, `false` otherwise
256257
* @return {Array} AST nodes
257258
*/
258-
function parseTokensWhile(conditionFn) {
259+
function parseTokensWhile(conditionFn: (token: any) => boolean | number): any {
259260
var node;
260261
var nodes = [];
261262
var token;
@@ -278,7 +279,7 @@ function parseTokensWhile(conditionFn) {
278279
*
279280
* @returns {Array} declaration nodes
280281
*/
281-
function parseDeclarations() {
282+
function parseDeclarations(): any {
282283
return parseTokensWhile(function (token) {
283284
return (token.type === 'property' || token.type === 'comment');
284285
});
@@ -289,6 +290,6 @@ function parseDeclarations() {
289290
*
290291
* @returns {Array} rule nodes
291292
*/
292-
function parseRules() {
293+
function parseRules(): any {
293294
return parseTokensWhile(function () { return _depth; });
294295
}

test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import parse from "./lexer.ts";
1+
import parse from "./parser.ts";
22

33
var ast = parse('p { color: black; }');
44
console.log(ast)

0 commit comments

Comments
 (0)