22
33const fs = require ( "fs" ) ;
44const vm = require ( "vm" ) ;
5+ const { getAllFiles } = require ( "./test-discovery" ) ;
56const components = require ( "../../components" ) ;
67const languagesCatalog = components . languages ;
78
@@ -70,7 +71,7 @@ module.exports = {
7071 }
7172
7273 // load the language itself
73- const languageSource = this . loadFileSource ( language ) ;
74+ const languageSource = this . loadComponentSource ( language ) ;
7475 context . Prism = this . runFileWithContext ( languageSource , { Prism : context . Prism } ) . Prism ;
7576 context . loadedLanguages . push ( language ) ;
7677
@@ -85,8 +86,32 @@ module.exports = {
8586 * @returns {Prism }
8687 */
8788 createEmptyPrism ( ) {
88- const coreSource = this . loadFileSource ( "core" ) ;
89+ const coreSource = this . loadComponentSource ( "core" ) ;
8990 const context = this . runFileWithContext ( coreSource ) ;
91+
92+ for ( const testSource of this . getChecks ( ) . map ( src => this . loadFileSource ( src ) ) ) {
93+ context . Prism = this . runFileWithContext ( testSource , {
94+ Prism : context . Prism ,
95+ /**
96+ * A pseudo require function for the checks.
97+ *
98+ * This function will behave like the regular `require` in real modules when called form a check file.
99+ *
100+ * @param {string } id The id of relative path to require.
101+ */
102+ require ( id ) {
103+ if ( id . startsWith ( './' ) ) {
104+ // We have to rewrite relative paths starting with './'
105+ return require ( './../checks/' + id . substr ( 2 ) ) ;
106+ } else {
107+ // This might be an id like 'mocha' or 'fs' or a relative path starting with '../'.
108+ // In both cases we don't have to change anything.
109+ return require ( id ) ;
110+ }
111+ }
112+ } ) . Prism ;
113+ }
114+
90115 return context . Prism ;
91116 } ,
92117
@@ -101,14 +126,37 @@ module.exports = {
101126
102127
103128 /**
104- * Loads the given file source as string
129+ * Loads the given component's file source as string
105130 *
106131 * @private
107132 * @param {string } name
108133 * @returns {string }
109134 */
110- loadFileSource ( name ) {
111- return this . fileSourceCache [ name ] = this . fileSourceCache [ name ] || fs . readFileSync ( __dirname + "/../../components/prism-" + name + ".js" , "utf8" ) ;
135+ loadComponentSource ( name ) {
136+ return this . loadFileSource ( __dirname + "/../../components/prism-" + name + ".js" ) ;
137+ } ,
138+
139+ /**
140+ * Loads the given file source as string
141+ *
142+ * @private
143+ * @param {string } src
144+ * @returns {string }
145+ */
146+ loadFileSource ( src ) {
147+ return this . fileSourceCache [ src ] = this . fileSourceCache [ src ] || fs . readFileSync ( src , "utf8" ) ;
148+ } ,
149+
150+
151+ checkCache : null ,
152+
153+ /**
154+ * Returns a list of files which add additional checks to Prism functions.
155+ *
156+ * @returns {ReadonlyArray<string> }
157+ */
158+ getChecks ( ) {
159+ return this . checkCache = this . checkCache || getAllFiles ( __dirname + "/../checks" ) ;
112160 } ,
113161
114162
0 commit comments