Fix no-raw-text crash on template literals without interpolations - #342
Open
theRizwan wants to merge 1 commit into
Open
Fix no-raw-text crash on template literals without interpolations#342theRizwan wants to merge 1 commit into
theRizwan wants to merge 1 commit into
Conversation
`report` read `node.expressions[0].name` unconditionally. A template
literal with no interpolations has an empty `expressions` array, so this
threw `TypeError: Cannot read properties of undefined (reading 'name')`
and aborted the whole lint run instead of reporting a lint error.
<View>{`hello`}</View>
A second case: when the first expression is not an `Identifier` it has no
`.name`, so `<View>{`${this.props.text}`}</View>` reported
`Raw text (TemplateLiteral: undefined)`.
Both now go through `templateLiteralValue`:
- no interpolations -> the literal text, matching how `{'some text'}`
is already reported
- `Identifier` -> unchanged, so existing messages and tests still hold
- anything else -> the expression's source text
Uses `context.getSourceCode()` rather than `context.sourceCode`, since
the peer range starts at eslint 3 and the modern accessor only exists
from 8.40.
Closes Intellicode#341
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #341.
reportinno-raw-textreadnode.expressions[0].nameunconditionally. A template literal with nointerpolations has an empty
expressionsarray, so this threw and aborted the whole lint runrather than reporting a lint error:
A second, milder case: when the first expression is not an
Identifierit has no.name, so<View>{${this.props.text}}</View>reportedRaw text (TemplateLiteral: undefined).The change
Both cases now go through one helper:
{hello}Raw text (hello){${text}}Raw text (TemplateLiteral: text){${this.props.text}}Raw text (TemplateLiteral: undefined)Raw text (TemplateLiteral: this.props.text)The no-interpolation case reports the literal text, which matches how a plain string expression
{'some text'}is already reported. TheIdentifiercase is deliberately left alone so existingmessages and tests still hold.
context.getSourceCode()is used rather thancontext.sourceCode, sincepeerDependenciesstarts ateslint@^3.17.0and the modern accessor only exists from 8.40.Tests
Two cases added to
tests/lib/rules/no-raw-text.js, both of which fail onmaster:lib/change leaves 4 failing (RuleTester exercises each in both parser modes) — reproducing theTypeErrorand theundefinedmessagenpm testis green: lint plus 133 passingHappy to adjust the message wording for the no-interpolation case if you'd prefer something else —
reporting the literal text seemed most consistent with the existing string-expression behaviour, but
it is the one judgement call in here.