-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
225 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ npm-debug.log | |
.babelrc | ||
|
||
test/ | ||
update-cases | ||
.travis.yml | ||
.editorconfig | ||
.eslintrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,71 @@ | ||
import Root from 'postcss/lib/root'; | ||
import Comment from 'postcss/lib/comment'; | ||
import Root from 'postcss/lib/root'; | ||
|
||
export default class Parser { | ||
|
||
constructor(input) { | ||
this.input = input; | ||
|
||
this.line = 0; | ||
this.pos = 0; | ||
this.root = new Root(); | ||
this.current = this.root; | ||
this.spaces = ''; | ||
this.indent = false; | ||
|
||
this.root.source = { input, start: { line: 1, column: 1 } }; | ||
} | ||
|
||
loop() { | ||
while ( this.line < this.lines.length ) { | ||
this.line += 1; | ||
let line; | ||
while ( this.pos < this.lines.length ) { | ||
line = this.lines[this.pos]; | ||
|
||
if ( !this.indent && line.indent.length && !line.comment ) { | ||
this.indent = line.indent; | ||
this.root.raws.indent = this.indent; | ||
} | ||
|
||
if ( line.comment ) { | ||
this.comment(line); | ||
} else if ( line.atrule ) { | ||
this.atrule(line); | ||
} | ||
|
||
this.pos += 1; | ||
} | ||
} | ||
|
||
comment(line) { | ||
let token = line.tokens[0]; | ||
let node = new Comment(); | ||
this.init(node, token[2], token[3]); | ||
node.source.end = { line: token[4], column: token[5] }; | ||
|
||
let text = token[1]; | ||
if ( token[6] === 'inline' ) { | ||
text = text.slice(2); | ||
} else { | ||
text = text.slice(2, -2); | ||
} | ||
|
||
if ( /^\s*$/.test(text) ) { | ||
node.text = ''; | ||
node.raws.left = text; | ||
node.raws.right = ''; | ||
} else { | ||
let match = text.match(/^(\s*)([^]*[^\s])(\s*)$/); | ||
node.text = match[2]; | ||
node.raws.left = match[1]; | ||
node.raws.right = match[3]; | ||
} | ||
} | ||
|
||
atrule() { | ||
} | ||
|
||
init(node, line, column) { | ||
this.current.push(node); | ||
node.source = { start: { line, column }, input: this.input }; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"raws": {}, | ||
"type": "root", | ||
"nodes": [ | ||
{ | ||
"raws": { | ||
"left": " ", | ||
"right": " " | ||
}, | ||
"type": "comment", | ||
"source": { | ||
"start": { | ||
"line": 1, | ||
"column": 1 | ||
}, | ||
"input": { | ||
"file": "comments.sss" | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 10 | ||
} | ||
}, | ||
"text": "multi\n line" | ||
}, | ||
{ | ||
"raws": { | ||
"left": " ", | ||
"right": "" | ||
}, | ||
"type": "comment", | ||
"source": { | ||
"start": { | ||
"line": 3, | ||
"column": 1 | ||
}, | ||
"input": { | ||
"file": "comments.sss" | ||
}, | ||
"end": { | ||
"line": 3, | ||
"column": 9 | ||
} | ||
}, | ||
"text": "inline" | ||
} | ||
], | ||
"source": { | ||
"input": { | ||
"file": "comments.sss" | ||
}, | ||
"start": { | ||
"line": 1, | ||
"column": 1 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* multi | ||
line */ | ||
// inline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import parse from '../parse'; | ||
|
||
import { jsonify } from 'postcss-parser-tests'; | ||
import path from 'path'; | ||
import test from 'ava'; | ||
import fs from 'fs'; | ||
|
||
test('detects indent', t => { | ||
let root = parse('@one\n @two\n @three'); | ||
t.same(root.raws.indent, ' '); | ||
}); | ||
|
||
test('ignores comments in indent detection', t => { | ||
let root = parse('@one\n // comment\n @two'); | ||
t.same(root.raws.indent, ' '); | ||
}); | ||
|
||
let tests = fs.readdirSync(path.join(__dirname, 'cases')) | ||
.filter(i => path.extname(i) === '.sss' ); | ||
|
||
function read(file) { | ||
return fs.readFileSync(path.join(__dirname, 'cases', file)).toString(); | ||
} | ||
|
||
for ( let name of tests ) { | ||
test('parses ' + name, t => { | ||
let sss = read(name).trim(); | ||
let json = read(name.replace(/\.sss/, '.json')).trim(); | ||
let root = parse(sss, { from: name }); | ||
t.same(jsonify(root), json); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env node | ||
|
||
var parse = require('./parse'); | ||
|
||
var tests = require('postcss-parser-tests'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
|
||
var dir = path.join(__dirname, 'test', 'cases'); | ||
fs.readdirSync(dir).filter(function (i) { | ||
return path.extname(i) === '.sss'; | ||
}).forEach(function (name) { | ||
var sssFile = path.join(dir, name); | ||
var jsonFile = sssFile.replace(/\.sss/, '.json'); | ||
var sss = fs.readFileSync(sssFile).toString().trim(); | ||
var root = parse(sss, { from: name }); | ||
fs.writeFileSync(jsonFile, tests.jsonify(root) + '\n'); | ||
}); |