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

properly deprecate the options hash #1187

Merged
merged 3 commits into from
Jun 23, 2019
Merged

Conversation

jneen
Copy link
Member

@jneen jneen commented Jun 13, 2019

Closes #1186

The warnings were being generated because of the one-step recursion in enum_for passing an empty options hash. Thank you @mojavelinux for spotting this.

As it is now, in the common happy-path case in which no options are passed, we pay only the price of a single nil check. Otherwise, we detect the :continue option, which unconditionally outputs a warning. Empty hashes are only a warning with -w, though this will become an ArgumentError in the next major version. We also properly raise an ArgumentError if invalid options are passed.

end

return enum_for(:lex, string, opts) unless block_given?
Copy link
Member Author

Choose a reason for hiding this comment

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

This was the culprit for the warning.

lib/rouge/lexer.rb Outdated Show resolved Hide resolved
@jneen
Copy link
Member Author

jneen commented Jun 14, 2019

I have updated the branch to not raise an error. However I must unconditionally warn, as it is not appropriate to swallow such errors as we have been. Your users can fix the warning by updating AsciiDoctor. I know this is not ideal for your project, @mojavelinux, but we do have other users and I would like to prevent this situation in the future.

@mojavelinux
Copy link
Contributor

I can compromise on the warning. Since it's only going to happen for the PHP language (as the code in Asciidoctor stands), it's contained enough to be manageable until the new versions of Asciidoctor roll out.

@jneen jneen force-pushed the bugfix.deprecation-warnings branch from f3985b5 to b88ca67 Compare June 14, 2019 00:52
warn 'The use of opts with Lexer.lex is deprecated' if $VERBOSE
def lex(string, opts=nil, &b)
if opts
if (opts.keys - [:continue]).size > 0
Copy link
Contributor

Choose a reason for hiding this comment

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

You can also use Array#empty? here:

Suggested change
if (opts.keys - [:continue]).size > 0
unless (opts.keys - [:continue]).empty?

@ashmaroli
Copy link
Contributor

@jneen Since there's a chance that Lexer#lex can be called multiple times with a truthy opts, using (opts.keys - [:continue]) is going to generate temporary arrays just to be checked if the result is empty.
Instead, the suggested route by @mojavelinux if opts.size == 1 && opts[:continue] has no such side-effects.

@pyrmont pyrmont added the needs-review The PR needs to be reviewed label Jun 14, 2019
@pyrmont
Copy link
Contributor

pyrmont commented Jun 18, 2019

@ashmaroli I don't think it will matter in terms of actual usage given how rare someone calling Lexer#lex with an opts hash should be.

@jneen This looks fine to merge to me. Are you OK with me squashing and merging?

@pyrmont pyrmont added author-action The PR has been reviewed but action by the author is needed and removed needs-review The PR needs to be reviewed labels Jun 18, 2019
@jneen
Copy link
Member Author

jneen commented Jun 23, 2019

Can we merge this? The current released version is very broken. Note:

    def lex(string, opts={}, &b)
      unless opts.nil?
        warn 'The use of opts with Lexer.lex is deprecated' if $VERBOSE
      end

the opts argument is defaulted to {}, so in the happy case it will not be nil, and a warning will be issued.

@mojavelinux
Copy link
Contributor

I'm in support of it.

@jneen
Copy link
Member Author

jneen commented Jun 23, 2019

@ashmaroli while i appreciate your concern, I believe the urgency of warning at the appropriate times is a much greater concern than a possible slight performance degradation for users who have written custom lexers that use lex :continue => true and not the publicly documented delegate API.

@jneen
Copy link
Member Author

jneen commented Jun 23, 2019

(for all other users, it is only a single nil check)

@jneen jneen force-pushed the bugfix.deprecation-warnings branch from 87f8079 to 93191e9 Compare June 23, 2019 02:19
@pyrmont pyrmont merged commit 50f747d into master Jun 23, 2019
@pyrmont pyrmont removed the author-action The PR has been reviewed but action by the author is needed label Jun 23, 2019
@tancnle tancnle deleted the bugfix.deprecation-warnings branch September 22, 2021 12:26
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.

Lexer#lex warns even when no options are passed in
4 participants