Skip to content

Commit

Permalink
Merge pull request #2299 from opral/improve-m-function-matcher
Browse files Browse the repository at this point in the history
Improve m function matcher & test coverage
  • Loading branch information
felixhaeberle authored Feb 27, 2024
2 parents 1fba64c + 92f7c95 commit 24d0b23
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-windows-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inlang/plugin-m-function-matcher": minor
---

improve matcher to include function referencing & improve test coverage
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,49 @@ describe("Paraglide Message Parser", () => {
])
})

it("should parse function calls without parentheses", () => {
const sourceCode = `import * as m from 'module';
const a = {
b: m.helloWorld,
c: m.helloWorld(),
d: m.helloWorld().someFunction(),
e: m.this_is_a_message123,
}
`
const result = parse(sourceCode)
expect(result).toEqual([
{
messageId: "helloWorld",
position: {
start: { line: 4, character: 9 },
end: { line: 4, character: 19 },
},
},
{
messageId: "helloWorld",
position: {
start: { line: 5, character: 9 },
end: { line: 5, character: 21 },
},
},
{
messageId: "helloWorld",
position: {
start: { line: 6, character: 9 },
end: { line: 6, character: 21 },
},
},
{
messageId: "this_is_a_message123",
position: {
start: { line: 7, character: 9 },
end: { line: 7, character: 29 },
},
},
])
})

it("should match if m is defined before the reference to paraglide", () => {
const sourceCode = `
m.helloWorld();
Expand Down Expand Up @@ -340,4 +383,11 @@ describe("Paraglide Message Parser", () => {
const result = parse(sourceCode)
expect(result).toEqual([])
})

it("should return an empty array when parsing fails", () => {
const sourceCode = undefined

const result = parse(sourceCode as unknown as string)
expect(result).toEqual([])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const createParser = () => {
findMessage: () => {
return Parsimmon.seqMap(
Parsimmon.regex(/.*?(?<![a-zA-Z0-9/])m\./s), // no preceding letters or numbers
Parsimmon.index, // start position of the message id
Parsimmon.regex(/[^(]*/), // message id
Parsimmon.index, // end position of the message id
Parsimmon.regex(/\((?:[^()]|\([^()]*\))*\)/), // function arguments
Parsimmon.index, // Capture start position
Parsimmon.regex(/\w+/), // Match the function name
Parsimmon.index, // Capture end position of function name
Parsimmon.regex(/\((?:[^()]|\([^()]*\))*\)/).or(Parsimmon.succeed("")), // function arguments or empty string
(_, start, messageId, end, args) => {
return {
messageId: `${messageId}`,
Expand Down

0 comments on commit 24d0b23

Please sign in to comment.