|
1 | 1 | const path = require("path"); |
2 | 2 | const camelCase = require("camelcase"); |
3 | 3 | const { |
4 | | - get, |
5 | | - getOptions, |
6 | 4 | capitalizeFirstLetter, |
7 | | - startsWithUpperCaseLetter, |
8 | 5 | DECORATOR_PATHS, |
| 6 | + EMBER_DECORATOR_SPECIFIERS, |
| 7 | + get, |
| 8 | + getOptions, |
| 9 | + getRuntimeData, |
9 | 10 | LAYOUT_IMPORT_SPECIFIER, |
10 | | - METHOD_DECORATORS, |
11 | 11 | META_DECORATORS, |
12 | | - EMBER_DECORATOR_SPECIFIERS |
| 12 | + METHOD_DECORATORS, |
| 13 | + startsWithUpperCaseLetter |
13 | 14 | } = require("./util"); |
14 | | -const { hasValidProps, isFileOfType } = require("./validation-helper"); |
15 | 15 | const { |
16 | | - withComments, |
| 16 | + hasValidProps, |
| 17 | + isFileOfType, |
| 18 | + isTestFile |
| 19 | +} = require("./validation-helper"); |
| 20 | +const { |
17 | 21 | createClass, |
| 22 | + createEmberDecoratorSpecifiers, |
18 | 23 | createImportDeclaration, |
19 | | - createEmberDecoratorSpecifiers |
| 24 | + withComments |
20 | 25 | } = require("./transform-helper"); |
21 | 26 | const EOProp = require("./EOProp"); |
22 | 27 | const logger = require("./log-helper"); |
@@ -131,11 +136,11 @@ function getDecoratorInfo(specifier, importPropDecoratorMap) { |
131 | 136 | const isMethodDecorator = METHOD_DECORATORS.includes(importedName); |
132 | 137 | return { |
133 | 138 | decoratorName, |
134 | | - localName, |
135 | 139 | importedName, |
136 | 140 | isImportedAs, |
137 | 141 | isMetaDecorator, |
138 | | - isMethodDecorator |
| 142 | + isMethodDecorator, |
| 143 | + localName |
139 | 144 | }; |
140 | 145 | } |
141 | 146 |
|
@@ -223,15 +228,15 @@ function getDecoratorImports(j, root) { |
223 | 228 | function getDecoratorsToImport(instanceProps, decoratorsMap = {}) { |
224 | 229 | return instanceProps.reduce((specs, prop) => { |
225 | 230 | return { |
226 | | - attribute: specs.attribute || prop.hasAttributeDecorator, |
227 | | - readOnly: specs.readOnly || prop.hasReadOnly, |
228 | 231 | action: specs.action || prop.isAction, |
229 | | - layout: specs.layout || prop.isLayout, |
230 | | - tagName: specs.tagName || prop.isTagName, |
| 232 | + attribute: specs.attribute || prop.hasAttributeDecorator, |
231 | 233 | className: specs.className || prop.hasClassNameDecorator, |
232 | 234 | classNames: specs.classNames || prop.isClassNames, |
233 | | - unobserves: specs.unobserves || prop.hasUnobservesDecorator, |
| 235 | + layout: specs.layout || prop.isLayout, |
234 | 236 | off: specs.off || prop.hasOffDecorator, |
| 237 | + readOnly: specs.readOnly || prop.hasReadOnly, |
| 238 | + tagName: specs.tagName || prop.isTagName, |
| 239 | + unobserves: specs.unobserves || prop.hasUnobservesDecorator, |
235 | 240 | volatile: specs.volatile || prop.hasVolatile |
236 | 241 | }; |
237 | 242 | }, decoratorsMap); |
@@ -460,11 +465,21 @@ function getExpressionToReplace(j, eoCallExpression) { |
460 | 465 | * @param {String} filePath |
461 | 466 | * @return {String} |
462 | 467 | */ |
463 | | -function getClassName(j, eoCallExpression, filePath) { |
| 468 | +function getClassName( |
| 469 | + j, |
| 470 | + eoCallExpression, |
| 471 | + filePath, |
| 472 | + superClassName, |
| 473 | + type = "EmberObject" |
| 474 | +) { |
464 | 475 | const varDeclaration = getClosetVariableDeclaration(j, eoCallExpression); |
465 | 476 | const className = |
466 | 477 | getVariableName(varDeclaration) || camelCase(path.basename(filePath, "js")); |
467 | | - return capitalizeFirstLetter(className); |
| 478 | + let capitalizedClassName = capitalizeFirstLetter(className); |
| 479 | + if (capitalizedClassName === superClassName) { |
| 480 | + capitalizedClassName += capitalizeFirstLetter(type); |
| 481 | + } |
| 482 | + return capitalizedClassName; |
468 | 483 | } |
469 | 484 |
|
470 | 485 | /** |
@@ -499,6 +514,24 @@ function parseEmberObjectCallExpression(eoCallExpression) { |
499 | 514 | */ |
500 | 515 | function replaceEmberObjectExpressions(j, root, filePath, options = {}) { |
501 | 516 | logger.info(`[${filePath}]: BEGIN`); |
| 517 | + |
| 518 | + const runtimeConfigPath = options["runtime-config-path"]; |
| 519 | + |
| 520 | + if (runtimeConfigPath) { |
| 521 | + options.runtimeData = getRuntimeData(runtimeConfigPath, filePath); |
| 522 | + if (!options.runtimeData) { |
| 523 | + logger.warn( |
| 524 | + `${filePath} SKIPPED Cound not find runtime data NO_RUNTIME_DATA` |
| 525 | + ); |
| 526 | + return; |
| 527 | + } |
| 528 | + } |
| 529 | + |
| 530 | + if (isTestFile(filePath)) { |
| 531 | + logger.warn(`[${filePath}]: Skipping test file`); |
| 532 | + return; |
| 533 | + } |
| 534 | + |
502 | 535 | if (options.type && !isFileOfType(filePath, options.type)) { |
503 | 536 | logger.warn( |
504 | 537 | `[${filePath}]: FAILURE Type mismatch, expected type '${ |
@@ -536,7 +569,13 @@ function replaceEmberObjectExpressions(j, root, filePath, options = {}) { |
536 | 569 | const superClassName = get(eoCallExpression, "value.callee.object.name"); |
537 | 570 | const es6ClassDeclaration = createClass( |
538 | 571 | j, |
539 | | - getClassName(j, eoCallExpression, filePath), |
| 572 | + getClassName( |
| 573 | + j, |
| 574 | + eoCallExpression, |
| 575 | + filePath, |
| 576 | + superClassName, |
| 577 | + get(options, "runtimeData.type") |
| 578 | + ), |
540 | 579 | eoProps, |
541 | 580 | superClassName, |
542 | 581 | mixins |
@@ -568,11 +607,11 @@ function replaceEmberObjectExpressions(j, root, filePath, options = {}) { |
568 | 607 | } |
569 | 608 |
|
570 | 609 | module.exports = { |
571 | | - getVariableName, |
572 | | - getEmberObjectProps, |
573 | | - getEmberObjectCallExpressions, |
| 610 | + getClassName, |
574 | 611 | getClosetVariableDeclaration, |
| 612 | + getEmberObjectCallExpressions, |
| 613 | + getEmberObjectProps, |
575 | 614 | getExpressionToReplace, |
576 | | - replaceEmberObjectExpressions, |
577 | | - getClassName |
| 615 | + getVariableName, |
| 616 | + replaceEmberObjectExpressions |
578 | 617 | }; |
0 commit comments