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.

check `node.raw` before checking because it's not standard and may break custom parser

See original GitHub issue

What rule do you want to change?

https://github.com/eslint/eslint/search?q=node.raw&type=

Does this change cause the rule to produce more or fewer warnings?

Fewer error for custom parser, raw of Literal is not standard, see https://github.com/estree/estree/issues/14

How will the change be implemented? (New option, new default behavior, etc.)?

check node.raw, return if not presented

Please provide some example code that this change will affect:

Context: https://github.com/mdx-js/eslint-mdx/pull/284#issuecomment-800399534

What does the rule currently do for this code?

break if the custom parser does not have node.raw

What will the rule do after it’s changed?

no error

Are you willing to submit a pull request to implement this change?

Yes

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
wooormcommented, Mar 16, 2021

Fewer error for custom parser, raw of Literal is not standard, see estree/estree#14

Specifically, fewer crashes.


My use case for this is that certain languages can compile to JavaScript. Such as markdown/MDX, and others. But those language have different and noncompatible representations of literals.

For example, in markdown, an alt attribute can be represented with ![the alt attribute](). A custom parser could represent that attribute as a JSX attribute in estree. But in those cases, setting a raw field on the literal is nonsensical: there are no quotes, escapes don‘t work, character references (&) could exist, etc.

A different solution for custom parsers would be to try and create a sensical .raw attribute. The problem with this approach though is that it now needs to know what the preferred quote style is.

So, I think a better solution would be for rules that can only work on .raw to exit gracefully if that field does not exist.

0reactions
mdjermanoviccommented, Mar 17, 2021

If the rules would use another way to get the source code, they’ll still assume that if node.type === “Literal” && typeof node.value === “string” then node is a JavaScript string literal.

Tokens? What other ways are there?

Tokens, or slicing text from the source code by node’s .range.

.raw should represent the source code as-is.

I agree, but in my use case this conflicts with the other requirement of .raw, namely that it must exist. There are languages that can be represented as an estree, but setting .raw fields would trip ESLint rules up.

Rules that need .raw are mostly rules with a purpose to check how the code was exactly written in JS and enforce some style or best practices about it. Those rules are thus not applicable to languages with a different syntax for literals.

If we treat an absence of .raw as a way to tell that it isn’t really a JS literal, that would add maintenance to rules that were designed for JS. It could also hide bugs in rules and other parsers.

ESLint can lint the JS parts fine. .raw is set there. Turning ESLint rules off means that it doesn’t lint anything.

You’ll produce a single AST with JS and non-JS parts, where some Literal nodes have .raw but some don’t?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest tests failed after upgrading axios to v1.1.2 · Issue #5101
The following fix works for now but I expect a standard fix for this issue. "test": "react-scripts test --transformIgnorePatterns \"node_modules ...
Read more >
Parsing huge logfiles in Node.js - read in line-by-line
The following solution can parse very large files, line by line using stream & pipe. For testing I used a 2.1 gb file...
Read more >
Process | Node.js v19.3.0 Documentation
The 'beforeExit' event is emitted when Node.js empties its event loop and has no additional work to schedule. Normally, the Node.js process will...
Read more >
Why the Hell Would I Use Node.js? A Case-by-case Tutorial
Node.js can solve I/O scaling, but was not created to compute scaling problems. Learn why and when to use Node.js in this case-by-case...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser ... Tests can appear before, after, or interspersed with...
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