pugjs / pug-parser Goto Github PK
View Code? Open in Web Editor NEWHome Page: https://github.com/pugjs/pug/tree/master/packages/pug-parser
License: MIT License
Home Page: https://github.com/pugjs/pug/tree/master/packages/pug-parser
License: MIT License
@ForbesLindesay thanks for fixing #3, but just noticed that I forgot to mention that line numbers and source files aren't reported for interpolated tags either. See:
{
"type": "Tag",
"name": "a",
"selfClosing": false,
"block": {
"type": "Block",
"nodes": []
},
"attrs": [{
"name": "class",
"val": "\'rho\'",
"escaped": false
}, {
"name": "href",
"val": "\'#\'",
"escaped": true
}, {
"name": "class",
"val": "\'rho--modifier\'",
"escaped": true
}],
"attributeBlocks": [],
"isInline": true,
"line": 1
}
We already have void-elements, so why not split this out as well?
I've started testing on using jade-lexer+jade-parser instead of jade directly for my project.
I've noticed, that when parsing textOnly nodes (those with the dot at the end), there's a misbehavior. Like it is now, the text nodes are parsed on line 659, but then are dropped on line 683, where the parser tries to parse the text-block again.
I think the simple solution to this problem is to simply add a line like
if (tag.textOnly) break;
before line 659.
But then, it would still not work, because parseTextBlock needs the "start-pipeless-text" and "end-pipeless-text" tokens, which the lexer seems not to produce in this situation.
While looking for the solution, I've noticed another bug: Without this fix, the parser still parsed the full text-block, even if it was only told to parse one text-node. That's because of the way that parseText behaves. I think, when the type is "text", it should really just parse one text-node, right?
Another question: With jade, when parsing a script.
tag, it's children are nodes of type 'Code', while with jade-lexer and jade-parser, they are of type 'Text'. Is this intended?
I'm sorry if it's too early for bug reports ;)
Let me know if I can help you on something
Add an extra option, plugins
which should be an array of plugin objects. Plugins can define methods to handle custom token types in any of the places where we have switch statements at the moment. e.g. if id
shorthand and default
from case
/when
blocks weren't implemented, here's how you could implement them in a plugin:
opts = {
plugins: [
{
expressionTokens: {
'id': function (parser) {
parser.tokens.defer({
type: 'tag',
val: 'div',
line: parser.peek().line,
filename: parser.filename
});
return parser.parseExpr();
}
},
tagAttributeTokens: {
'id': function (parser, tag) {
var tok = parser.expect('id');
tag.attrs.push({
name: tok.type,
val: "'" + tok.val + "'",
escaped: false
});
}
},
caseTokens: {
'default': function (parser, block) {
var tok = parser.expect('default');
block.nodes.push({
type: 'When',
expr: 'default',
block: parser.parseBlockExpansion(),
debug: false,
line: tok.line,
filename: parser.filename
});
}
}
}
]
};
N.B. we should throw an exception if two plugins attempt to handle the same token type in the same place. We should also probably not allow plugins to handle built in tokens here (built in tokens can be handled in a post-parse plugin that can walk the tree and re-write the output AST or by re-writing tokens to a different name in a pre-parse plugin).
Both
p #[a.rho(href='#', class='rho--modifier') with inline link]
and
p Some text #[a.rho(href='#', class='rho--modifier')]
throw:
Uncaught Error: Unexpected token `end-jade-interpolation` expected `text`, `code`, `:`, `newline` or `eos`
But
p Some text #[a.rho(href='#', class='rho--modifier') with inline link]
does not
Example:
.alpha.beta(class='delta zeta')
.eta.epsilon
.lambda.lambda--modifier(class=gamma ? 'gamma' : null)
Results:
{ type: 'Tag',
name: 'div',
block: { type: 'Block', nodes: [ [Object], [Object] ] },
attrs:
[ { name: 'class', val: '\'alpha\'', escaped: false },
{ name: 'class', val: '\'beta\'', escaped: false },
{ name: 'class', val: '\'delta zeta\'', escaped: true } ],
attributeBlocks: [],
isInline: false,
line: 1
}
{ type: 'Tag',
name: 'div',
block: { type: 'Block', nodes: [ [Object] ] },
attrs:
[ { name: 'class', val: '\'eta\'', escaped: false },
{ name: 'class', val: '\'epsilon\'', escaped: false } ],
attributeBlocks: [],
isInline: false
}
{ type: 'Tag',
name: 'div',
block: { type: 'Block', nodes: [ [Object], [Object] ] },
attrs:
[ { name: 'class', val: '\'lambda\'', escaped: false },
{ name: 'class', val: '\'lambda--modifier\'', escaped: false },
{ name: 'class', val: 'gamma ? \'gamma\' : null', escaped: true } ],
attributeBlocks: [],
isInline: false
}
This is a results of the function line()
https://github.com/jadejs/jade-parser/blob/master/index.js#L73-L75 returning undefined as this.tokens.lineno
is undefined
Pug parser only shows line numbers. Not starting and ending line:col numbers.
Making proper sourcemaps without this info is kind of impossible.
Currently, the NPM package uses "const" twice in in the source. This has been fixed in the repo, but has not been updated on the package. It is causing Travis CI tests to fail on old versions of Node.
NPM Package:
388 parseBlockExpansion: function(){
389 var tok = this.accept(':');
390 if (tok) {
391 const expr = this.parseExpr();
392 return expr.type === 'Block' ? expr : this.initBlock(tok.line, [expr]);
corresponding to
Repo:
382 parseBlockExpansion: function(){
383 var tok = this.accept(':');
384 if (tok) {
385 return this.initBlock(tok.line, [this.parseExpr()]);
and one other removed instance.
NPM Package:
1056 case ':':
1057 this.advance();
1058 const expr = this.parseExpr();
1059 tag.block = expr.type === 'Block' ? expr : this.initBlock(tag.line, [expr]);
1060 break;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.