Skip to content

Fix no-raw-text crash on template literals without interpolations - #342

Open
theRizwan wants to merge 1 commit into
Intellicode:masterfrom
theRizwan:fix/no-raw-text-template-literal-crash
Open

Fix no-raw-text crash on template literals without interpolations#342
theRizwan wants to merge 1 commit into
Intellicode:masterfrom
theRizwan:fix/no-raw-text-template-literal-crash

Conversation

@theRizwan

Copy link
Copy Markdown

Fixes #341.

report in no-raw-text read node.expressions[0].name unconditionally. A template literal with no
interpolations has an empty expressions array, so this threw and aborted the whole lint run
rather than reporting a lint error:

<View>{`hello`}</View>
TypeError: Cannot read properties of undefined (reading 'name')
Occurred while linting <input>:1
Rule: "react-native/no-raw-text"

A second, milder case: when the first expression is not an Identifier it has no .name, so
<View>{${this.props.text}}</View> reported Raw text (TemplateLiteral: undefined).

The change

Both cases now go through one helper:

Input Before After
{hello} throws Raw text (hello)
{${text}} Raw text (TemplateLiteral: text) unchanged
{${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. The Identifier case is deliberately left alone so existing
messages and tests still hold.

context.getSourceCode() is used rather than context.sourceCode, since peerDependencies starts at
eslint@^3.17.0 and the modern accessor only exists from 8.40.

Tests

Two cases added to tests/lib/rules/no-raw-text.js, both of which fail on master:

  • reverting only the lib/ change leaves 4 failing (RuleTester exercises each in both parser modes) — reproducing the TypeError and the undefined message
  • with the fix, npm test is green: lint plus 133 passing

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

`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
@theRizwan theRizwan changed the title fix(no-raw-text): handle template literals without interpolations Fix no-raw-text crash on template literals without interpolations Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no-raw-text: TypeError on template literals with no interpolations aborts the lint run

1 participant