-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit TS types for variables extracted from sass
- Loading branch information
1 parent
e7f9a00
commit 72a71e0
Showing
3 changed files
with
111 additions
and
6 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
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
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,75 @@ | ||
const ts = require('typescript'); | ||
const prettier = require('prettier'); | ||
|
||
async function deriveSassVariableTypes( | ||
extractedVars, | ||
extractedVarsModuleName, | ||
extractedVarTypesFilename | ||
) { | ||
const extractedVarsModuleDeclaration = ts.createModuleDeclaration( | ||
undefined, | ||
[ts.createModifier(ts.SyntaxKind.DeclareKeyword)], | ||
ts.createStringLiteral(extractedVarsModuleName), | ||
ts.createModuleBlock([ | ||
ts.createVariableStatement( | ||
undefined, | ||
ts.createVariableDeclarationList( | ||
[ | ||
ts.createVariableDeclaration( | ||
'sassVariables', | ||
deriveValueType(extractedVars) | ||
), | ||
], | ||
ts.NodeFlags.Const | ||
) | ||
), | ||
ts.createExportAssignment( | ||
undefined, | ||
undefined, | ||
undefined, | ||
ts.createIdentifier('sassVariables') | ||
), | ||
]), | ||
ts.NodeFlags.None | ||
); | ||
|
||
const moduleSource = ts | ||
.createPrinter({ newLine: ts.NewLineKind.LineFeed }) | ||
.printNode( | ||
ts.EmitHint.Unspecified, | ||
extractedVarsModuleDeclaration, | ||
ts.createSourceFile(extractedVarTypesFilename, '', ts.ScriptTarget.Latest) | ||
); | ||
|
||
const prettierOptions = await prettier.resolveConfig(extractedVarTypesFilename); | ||
const prettifiedModuleSource = prettier.format(moduleSource, prettierOptions); | ||
|
||
return prettifiedModuleSource; | ||
} | ||
|
||
function deriveValueType(extractedValue) { | ||
switch (typeof extractedValue) { | ||
case 'object': | ||
return ts.createTypeLiteralNode( | ||
Object.keys(extractedValue).map(key => | ||
ts.createPropertySignature( | ||
undefined, | ||
ts.createStringLiteral(key), | ||
undefined, | ||
deriveValueType(extractedValue[key]), | ||
undefined | ||
) | ||
) | ||
); | ||
case 'string': | ||
return ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword); | ||
case 'number': | ||
return ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword); | ||
default: | ||
return ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword); | ||
} | ||
} | ||
|
||
module.exports = { | ||
deriveSassVariableTypes, | ||
}; |