@@ -50,6 +50,84 @@ var ERROR_NAMES = [
5050
5151// MAIN //
5252
53+ /**
54+ * Transforms a file for a production build.
55+ *
56+ * ## Notes
57+ *
58+ * - This code is adapted from the respective stdlib [GitHub Action][1].
59+ *
60+ * [1]: https://github.com/stdlib-js/transform-errors-action
61+ *
62+ * @param {Object } fileInfo - file information
63+ * @param {Object } api - JSCodeshift API
64+ * @returns {Object } transformed file
65+ */
66+ /**
67+ * Tests whether a variable declaration is for the `@stdlib/string-format` require.
68+ *
69+ * @private
70+ * @param {Object } path - AST node path
71+ * @returns {boolean } boolean indicating whether a variable declaration is for the `@stdlib/string-format` require
72+ */
73+ function onStringFormat ( path ) {
74+ var node = path . node ;
75+ return node . init &&
76+ node . init . type === 'CallExpression' &&
77+ node . init . callee . name === 'require' &&
78+ node . init . arguments [ 0 ] . value === '@stdlib/string-format' ;
79+ }
80+ /**
81+ * Returns false if the node index is equal to zero and true otherwise.
82+ *
83+ * @private
84+ * @param {Object } path - AST node path
85+ * @param {number } idx - node index
86+ * @returns {boolean } boolean indicating whether to keep the node
87+ */
88+ function dropFirst ( path , idx ) {
89+ return idx !== 0 ;
90+ }
91+ /**
92+ * Deletes the comments associated with a given node.
93+ *
94+ * @private
95+ * @param {Object } path - AST node path
96+ * @returns {void }
97+ */
98+ function deleteComment ( path ) {
99+ var i ;
100+ if ( path . node . comments ) {
101+ for ( i = 0 ; i < path . node . comments . length ; i ++ ) {
102+ if ( contains ( path . node . comments [ i ] . value , '@license Apache-2.0' ) ) {
103+ path . node . comments [ i ] . value = '* @license Apache-2.0 ' ;
104+ }
105+ }
106+ }
107+ }
108+ /**
109+ * Rewrites a `require` statement to include the `/dist` directory if the module being required starts with `@stdlib`.
110+ *
111+ * @private
112+ * @param {Object } path - AST node path
113+ * @returns {void }
114+ */
115+ function rewriteRequire ( path ) {
116+ if ( startsWith ( path . value . arguments [ 0 ] . value , '@stdlib' ) ) {
117+ path . value . arguments [ 0 ] . value += '/dist' ;
118+ }
119+ }
120+ /**
121+ * Tests whether a path is a require call for `@stdlib/error-tools-fmtprodmsg`.
122+ *
123+ * @private
124+ * @param {Object } path - AST node path
125+ * @returns {boolean } boolean indicating whether a path is a require call for `@stdlib/error-tools-fmtprodmsg`
126+ */
127+ function hasRequire ( path ) {
128+ return path . value . callee . name === 'require' &&
129+ path . value . arguments [ 0 ] . value === '@stdlib/error-tools-fmtprodmsg' ;
130+ }
53131/**
54132* Transforms a file for a production build.
55133*
@@ -121,21 +199,6 @@ function transformer( fileInfo, api ) {
121199
122200 return replace ( out , RE_INDENT , '\n' ) ;
123201
124- /**
125- * Tests whether a variable declaration is for the `@stdlib/string-format` require.
126- *
127- * @private
128- * @param {Object } path - AST node path
129- * @returns {boolean } boolean indicating whether a variable declaration is for the `@stdlib/string-format` require
130- */
131- function onStringFormat ( path ) {
132- var node = path . node ;
133- return node . init &&
134- node . init . type === 'CallExpression' &&
135- node . init . callee . name === 'require' &&
136- node . init . arguments [ 0 ] . value === '@stdlib/string-format' ;
137- }
138-
139202 /**
140203 * Assigns the variable name for the `@stdlib/string-format` require.
141204 *
@@ -147,49 +210,6 @@ function transformer( fileInfo, api ) {
147210 formatVar = path . node . id . name ;
148211 }
149212
150- /**
151- * Returns false if the node index is equal to zero and true otherwise.
152- *
153- * @private
154- * @param {Object } path - AST node path
155- * @param {number } idx - node index
156- * @returns {boolean } boolean indicating whether to keep the node
157- */
158- function dropFirst ( path , idx ) {
159- return idx !== 0 ;
160- }
161-
162- /**
163- * Deletes the comments associated with a given node.
164- *
165- * @private
166- * @param {Object } path - AST node path
167- * @returns {void }
168- */
169- function deleteComment ( path ) {
170- var i ;
171- if ( path . node . comments ) {
172- for ( i = 0 ; i < path . node . comments . length ; i ++ ) {
173- if ( contains ( path . node . comments [ i ] . value , '@license Apache-2.0' ) ) {
174- path . node . comments [ i ] . value = '* @license Apache-2.0 ' ;
175- }
176- }
177- }
178- }
179-
180- /**
181- * Rewrites a `require` statement to include the `/dist` directory if the module being required starts with `@stdlib`.
182- *
183- * @private
184- * @param {Object } path - AST node path
185- * @returns {void }
186- */
187- function rewriteRequire ( path ) {
188- if ( startsWith ( path . value . arguments [ 0 ] . value , '@stdlib' ) ) {
189- path . value . arguments [ 0 ] . value += '/dist' ;
190- }
191- }
192-
193213 /**
194214 * Callback invoked upon finding a string literal.
195215 *
@@ -243,23 +263,12 @@ function transformer( fileInfo, api ) {
243263 ] ) )
244264 ] ) ;
245265 debug ( 'Adding `require` call to `@stdlib/error-tools-fmtprodmsg`...' ) ;
246- j ( root . find ( j . Declaration ) . at ( 0 ) . get ( ) ) . insertBefore ( formatRequire ) ;
266+ j ( root . find ( j . Declaration ) . at ( 0 ) . get ( ) )
267+ . insertBefore ( formatRequire ) ;
247268 }
248269 }
249270 }
250271 }
251-
252- /**
253- * Tests whether a path is a require call for `@stdlib/error-tools-fmtprodmsg`.
254- *
255- * @private
256- * @param {Object } path - AST node path
257- * @returns {boolean } boolean indicating whether a path is a require call for `@stdlib/error-tools-fmtprodmsg`
258- */
259- function hasRequire ( path ) {
260- return path . value . callee . name === 'require' &&
261- path . value . arguments [ 0 ] . value === '@stdlib/error-tools-fmtprodmsg' ;
262- }
263272}
264273
265274
0 commit comments