Skip to content

Commit d6e781b

Browse files
author
Robert Jackson
authored
Merge pull request #136 from Turbo87/cleanup
Cleanup file structure
2 parents 4dc0bc4 + ed7d555 commit d6e781b

4 files changed

Lines changed: 81 additions & 110 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"jest": {
2828
"collectCoverageFrom": [
29-
"transforms/angle-brackets/transforms/**/*.{js,jsx}"
29+
"transforms/angle-brackets/transform.js"
3030
]
3131
},
3232
"dependencies": {

transforms/-preset.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

transforms/angle-brackets/index.js

100644100755
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
1-
'use strict';
1+
const { getOptions: getCLIOptions } = require('codemod-cli');
2+
const path = require('path');
3+
const fs = require('fs');
24

3-
const preset = require('../-preset');
5+
const transform = require('./transform');
46

5-
module.exports = preset('angle-brackets');
7+
function getOptions() {
8+
let options = {};
9+
10+
let cliOptions = getCLIOptions();
11+
if (cliOptions.config) {
12+
let filePath = path.join(process.cwd(), cliOptions.config);
13+
let config = JSON.parse(fs.readFileSync(filePath));
14+
15+
if (config.helpers) {
16+
options.helpers = config.helpers;
17+
}
18+
19+
if (config.skipFilesThatMatchRegex) {
20+
options.skipFilesThatMatchRegex = new RegExp(config.skipFilesThatMatchRegex);
21+
}
22+
23+
options.skipBuiltInComponents = !!config.skipBuiltInComponents;
24+
}
25+
26+
return options;
27+
}
28+
29+
module.exports = function(file) {
30+
try {
31+
return transform(file, getOptions());
32+
} catch (e) {
33+
throw new Error(
34+
`Transformation errored on file ${file.path}. Reason ${e}. Please report this in https://github.com/ember-codemods/ember-angle-brackets-codemod/issues\n\nStack trace:\n${e.stack}`
35+
);
36+
}
37+
};

transforms/angle-brackets/transforms/angle-brackets-syntax.js renamed to transforms/angle-brackets/transform.js

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,8 @@
11
const glimmer = require('@glimmer/syntax');
22
const prettier = require('prettier');
3-
const path = require('path');
4-
const fs = require('fs');
53

64
const _EMPTY_STRING_ = `ANGLE_BRACKET_EMPTY_${Date.now()}`;
75

8-
class Config {
9-
constructor(options) {
10-
this.helpers = [];
11-
this.skipBuiltInComponents = false;
12-
13-
if (options.config) {
14-
let filePath = path.join(process.cwd(), options.config);
15-
let config = JSON.parse(fs.readFileSync(filePath));
16-
17-
if (config.helpers) {
18-
this.helpers = config.helpers;
19-
}
20-
21-
if (config.skipFilesThatMatchRegex) {
22-
this.skipFilesThatMatchRegex = new RegExp(config.skipFilesThatMatchRegex);
23-
}
24-
25-
this.skipBuiltInComponents = !!config.skipBuiltInComponents;
26-
}
27-
}
28-
}
29-
306
/**
317
* List of HTML attributes for which @ should not be appended
328
*/
@@ -148,28 +124,28 @@ const IGNORE_MUSTACHE_STATEMENTS = [
148124
'app-version',
149125
];
150126

151-
const isAttribute = key => {
127+
function isAttribute(key) {
152128
return HTML_ATTRIBUTES.includes(key) || key.startsWith('data-');
153-
};
129+
}
154130

155-
const isNestedComponentTagName = tagName => {
131+
function isNestedComponentTagName(tagName) {
156132
return tagName && tagName.includes && (tagName.includes('/') || tagName.includes('-'));
157-
};
133+
}
158134

159135
/**
160136
* Returns a capitalized tagname for angle brackets syntax
161137
* {{my-component}} => MyComponent
162138
*/
163-
const capitalizedTagName = tagname => {
139+
function capitalizedTagName(tagname) {
164140
return tagname
165141
.split('-')
166142
.map(s => {
167143
return s[0].toUpperCase() + s.slice(1);
168144
})
169145
.join('');
170-
};
146+
}
171147

172-
const transformTagName = tagName => {
148+
function transformTagName(tagName) {
173149
if (tagName.includes('.')) {
174150
return tagName;
175151
}
@@ -179,14 +155,14 @@ const transformTagName = tagName => {
179155
}
180156

181157
return capitalizedTagName(tagName);
182-
};
158+
}
183159

184-
const transformNestedTagName = tagName => {
160+
function transformNestedTagName(tagName) {
185161
const paths = tagName.split('/');
186162
return paths.map(name => capitalizedTagName(name)).join('::');
187-
};
163+
}
188164

189-
const transformNestedSubExpression = subExpression => {
165+
function transformNestedSubExpression(subExpression) {
190166
let positionalArgs = subExpression.params.map(param => {
191167
if (param.type === 'SubExpression') {
192168
return transformNestedSubExpression(param);
@@ -214,9 +190,9 @@ const transformNestedSubExpression = subExpression => {
214190

215191
let args = positionalArgs.concat(namedArgs);
216192
return `(${subExpression.path.original} ${args.join(' ')})`;
217-
};
193+
}
218194

219-
const shouldSkipFile = (fileInfo, config) => {
195+
function shouldSkipFile(fileInfo, config) {
220196
let source = fileInfo.source;
221197

222198
if (source.includes('~')) {
@@ -235,18 +211,14 @@ const shouldSkipFile = (fileInfo, config) => {
235211
}
236212

237213
return false;
238-
};
214+
}
239215

240-
/**
241-
* exports
242-
*
243-
* @param fileInfo
244-
* @param api
245-
* @param options
246-
* @returns {undefined}
247-
*/
248-
module.exports = function(fileInfo, api, options) {
249-
const config = new Config(options);
216+
module.exports = function transform(fileInfo, config) {
217+
config = config || {};
218+
config.helpers = config.helpers || [];
219+
config.skipBuiltInComponents =
220+
'skipBuiltInComponents' in config ? config.skipBuiltInComponents : false;
221+
config.skipFilesThatMatchRegex = config.skipFilesThatMatchRegex || null;
250222

251223
if (shouldSkipFile(fileInfo, config)) {
252224
return fileInfo.source;
@@ -256,12 +228,13 @@ module.exports = function(fileInfo, api, options) {
256228
mode: 'codemod',
257229
parseOptions: { ignoreStandalone: true },
258230
});
231+
259232
const b = glimmer.builders;
260233

261234
/**
262235
* Transform the attributes names & values properly
263236
*/
264-
const transformAttrs = attrs => {
237+
function transformAttrs(attrs) {
265238
return attrs.map(a => {
266239
let _key = a.key;
267240
let _valueType = a.value.type;
@@ -308,26 +281,26 @@ module.exports = function(fileInfo, api, options) {
308281

309282
return b.attr(_key, _value);
310283
});
311-
};
284+
}
312285

313-
const isQueryParam = param => {
286+
function isQueryParam(param) {
314287
return (
315288
param &&
316289
param.type === 'SubExpression' &&
317290
param.path &&
318291
param.path.original === 'query-params'
319292
);
320-
};
293+
}
321294

322-
const transformLinkToTextParam = textParam => {
295+
function transformLinkToTextParam(textParam) {
323296
if (textParam.type.includes('Literal')) {
324297
return b.text(textParam.value);
325298
} else {
326299
return b.mustache(textParam.original);
327300
}
328-
};
301+
}
329302

330-
const transformModelParams = modelParam => {
303+
function transformModelParams(modelParam) {
331304
let type = modelParam.type;
332305
if (type === 'StringLiteral') {
333306
return b.text(modelParam.value);
@@ -336,9 +309,9 @@ module.exports = function(fileInfo, api, options) {
336309
} else {
337310
return b.mustache(modelParam.original);
338311
}
339-
};
312+
}
340313

341-
const transformLinkToAttrs = params => {
314+
function transformLinkToAttrs(params) {
342315
let attributes = [];
343316
let dataAttributes = getDataAttributesFromParams(params);
344317
params = getNonDataAttributesFromParams(params);
@@ -389,36 +362,36 @@ module.exports = function(fileInfo, api, options) {
389362
}
390363

391364
return attributes.concat(dataAttributes);
392-
};
365+
}
393366

394-
const tranformValuelessDataParams = params => {
367+
function tranformValuelessDataParams(params) {
395368
let dataAttributes = getDataAttributesFromParams(params);
396369
let valuelessDataAttributes = dataAttributes.map(param =>
397370
b.attr(param.parts[0], b.text(_EMPTY_STRING_))
398371
);
399372
return valuelessDataAttributes;
400-
};
373+
}
401374

402-
const transformNodeAttributes = node => {
375+
function transformNodeAttributes(node) {
403376
let params = tranformValuelessDataParams(node.params);
404377
let attributes = transformAttrs(node.hash.pairs);
405378

406379
return params.concat(attributes);
407-
};
380+
}
408381

409-
const getDataAttributesFromParams = params => {
382+
function getDataAttributesFromParams(params) {
410383
return params.filter(param => param.original && `${param.original}`.startsWith('data-'));
411-
};
384+
}
412385

413-
const getNonDataAttributesFromParams = params => {
386+
function getNonDataAttributesFromParams(params) {
414387
return params.filter(p => !(p.original && `${p.original}`.startsWith('data-')));
415-
};
388+
}
416389

417-
const shouldIgnoreMustacheStatement = name => {
390+
function shouldIgnoreMustacheStatement(name) {
418391
return IGNORE_MUSTACHE_STATEMENTS.includes(name) || config.helpers.includes(name);
419-
};
392+
}
420393

421-
const nodeHasPositionalParameters = node => {
394+
function nodeHasPositionalParameters(node) {
422395
if (node.params.length > 0) {
423396
let firstParamType = node.params[0].type;
424397

@@ -432,9 +405,9 @@ module.exports = function(fileInfo, api, options) {
432405
}
433406

434407
return false;
435-
};
408+
}
436409

437-
const transformNode = node => {
410+
function transformNode(node) {
438411
const tagName = node.path.original;
439412

440413
if (config.skipBuiltInComponents && BUILT_IN_COMPONENTS.includes(tagName)) {
@@ -476,7 +449,7 @@ module.exports = function(fileInfo, api, options) {
476449
children,
477450
blockParams,
478451
});
479-
};
452+
}
480453

481454
glimmer.traverse(ast, {
482455
MustacheStatement(node) {

0 commit comments

Comments
 (0)