-
-
Notifications
You must be signed in to change notification settings - Fork 978
fix(linter): update no-unnecessary-template-expression docs and test case #17453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| const str1 = `Hello world`; | ||
| const text = 'hello'; | ||
| const wrapped = `${text}`; | ||
|
|
||
| export {}; | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,46 +8,57 @@ pub struct NoUnnecessaryTemplateExpression; | |||||
| declare_oxc_lint!( | ||||||
| /// ### What it does | ||||||
| /// | ||||||
| /// This rule disallows unnecessary template literals. | ||||||
| /// Disallows unnecessary template expressions (interpolations) that can be simplified. | ||||||
| /// | ||||||
| /// ### Why is this bad? | ||||||
| /// | ||||||
| /// Template literals should only be used when they are needed for string interpolation or multi-line strings. Using template literals when a simple string would suffice adds unnecessary complexity. | ||||||
| /// Template literals with substitution expressions that are unnecessary add complexity | ||||||
| /// without providing any benefit. Static string literal expressions or expressions that | ||||||
| /// are already strings can be simplified. | ||||||
| /// | ||||||
| /// Note: This rule does not flag template literals without substitution expressions. | ||||||
| /// For example, `` `hello` `` is allowed even though it could be written as `'hello'`. | ||||||
| /// | ||||||
| /// ### Examples | ||||||
| /// | ||||||
| /// Examples of **incorrect** code for this rule: | ||||||
| /// ```ts | ||||||
| /// const str1 = `Hello world`; | ||||||
| /// // Static values can be incorporated into the surrounding template | ||||||
| /// const ab1 = `${'a'}${'b'}`; | ||||||
| /// const ab2 = `a${'b'}`; | ||||||
| /// | ||||||
| /// const str2 = `42`; | ||||||
| /// const stringWithNumber = `${'1 + 1 = '}${2}`; | ||||||
| /// const stringWithBoolean = `${'true is '}${true}`; | ||||||
| /// | ||||||
| /// const str3 = `true`; | ||||||
| /// // Expressions that are already strings can be rewritten without a template | ||||||
| /// const text = 'a'; | ||||||
| /// const wrappedText = `${text}`; | ||||||
| /// | ||||||
| /// // Template with only literal expressions | ||||||
| /// const str4 = `${'Hello'} ${'world'}`; | ||||||
| /// declare const intersectionWithString: string & { _brand: 'test-brand' }; | ||||||
| /// const wrappedIntersection = `${intersectionWithString}`; | ||||||
| /// ``` | ||||||
| /// | ||||||
| /// Examples of **correct** code for this rule: | ||||||
| /// ```ts | ||||||
| /// const str1 = 'Hello world'; | ||||||
| /// // Static values incorporated into the template | ||||||
| /// const ab1 = `ab`; | ||||||
| /// | ||||||
| /// const str2 = '42'; | ||||||
| /// // Template with non-trivial interpolation | ||||||
|
||||||
| /// // Template with non-trivial interpolation | |
| /// // Template with interpolation and static content |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused variable wrapped.