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

Don't monkey-patch Blocky.utils.xml.document in node module #5461

Merged
merged 1 commit into from
Sep 14, 2021

Conversation

cpcallen
Copy link
Contributor

@cpcallen cpcallen commented Sep 14, 2021

The basics

  • I branched from goog_module
  • My pull request is against goog_module
  • My code follows the style guide
  • I have run npm test.

The details

Resolves

Part of #5026

Proposed Changes

  • Rename the Blockly.utils.xml.document export to getDocument (this function already had that as its internal name), and have it return a new module-global variable xmlDocument.
  • Introduce a .setDocument export that sets xmlDocument.
  • Call .setDocument instead of monkey-patching .getDocument in scripts/package/node/core.js (which becomes part of the blockly package).
  • Also clean up the code around this location a little.

Reason for Changes

Monkey-patching (i.e., modifying exported properties from another module) is contrary to the style guide and is generally to be avoided wherever possible.

Test Coverage

Ran npm test.

Documentation

An entry has been added to scripts/migration/renamings.js for document -> getDocument.

Use Blockly.utils.xml.setDocument instead.
@cpcallen cpcallen added this to the 2021_q3_release milestone Sep 14, 2021
@cpcallen cpcallen requested a review from a team as a code owner September 14, 2021 17:30
@google-cla google-cla bot added the cla: yes Used by Google's CLA checker. label Sep 14, 2021
@cpcallen cpcallen merged commit d202ae0 into google:goog_module Sep 14, 2021
@cpcallen cpcallen deleted the no-monkey-patch-xml branch September 14, 2021 18:55
cpcallen added a commit to cpcallen/blockly that referenced this pull request Jan 12, 2023
Introduce a (hopefully generally applicable) mechanism for
injecting dependencies into modules, specifically in this case
to inject required bits of JSDOM's Window and Document
implementations into core/utils/xml.js when running in
node.js or other environments lacking a DOM.

The injectDependencies function uses an options object to
facilitate optionally injecting multiple named dependencies
at the same time.

Rename the xmlDocument local variable back to document (was
renamed in google#5461) so that the name used in this module
corresponds to the usual global variable it replaces.

Change the injection in scripts/package/node/core.js to use
injectDependencies instead of setXmlDocument + global variables;
also eliminate apparently-unnecessary creation of a special
Document instance, using the default one supplied by jsdom
instead.

Fixes google#6725.
cpcallen added a commit to cpcallen/blockly that referenced this pull request Jan 12, 2023
Introduce a (hopefully generally applicable) mechanism for
injecting dependencies into modules, specifically in this case
to inject required bits of JSDOM's Window and Document
implementations into core/utils/xml.js when running in
node.js or other environments lacking a DOM.

The injectDependencies function uses an options object to
facilitate optionally injecting multiple named dependencies
at the same time.

Rename the xmlDocument local variable back to document (was
renamed in google#5461) so that the name used in this module
corresponds to the usual global variable it replaces.

Change the injection in scripts/package/node/core.js to use
injectDependencies instead of setXmlDocument + global variables;
also eliminate apparently-unnecessary creation of a special
Document instance, using the default one supplied by jsdom
instead.

Fixes google#6725.
BeksOmega pushed a commit that referenced this pull request Jan 12, 2023
…/package/node/core.js` and `core/utils/xml.ts` (#6764)

* fix(node): Don't use global variables for jsdom injection

Introduce a (hopefully generally applicable) mechanism for
injecting dependencies into modules, specifically in this case
to inject required bits of JSDOM's Window and Document
implementations into core/utils/xml.js when running in
node.js or other environments lacking a DOM.

The injectDependencies function uses an options object to
facilitate optionally injecting multiple named dependencies
at the same time.

Rename the xmlDocument local variable back to document (was
renamed in #5461) so that the name used in this module
corresponds to the usual global variable it replaces.

Change the injection in scripts/package/node/core.js to use
injectDependencies instead of setXmlDocument + global variables;
also eliminate apparently-unnecessary creation of a special
Document instance, using the default one supplied by jsdom
instead.

Fixes #6725.

* deprecate(xml): Deprecate getXmlDocument and setXmlDocument

Mark getXmlDocument and setXmlDocument as @deprecated, with
suitable calls to deprecation.warn().

There are no remaining callers to either function within core -
setXmlDocument was only used by the node.js wrapper, and and
apparently getXmlDocument was never used AFAICT - and we do not
anticipate that either were used by external developers.

* fix: Corrections for comments on PR #6764.
gonfunko pushed a commit that referenced this pull request Jan 19, 2023
…/package/node/core.js` and `core/utils/xml.ts` (#6764)

* fix(node): Don't use global variables for jsdom injection

Introduce a (hopefully generally applicable) mechanism for
injecting dependencies into modules, specifically in this case
to inject required bits of JSDOM's Window and Document
implementations into core/utils/xml.js when running in
node.js or other environments lacking a DOM.

The injectDependencies function uses an options object to
facilitate optionally injecting multiple named dependencies
at the same time.

Rename the xmlDocument local variable back to document (was
renamed in #5461) so that the name used in this module
corresponds to the usual global variable it replaces.

Change the injection in scripts/package/node/core.js to use
injectDependencies instead of setXmlDocument + global variables;
also eliminate apparently-unnecessary creation of a special
Document instance, using the default one supplied by jsdom
instead.

Fixes #6725.

* deprecate(xml): Deprecate getXmlDocument and setXmlDocument

Mark getXmlDocument and setXmlDocument as @deprecated, with
suitable calls to deprecation.warn().

There are no remaining callers to either function within core -
setXmlDocument was only used by the node.js wrapper, and and
apparently getXmlDocument was never used AFAICT - and we do not
anticipate that either were used by external developers.

* fix: Corrections for comments on PR #6764.

(cherry picked from commit cd57e74)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Used by Google's CLA checker. type: cleanup
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants