Skip to content
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

[bug] Literal strings are matched generically #1057

Closed
bruceg opened this issue Apr 16, 2024 · 5 comments
Closed

[bug] Literal strings are matched generically #1057

bruceg opened this issue Apr 16, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@bruceg
Copy link

bruceg commented Apr 16, 2024

⏯ Playground Link

https://ast-grep.github.io/playground.html#eyJtb2RlIjoiUGF0Y2giLCJsYW5nIjoicnVzdCIsInF1ZXJ5IjoiJEUud2l0aF9tZWFuaW5nKCRELCBcIm1lYW5pbmdcIikiLCJyZXdyaXRlIjoiJEUud2l0aF9tZWFuaW5nKCRELCBtZWFuaW5nOjpNRVNTQUdFKSIsImNvbmZpZyI6IiMgWUFNTCBSdWxlIGlzIG1vcmUgcG93ZXJmdWwhXG4jIGh0dHBzOi8vYXN0LWdyZXAuZ2l0aHViLmlvL2d1aWRlL3J1bGUtY29uZmlnLmh0bWwjcnVsZVxucnVsZTpcbiAgYW55OlxuICAgIC0gcGF0dGVybjogY29uc29sZS5sb2coJEEpXG4gICAgLSBwYXR0ZXJuOiBjb25zb2xlLmRlYnVnKCRBKVxuZml4OlxuICBsb2dnZXIubG9nKCRBKSIsInNvdXJjZSI6ImZuIHRlc3QoKSB7XG4gICAgc2NoZW1hLndpdGhfbWVhbmluZyhGT08sIFwibWVhbmluZ1wiKTtcbiAgICBzY2hlbWEud2l0aF9tZWFuaW5nKEZPTywgXCJzZXJ2aWNlXCIpO1xufSJ9

💻 Code

fn test() {
    schema.with_meaning(FOO, "meaning");
    schema.with_meaning(FOO, "service");
}

Pattern:

$E.with_meaning($D, "meaning")

🙁 Actual behavior

The pattern matches both of the calls in the above code snippet, with "meaning" matching "service" in the second.

🙂 Expected behavior

I expected that it would only match the first one, since the constant literal string is different from the pattern. My use case is to replace a literal string with a predefined constant, but the pattern is matching the wrong strings to be useful.

@bruceg bruceg added the bug Something isn't working label Apr 16, 2024
@HerringtonDarkholme
Copy link
Member

HerringtonDarkholme commented Apr 16, 2024

tree-sitter does not report string literal's content.

image

So both "foo" and "bar" has the same structure like

string_literal
  " opening quote
  " closing quote

@HerringtonDarkholme
Copy link
Member

HerringtonDarkholme commented Apr 16, 2024

This is fixed in tree-sitter/tree-sitter-rust#218

But it is blocked by https://github.com/tree-sitter/tree-sitter-typescript/blob/master/Cargo.toml#L29

More context: tree-sitter/tree-sitter-typescript#289

@bruceg
Copy link
Author

bruceg commented Apr 16, 2024

Thanks for the explanation, @HerringtonDarkholme

@HerringtonDarkholme
Copy link
Member

HerringtonDarkholme commented May 2, 2024

I have created a new issue to track the fix #1104 . Closing this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants