2
2
3
3
const fs = require ( "fs" ) ;
4
4
const vm = require ( "vm" ) ;
5
+ const { getAllFiles } = require ( "./test-discovery" ) ;
5
6
const components = require ( "../../components" ) ;
6
7
const languagesCatalog = components . languages ;
7
8
@@ -70,7 +71,7 @@ module.exports = {
70
71
}
71
72
72
73
// load the language itself
73
- const languageSource = this . loadFileSource ( language ) ;
74
+ const languageSource = this . loadComponentSource ( language ) ;
74
75
context . Prism = this . runFileWithContext ( languageSource , { Prism : context . Prism } ) . Prism ;
75
76
context . loadedLanguages . push ( language ) ;
76
77
@@ -85,8 +86,32 @@ module.exports = {
85
86
* @returns {Prism }
86
87
*/
87
88
createEmptyPrism ( ) {
88
- const coreSource = this . loadFileSource ( "core" ) ;
89
+ const coreSource = this . loadComponentSource ( "core" ) ;
89
90
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
+
90
115
return context . Prism ;
91
116
} ,
92
117
@@ -101,14 +126,37 @@ module.exports = {
101
126
102
127
103
128
/**
104
- * Loads the given file source as string
129
+ * Loads the given component's file source as string
105
130
*
106
131
* @private
107
132
* @param {string } name
108
133
* @returns {string }
109
134
*/
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" ) ;
112
160
} ,
113
161
114
162
0 commit comments