Skip to content

feat(#107): wrong-sprintf-arguments lint#206

Merged
rultor merged 27 commits intoobjectionary:masterfrom
h1alexbel:107-sprintf
Jan 19, 2025
Merged

feat(#107): wrong-sprintf-arguments lint#206
rultor merged 27 commits intoobjectionary:masterfrom
h1alexbel:107-sprintf

Conversation

@h1alexbel
Copy link
Member

In this pull I've introduced new lint: wrong-sprintf-arguments, that issues warning defect if QQ.txt.sprintf object has
different number of passed arguments than number of its placeholder variables.

closes #107
History:

@h1alexbel
Copy link
Member Author

@maxonfjvipon please check

<xsl:variable name="decimal" select="sum(for $i in 1 to $length return (index-of(string-to-codepoints('0123456789ABCDEF'), string-to-codepoints(substring($hex-upper, $i, 1))) - 1) * xs:integer(math:pow(16, $length - $i)))"/>
<xsl:sequence select="$decimal"/>
</xsl:function>
<xsl:variable name="sprintf" select="//o[@base='.sprintf'][o[@base='.txt']/o[@base='QQ']]"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel QQ is a placeholder for Q.org.eolang, so it'll be //o[@base='.sprintf' and o[@base='.txt']/o[@base='.eolang']/o[@base='.org']/o[@base='Q']] or //o[@base='org.eolang.txt.sprintf']

</xsl:function>
<xsl:variable name="sprintf" select="//o[@base='.sprintf'][o[@base='.txt']/o[@base='QQ']]"/>
<xsl:variable name="placeholder">
<xsl:for-each select="tokenize($sprintf, '-')">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel I don't really understand what you're tokenizing here, $sprintf is the element <o base='.sprintf>...`, or I'm wrong?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxonfjvipon here, $sprintf will be a value from a text() node inside sprintf object:

<o base=".sprintf" line="4" pos="8">
  <o base=".txt" line="4" pos="4">
    <o base="QQ" line="4" pos="2"/>
  </o>
  <o base="string" line="5" pos="4">
    48-65-6C-6C-6F-2C-20-25-73-21-20-59-6F-75-72-20-61-63-63-6F-75-6E-74-20-69-73-20-25-64-2E
  </o>
  <o base="tuple" line="6" pos="4">
    <o base=".empty">
      <o base="tuple"/>
    </o>
    <o base="name" line="6" pos="6"/>
  </o>
</o>

48-65-6C-6C-6F-2C-20-25-73-21-20-59-6F-75-72-20-61-63-63-6F-75-6E-74-20-69-73-20-25-64-2E will be selected as $sprintf here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel what if the tuple (2nd argument of sprintf) would also contain a string or number?

sprintf
  "Hello, %s"
  * "Jeff"

In XMIR it would look like this:

<o base=".sprintf" line="4" pos="8">
  <o base=".txt" line="4" pos="4">
    <o base="QQ" line="4" pos="2"/>
  </o>
  <o base="string" line="5" pos="4">
    48-65-6C-6C-6F-2C-20-25-73-21-20-59-6F-75-72-20-61-63-63-6F-75-6E-74-20-69-73-20-25-64-2E
  </o>
  <o base="tuple" line="6" pos="4">
    <o base=".empty">
      <o base="tuple"/>
    </o>
    <o base="string" line="5" pos="4">
      48-65-6C-6C-6F
    </o>
  </o>
</o>

What will the result of $sprintf variable in such case?

<xsl:value-of select="codepoints-to-string(eo:hex-to-placeholder(.))"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="declared" select="count(matches($placeholder, '%s')) + count(matches($placeholder, '%d'))"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel sprintf supports more formatters than just %d and %s

@h1alexbel h1alexbel requested a review from maxonfjvipon January 9, 2025 12:35
@h1alexbel
Copy link
Member Author

@maxonfjvipon updated. Take a look, please

@h1alexbel
Copy link
Member Author

@maxonfjvipon reminder

</xsl:function>
<xsl:variable name="sprintf" select="//o[@base='.sprintf'][o[@base='.txt']/o[@base='QQ']]"/>
<xsl:variable name="placeholder">
<xsl:for-each select="tokenize($sprintf, '-')">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel what if the tuple (2nd argument of sprintf) would also contain a string or number?

sprintf
  "Hello, %s"
  * "Jeff"

In XMIR it would look like this:

<o base=".sprintf" line="4" pos="8">
  <o base=".txt" line="4" pos="4">
    <o base="QQ" line="4" pos="2"/>
  </o>
  <o base="string" line="5" pos="4">
    48-65-6C-6C-6F-2C-20-25-73-21-20-59-6F-75-72-20-61-63-63-6F-75-6E-74-20-69-73-20-25-64-2E
  </o>
  <o base="tuple" line="6" pos="4">
    <o base=".empty">
      <o base="tuple"/>
    </o>
    <o base="string" line="5" pos="4">
      48-65-6C-6C-6F
    </o>
  </o>
</o>

What will the result of $sprintf variable in such case?

@h1alexbel
Copy link
Member Author

@maxonfjvipon thanks for the idea above, updated PR, take a look, please

@h1alexbel
Copy link
Member Author

@maxonfjvipon reminder

[] > app
QQ.io.stdout > @
org.eolang.txt.sprintf
"Hello, %s! Your account is %d."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel sprintf requires 2 mandatory argument, so if you want to provide empty tuple - just do add * as the second argument

<xsl:variable name="used" select="count($tupled[not(@base='tuple')]/@base) + count(tokenize(substring($nested, 1, string-length($nested) - 1), '\s+'))"/>
<xsl:template match="/">
<defects>
<xsl:if test="$sprintf-text != '' and $declared != $used">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel what if there are not one sprintf in the file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxonfjvipon good finding

@h1alexbel
Copy link
Member Author

@maxonfjvipon updated. Take a look, please

@h1alexbel
Copy link
Member Author

@maxonfjvipon reminder

1 similar comment
@h1alexbel
Copy link
Member Author

@maxonfjvipon reminder

# App.
[] > app
QQ.io.stdout > @
not.sprintf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel something went wrong here

# App.
[] > app
QQ.io.stdout > @
not.sprintf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel and here

input: |
# App.
[] > app
QQ.io.stdout > @
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel something's wrong with indentation here

input: |
# App.
[] > app
QQ.io.stdout > @
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel something's wrong with indentation here

input: |
# App.
[] > app
QQ.io.stdout > @
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel something's wrong with indentation here

input: |
# App.
[] > app
QQ.io.stdout > @
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel something's wrong with indentation here

input: |
# App.
[] > app
QQ.io.stdout > @
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel something's wrong with indentation here

input: |
# App.
[] > app
QQ.io.stdout > @
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel something's wrong with indentation here

@h1alexbel
Copy link
Member Author

@maxonfjvipon fixed indentation, please check again

@h1alexbel
Copy link
Member Author

@yegor256 take a look, please

if (rule instanceof SpellingCheckRule) {
((SpellingCheckRule) rule).addIgnoreTokens(
Arrays.asList("decoratee", "eolang")
Arrays.asList("decoratee", "eolang", "sprintf")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@h1alexbel better enquote the string inside the error message, to make it look like this:

According to the formatting template of the "sprintf" object, a tuple of 3 elements is expected as the second argument of it, while a tuple of 2 elements is provided

@yegor256
Copy link
Member

yegor256 commented Jan 19, 2025

@h1alexbel excellent contribution, thanks! Could you please try this test too (must issue no warning):

QQ.txt.sprintf
  "Water constitutes 0.023%% of Earth mass, which is %d tons"
  * 1.4e18

@h1alexbel
Copy link
Member Author

@yegor256 updated. Take a look, please

@h1alexbel h1alexbel requested a review from yegor256 January 19, 2025 08:24
@yegor256
Copy link
Member

@rultor merge

@rultor
Copy link
Contributor

rultor commented Jan 19, 2025

@rultor merge

@yegor256 OK, I'll try to merge now. You can check the progress of the merge here.

@rultor rultor merged commit bed442a into objectionary:master Jan 19, 2025
14 checks passed
@rultor
Copy link
Contributor

rultor commented Jan 19, 2025

@rultor merge

@yegor256 Done! FYI, the full log is here (took me 9min).

@h1alexbel h1alexbel deleted the 107-sprintf branch January 19, 2025 09:41
@yegor256
Copy link
Member

@h1alexbel thanks!

@0crat
Copy link

0crat commented Jan 19, 2025

@maxonfjvipon Thank you for your review! We appreciate your contribution. Based on our team's policy, you've earned +11 points: +4 as the base reward, and +7 for the 26 comments you've made (capped at 8 points for comments). This aligns with our goal of encouraging thorough code reviews. Your current balance stands at +44. Keep up the great work in providing detailed feedback to your colleagues!

@0crat
Copy link

0crat commented Jan 19, 2025

@h1alexbel Hey there, great job on your contribution! 👍 You've earned +4 points, which is our base reward. We had to deduct 8 points because your code was a bit hefty (487 hits-of-code is over our 200 limit), and 6 points due to the high number of review comments (31). But don't worry, we added 14 points to keep you motivated! Your total balance is now +57. Remember, quality over quantity is key. Keep those contributions coming, and maybe aim for shorter, more focused code next time!

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.

wrong number of arguments for sprintf

5 participants