Code Monkey home page Code Monkey logo

pug-parser's People

Contributors

evanw avatar forbeslindesay avatar timothygu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pug-parser's Issues

Line number are not reported for classes found in Jade #[] syntax blocks

@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
}

Bug when parsing textOnly tags

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

Plugin API

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).

Errors when using `#[]` syntax

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

Line numbers undefined for nested Tag

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

NPM is outdated

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;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.