-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const ts = require('typescript'); | ||
const parser = require('./parser'); | ||
|
||
module.exports = (code) => { | ||
const lines = code.split('\n'); | ||
|
||
const propertyBag = []; | ||
|
||
let constructorLine = null; | ||
let className = null; | ||
let firstClassReferenceLine = null; | ||
let classHash = null; | ||
let replaceSuper = false; | ||
|
||
const transformedCodeArray = lines.map((line, i) => { | ||
if (parser.isComment(line)) { | ||
return ''; | ||
} | ||
if (new RegExp(className).test(line)) { | ||
if (firstClassReferenceLine === null) { | ||
firstClassReferenceLine = i; | ||
} | ||
return line; | ||
} | ||
if (parser.isClassHeader(line)) { | ||
className = parser.parseClassName(line); | ||
return line; | ||
} | ||
if (parser.isConstructor(line)) { | ||
constructorLine = i; | ||
const [replaced, didReplace] = parser.replaceConstructorName( | ||
line, | ||
lines, | ||
i, | ||
); | ||
if (didReplace) { | ||
replaceSuper = true; | ||
} | ||
return replaced; | ||
} | ||
if (parser.isDecorator(line)) { | ||
const [decorator, decoratorArguments] = parser.parseDecorator(line); | ||
if (decorator === 'UCLASS') { | ||
return ''; | ||
} | ||
if (decorator === 'UPROPERTY') { | ||
// basically now we need to check the next line to see what are we trying to decorate | ||
// and get the type accordingly | ||
const nextLine = lines[i + 1]; | ||
const [propertyName, propertyType] = parser.parseTypescriptClassField( | ||
nextLine, | ||
); | ||
propertyBag.push({ | ||
type: propertyType, | ||
name: propertyName, | ||
line: i + 1, | ||
decoratorArguments, | ||
}); | ||
return ''; | ||
} | ||
} | ||
|
||
return line; | ||
}); | ||
|
||
const [newClassHash, processedCode] = parser.injectCompiledClass( | ||
parser.injectBootstrap( | ||
parser.injectProperties( | ||
transformedCodeArray, | ||
propertyBag, | ||
constructorLine, | ||
), | ||
), | ||
firstClassReferenceLine - 1, | ||
className, | ||
); | ||
classHash = newClassHash; | ||
|
||
let times = 0; | ||
const replacedClassReferencesAndSuperCall = processedCode.map((line) => { | ||
if (new RegExp(className).test(line)) { | ||
if (times > 0 && !line.startsWith('export')) { | ||
return line.replace(className, `${className}_${classHash}`); | ||
} | ||
times += 1; | ||
} | ||
|
||
if (/super\(.*\)/.test(line) && replaceSuper) { | ||
return ''; | ||
} | ||
|
||
return line; | ||
}); | ||
|
||
const compiledJavascript = ts.transpileModule( | ||
replacedClassReferencesAndSuperCall.join('\n'), | ||
{ | ||
compilerOptions: { removeComments: false, target: 'es6', module: 'commonjs' }, | ||
}, | ||
).outputText; | ||
|
||
return compiledJavascript; | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,118 +1,15 @@ | ||
/* eslint-disable no-console */ | ||
const yargs = require('yargs/yargs'); | ||
const { hideBin } = require('yargs/helpers'); | ||
const ts = require('typescript'); | ||
|
||
const utils = require('./utils'); | ||
const parser = require('./parser'); | ||
const compiler = require('./compiler'); | ||
|
||
const { argv } = yargs(hideBin(process.argv)); | ||
|
||
const processCode = (code) => { | ||
const lines = code.split('\n'); | ||
|
||
const propertyBag = []; | ||
|
||
let constructorLine = null; | ||
let className = null; | ||
let firstClassReferenceLine = null; | ||
let classHash = null; | ||
let replaceSuper = false; | ||
|
||
const transformedCodeArray = lines.map((line, i) => { | ||
if (parser.isComment(line)) { | ||
return ''; | ||
} | ||
if (new RegExp(className).test(line)) { | ||
console.log(line, i); | ||
if (firstClassReferenceLine === null) { | ||
firstClassReferenceLine = i; | ||
} | ||
return line; | ||
} | ||
if (parser.isClassHeader(line)) { | ||
className = parser.parseClassName(line); | ||
return line; | ||
} | ||
if (parser.isConstructor(line)) { | ||
constructorLine = i; | ||
const [replaced, didReplace] = parser.replaceConstructorName( | ||
line, | ||
lines, | ||
i, | ||
); | ||
if (didReplace) { | ||
replaceSuper = true; | ||
} | ||
return replaced; | ||
} | ||
if (parser.isDecorator(line)) { | ||
const [decorator, decoratorArguments] = parser.parseDecorator(line); | ||
if (decorator === 'UCLASS') { | ||
return ''; | ||
} | ||
if (decorator === 'UPROPERTY') { | ||
// basically now we need to check the next line to see what are we trying to decorate | ||
// and get the type accordingly | ||
const nextLine = lines[i + 1]; | ||
const [propertyName, propertyType] = parser.parseTypescriptClassField( | ||
nextLine, | ||
); | ||
propertyBag.push({ | ||
type: propertyType, | ||
name: propertyName, | ||
line: i + 1, | ||
decoratorArguments, | ||
}); | ||
return ''; | ||
} | ||
} | ||
|
||
return line; | ||
}); | ||
|
||
const [newClassHash, processedCode] = parser.injectCompiledClass( | ||
parser.injectBootstrap( | ||
parser.injectProperties( | ||
transformedCodeArray, | ||
propertyBag, | ||
constructorLine, | ||
), | ||
), | ||
firstClassReferenceLine - 1, | ||
className, | ||
); | ||
classHash = newClassHash; | ||
|
||
let times = 0; | ||
const replacedClassReferencesAndSuperCall = processedCode.map((line) => { | ||
if (new RegExp(className).test(line)) { | ||
if (times > 0 && !line.startsWith('export')) { | ||
return line.replace(className, `${className}_${classHash}`); | ||
} | ||
times += 1; | ||
} | ||
|
||
if (/super\(.*\)/.test(line) && replaceSuper) { | ||
return ''; | ||
} | ||
|
||
return line; | ||
}); | ||
|
||
const compiledJavascript = ts.transpileModule( | ||
replacedClassReferencesAndSuperCall.join('\n'), | ||
{ | ||
compilerOptions: { removeComments: false, target: 'es6', module: 'commonjs' }, | ||
}, | ||
).outputText; | ||
|
||
utils.writeCode(argv.file, compiledJavascript); | ||
}; | ||
|
||
if (argv.file) { | ||
(async () => { | ||
const code = await utils.readCode(argv.file); | ||
processCode(code); | ||
const compiled = compiler(code); | ||
utils.writeCode(argv.file, compiled); | ||
})(); | ||
} |