-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
CodeGenerator.prototype.statementToCode
and valueToCode
should throw when given an invalid input name
#7665
Comments
Rachel & I agree with Neil that the getters can return null, but the Let's try to do this in v11. #7446 |
CodeGenerator.prototype.statementToCode
should throw when given an invalid input nameCodeGenerator.prototype.statementToCode
and valueToCode
should throw when given an invalid input name
To Fix
|
We're actually relying on In the procedure def return generator, we generate code for the |
I think we can just fix ours at the same time, and still include this as a breaking change. Does that sound reasonable? |
Yep SGTM! Just wanted to check since sometimes we don't like to change things if they're in block definitions, and I didn't know if the same thing applied to block generators (sorry should have clarified why I was retriaging in the original post). |
Previously generators could generate code from inputs that didn't exist and get back the empty string. This silent failure was causing problems for diagnosing issues. This PR changes the behaviour so that an error is thrown. This will break generators which rely on the previous behaviour. Several of our demo blocks needed editing to accomodate this change. Resolves #7665
Check for duplicates
Problem
It is a common pattern for the code generators for blocks with statement inputs to look like:
but if the given input name (in this case
'STATEMENT_INPUT'
does not correspond to one of the inputs on the block this fails silently, withcode
being set to''
. This is probably undesirable.Request
CodeGenerator.prototype.statementToCode(block, name)
should throw an error ifblock
does not have an input namedname
.Alternatives considered
As far as alternatives to this proposal: the obvious alternative is the status quo, which can leave developers confused about why their code is not working (see a recent example in the forum).
There are a few alternatives about how this might be achieved. In particular, any of the following functions might be modified to detect this error and throw:
Block.prototype.getInput(name)
could throw if no input namedname
is found (currently returnsnull
and is so documented).Block.prototype.getInputTargetBlock(name)
could throw if no input namedname
is found (currently returnsnull
and is so documented).CodeGeneartor.prototype.blockToCode(block)
could throw ifblock
is null (currently returns''
and is so documented).CodeGenerator.prototype.statementToCode(block, name)
could could throw ifblock.getTargetBlock(name)
returns null (currently it carries on and callsthis.blockToCode(null)
, but no specific behaviour is documented).I (@cpcallen) discussed this with @NeilFraser, who observed:
My view is that
blockToCode
should throw when passednull
(or any other non-Block
value), but evidently the current behaviour is documented and relied upon: a trial implementation breaks 35 of the mocha tests currently. Having onlystatementToCode
throw is less helpful at catching developer bugs and would still be a breaking change, but it my be preferable as it is less disruptive, does not change documented behaviour, and only causes seven mocha test failure.Additional context
No response
The text was updated successfully, but these errors were encountered: