-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Use methods for getting (and proxy traps for setting) variables on…
… the `Variables` class
- Loading branch information
Showing
5 changed files
with
89 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -932,7 +932,7 @@ Main.setMethod(function parseTemplateSyntax(source, name, wrap_ejs) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 1.0.0 | ||
* @version 2.3.18 | ||
* @version 2.4.0 | ||
* | ||
* @param {string} code | ||
* @param {Hawkejs.Scopes} scopes | ||
|
@@ -1018,8 +1018,21 @@ Main.setMethod(function rewriteVariableReferences(code, scopes, level) { | |
if (Hawkejs.hasGlobal(token.value)) { | ||
token.value = '(Hawkejs.getGlobal(' + JSON.stringify(token.value) + '))'; | ||
} else { | ||
// All the rest are variables that should be changed | ||
token.value = 'vars.' + token.value; | ||
|
||
let is_assignment = false, | ||
name = next?.name; | ||
|
||
if (name) { | ||
if (name == 'increment' || name == 'decrement' || name.indexOf('assign') === 0) { | ||
is_assignment = true; | ||
} | ||
} | ||
|
||
if (is_assignment) { | ||
token.value = 'vars.getProxy().' + token.value; | ||
} else { | ||
token.value = 'vars.get(' + JSON.stringify(token.value) + ')'; | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,28 @@ | ||
const PARENT = Symbol('parent'), | ||
RENDERER = Symbol('renderer'); | ||
RENDERER = Symbol('renderer'), | ||
TRAPLESS = Symbol('trapless'), | ||
PROXY = Symbol('proxy'); | ||
|
||
/** | ||
* This way we can skip transforming ejs `my_value = 1` | ||
* into complicated calls. | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 2.4.0 | ||
* @version 2.4.0 | ||
*/ | ||
const TRAPS = { | ||
set(target, name, value) { | ||
|
||
if (typeof name == 'symbol') { | ||
target[name] = value; | ||
} else { | ||
target.setFromTemplate(name, value); | ||
} | ||
|
||
return true; | ||
} | ||
}; | ||
|
||
/** | ||
* Simple class to use for storing variables | ||
|
@@ -13,6 +36,7 @@ const Variables = Fn.inherits('Hawkejs.Base', function Variables(renderer, varia | |
this[renderer.clone_symbol] = null; | ||
this[PARENT] = null; | ||
this[RENDERER] = renderer; | ||
this[TRAPLESS] = this; | ||
|
||
if (variables != null) { | ||
Object.assign(this, variables); | ||
|
@@ -60,6 +84,24 @@ Variables.setProperty(function length() { | |
return Object.keys(this).length; | ||
}); | ||
|
||
/** | ||
* Get the proxy of this instance. | ||
* This allows us to set variables without having to use the `set` method. | ||
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 2.4.0 | ||
* @version 2.4.0 | ||
*/ | ||
Variables.setMethod(function getProxy() { | ||
|
||
if (this[PROXY] == null) { | ||
this[PROXY] = new Proxy(this, TRAPS); | ||
this[PROXY][TRAPLESS] = this; | ||
} | ||
|
||
return this[PROXY]; | ||
}); | ||
|
||
/** | ||
* Get a specific variable | ||
* | ||
|
@@ -137,11 +179,12 @@ Variables.setMethod(function getExistingCloneIfValid(clone_name) { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 2.0.0 | ||
* @version 2.0.0 | ||
* @version 2.4.0 | ||
*/ | ||
Variables.setMethod(function createShim() { | ||
|
||
var result = Object.create(this); | ||
let result = Object.create(this[TRAPLESS]); | ||
result[PROXY] = null; | ||
|
||
return result; | ||
}); | ||
|
@@ -226,7 +269,7 @@ Variables.setMethod(function toJSON() { | |
* | ||
* @author Jelle De Loecker <[email protected]> | ||
* @since 2.0.0 | ||
* @version 2.0.0 | ||
* @version 2.4.0 | ||
*/ | ||
Variables.setMethod(function overlay(new_variables) { | ||
|
||
|
@@ -242,7 +285,7 @@ Variables.setMethod(function overlay(new_variables) { | |
|
||
// This will set this instance as the new parent of the variables instance | ||
// (This can overwrite the parent in the cloned instance) | ||
result[PARENT] = this; | ||
result[PARENT] = this[TRAPLESS]; | ||
|
||
return result; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters