-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from EricSmekens/plugin-system
Plugin system
- Loading branch information
Showing
14 changed files
with
824 additions
and
395 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
charset = utf-8 | ||
indent_style = tab | ||
indent_size = 2 | ||
tab_width = 2 | ||
|
||
[{package.json,}] | ||
indent_style = tab | ||
indent_size = 2 | ||
tab_width = 2 | ||
|
||
[*.json] | ||
indent_style = tab | ||
indent_size = 2 | ||
tab_width = 2 |
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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export default class Hooks { | ||
/** | ||
* @callback HookCallback | ||
* @this {*|Jsep} this | ||
* @param {Jsep} env | ||
* @returns: void | ||
*/ | ||
/** | ||
* Adds the given callback to the list of callbacks for the given hook. | ||
* | ||
* The callback will be invoked when the hook it is registered for is run. | ||
* | ||
* One callback function can be registered to multiple hooks and the same hook multiple times. | ||
* | ||
* @param {string|object} name The name of the hook, or an object of callbacks keyed by name | ||
* @param {HookCallback|boolean} callback The callback function which is given environment variables. | ||
* @param {?boolean} [first=false] Will add the hook to the top of the list (defaults to the bottom) | ||
* @public | ||
*/ | ||
add(name, callback, first) { | ||
if (typeof arguments[0] != 'string') { | ||
// Multiple hook callbacks, keyed by name | ||
for (let name in arguments[0]) { | ||
this.add(name, arguments[0][name], arguments[1]); | ||
} | ||
} | ||
else { | ||
(Array.isArray(name) ? name : [name]).forEach(function (name) { | ||
this[name] = this[name] || []; | ||
|
||
if (callback) { | ||
this[name][first ? 'unshift' : 'push'](callback); | ||
} | ||
}, this); | ||
} | ||
} | ||
|
||
/** | ||
* Runs a hook invoking all registered callbacks with the given environment variables. | ||
* | ||
* Callbacks will be invoked synchronously and in the order in which they were registered. | ||
* | ||
* @param {string} name The name of the hook. | ||
* @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered. | ||
* @public | ||
*/ | ||
run(name, env) { | ||
this[name] = this[name] || []; | ||
this[name].forEach(function (callback) { | ||
callback.call(env && env.context ? env.context : env, env); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Add default plugins: | ||
import jsep from './jsep.js'; | ||
import './plugins/ternary/ternary.js'; | ||
|
||
export * from './jsep.js'; | ||
export default jsep; |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {Jsep} from '../../jsep.js'; | ||
|
||
// Ternary expression: test ? consequent : alternate | ||
Jsep.hooksAdd('after-expression', function gobbleTernary(env) { | ||
if (this.code === Jsep.QUMARK_CODE) { | ||
this.index++; | ||
const test = env.node; | ||
const consequent = this.gobbleExpression(); | ||
|
||
if (!consequent) { | ||
this.throwError('Expected expression'); | ||
} | ||
|
||
this.gobbleSpaces(); | ||
|
||
if (this.code === Jsep.COLON_CODE) { | ||
this.index++; | ||
const alternate = this.gobbleExpression(); | ||
|
||
if (!alternate) { | ||
this.throwError('Expected expression'); | ||
} | ||
env.node = { | ||
type: 'ConditionalExpression', | ||
test, | ||
consequent, | ||
alternate, | ||
}; | ||
} | ||
else { | ||
this.throwError('Expected :'); | ||
} | ||
} | ||
}); |
Oops, something went wrong.