question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Clarify non-standard properties of AST that custom parsers should create

See original GitHub issue

I think we should clarify our position about non-standard properties of AST. Especially:

  • node.range
  • node.start
  • node.end
  • Properties of tokens and comments.

ESTree does not have those properties. However, we have been using node.range properties to write core rules since v0.x.y.

After ESLint v2.0.0, AST nodes have node.start and node.end properties as well for some reason. However, as far as I know, we have not been using those two properties in core rules, because the use of those two properties is a breaking change for custom parsers and we have not announced the change.

I thought so, but in fact, we have seemed to be using node.start and node.end in core rules, e.g. padded-blocks (1c123e29619fcc144262571688999d02777382cf).

If we clarify about non-standard properties of AST, it will become an important resource for the developer of custom parsers.

My position is:

  • AST nodes require node.range property.
  • AST nodes don’t require node.start and node.end properties (so we should not use node.start and node.end in core rules) at this time. We can add node.start and node.end in the next major release.
  • Tokens and comments are should be the following shape:
interface Token {
    type: string
    range: [number, number]
    loc: SourceLocation    // SourceLocation is defined in ESTree.
    value: string
}

Tell us about your environment

  • ESLint Version: v4.1.1
  • Node Version: v8.1.4
  • npm Version: v5.1.0

What parser (default, Babel-ESLint, etc.) are you using?

  • typescript-eslint-parser

What did you do? Please include the actual source code causing the issue.

/*eslint padded-blocks: [error, never] */
do {
    foo()
    // comment

} while (a)
$ eslint test.js --no-eslintrc --no-ignore --fix --parser typescript-eslint-parser

What did you expect to happen?

/*eslint padded-blocks: [error, never] */
do {
    foo()
    // comment
} while (a)

What actually happened? Please include the actual, raw output from ESLint.

The most code was lost.


} while (a)

The root cause is that the comment tokens of typescript-eslint-parser don’t have start and end properties but padded-blocks rule is using those.

I’m not sure which of padded-blocks and typescript-eslint-parser I should fix because the specification of AST is unclear.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
not-an-aardvarkcommented, Jul 17, 2017

@platinumazure Yes, I think so.

We could add an internal rule to prohibit node.start and node.end, but we would have to make sure it still allows node.loc.start and node.loc.end.

I don’t think we need to officially add support for node.start and node.end – if we consider it to be an implementation detail and always rely on node.range, then it will make it easier for parsers since they won’t have to support both values.

2reactions
not-an-aardvarkcommented, Jul 18, 2017

We could use a custom parser when running tests on the ESLint codebase. The parser would parse the source text with espree, then traverse the AST and delete all the start and end properties. That way we could disallow start and end within the ESLint codebase without modifying RuleTester for everyone else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Custom Parsers - Pluggable JavaScript Linter
The AST that custom parsers should create is based on ESTree. The AST requires some additional properties about detail information of the source...
Read more >
PostCSS + CSSTree · Issue #23 - GitHub
Save all spaces in AST. Parse non-standard CSS. Non-standard properties, values, at-rule. Allow at-rule and rules inside rules. Is it possible ...
Read more >
Parsing in Java: all the tools and libraries you can use
We present and compare all possible alternatives you can use to parse languages in Java. From libraries to parser generators, we present all...
Read more >
What are the downsides of using a simple regex-based ...
The small library does not parse true Markdown, only a small custom version of it created by the same person who created the...
Read more >
Develop Advanced Security Information Model (ASIM) parsers ...
This article explains how to develop, test, and deploy Microsoft Sentinel Advanced Security Information Model (ASIM) parsers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found