Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function addReducerToStateInterface(source, reducersPath, options) {
const stateInterface = source.statements.find(stm => stm.kind === ts.SyntaxKind.InterfaceDeclaration);
let node = stateInterface;
if (!node) {
return new change_1.NoopChange();
}
const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);
const keyInsert = state + ': from' + stringUtils.classify(options.name) + '.State;';
const expr = node;
let position;
let toInsert;
if (expr.members.length === 0) {
position = expr.getEnd() - 1;
toInsert = ` ${keyInsert}\n`;
}
else {
node = expr.members[expr.members.length - 1];
position = node.getEnd() + 1;
// Get the indentation of the last element, if any.
.map(n => n.text);
return importFiles.filter(file => file === fileName).length === 1;
});
if (relevantImports.length > 0) {
let importsAsterisk = false;
// imports from import file
const imports = [];
relevantImports.forEach(n => {
Array.prototype.push.apply(imports, findNodes(n, ts.SyntaxKind.Identifier));
if (findNodes(n, ts.SyntaxKind.AsteriskToken).length > 0) {
importsAsterisk = true;
}
});
// if imports * from fileName, don't add symbolName
if (importsAsterisk) {
return new change_1.NoopChange();
}
const importTextNodes = imports.filter(n => n.text === symbolName);
// insert import if it's not there
if (importTextNodes.length === 0) {
const fallbackPos = findNodes(relevantImports[0], ts.SyntaxKind.CloseBraceToken)[0].getStart() ||
findNodes(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].getStart();
return insertAfterLastOccurrence(imports, `, ${symbolName}`, fileToEdit, fallbackPos);
}
return new change_1.NoopChange();
}
// no such import declaration exists
const useStrict = findNodes(rootNode, ts.SyntaxKind.StringLiteral).filter(n => n.getText() === 'use strict');
let fallbackPos = 0;
if (useStrict.length > 0) {
fallbackPos = useStrict[0].end;
}
if (findNodes(n, ts.SyntaxKind.AsteriskToken).length > 0) {
importsAsterisk = true;
}
});
// if imports * from fileName, don't add symbolName
if (importsAsterisk) {
return new change_1.NoopChange();
}
const importTextNodes = imports.filter(n => n.text === symbolName);
// insert import if it's not there
if (importTextNodes.length === 0) {
const fallbackPos = findNodes(relevantImports[0], ts.SyntaxKind.CloseBraceToken)[0].getStart() ||
findNodes(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].getStart();
return insertAfterLastOccurrence(imports, `, ${symbolName}`, fileToEdit, fallbackPos);
}
return new change_1.NoopChange();
}
// no such import declaration exists
const useStrict = findNodes(rootNode, ts.SyntaxKind.StringLiteral).filter(n => n.getText() === 'use strict');
let fallbackPos = 0;
if (useStrict.length > 0) {
fallbackPos = useStrict[0].end;
}
const open = isDefault ? '' : '{ ';
const close = isDefault ? '' : ' }';
// if there are no imports or 'use strict' statement, insert import at beginning of file
const insertAtBeginning = allImports.length === 0 && useStrict.length === 0;
const separator = insertAtBeginning ? '' : ';\n';
const toInsert = `${separator}import ${open}${symbolName}${close}` +
` from '${fileName}'${insertAtBeginning ? ';\n' : ''}`;
return insertAfterLastOccurrence(allImports, toInsert, fileToEdit, fallbackPos, ts.SyntaxKind.StringLiteral);
}
if (ast_utils_1.findNodes(n, ts.SyntaxKind.AsteriskToken).length > 0) {
importsAsterisk_1 = true;
}
});
// if imports * from fileName, don't add symbolName
if (importsAsterisk_1) {
return new change_1.NoopChange();
}
var importTextNodes = imports_1.filter(function (n) { return n.text === symbolName; });
// insert import if it's not there
if (importTextNodes.length === 0) {
var fallbackPos_1 = ast_utils_1.findNodes(relevantImports[0], ts.SyntaxKind.CloseBraceToken)[0].getStart() ||
ast_utils_1.findNodes(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].getStart();
return ast_utils_1.insertAfterLastOccurrence(imports_1, ", " + symbolName, fileToEdit, fallbackPos_1);
}
return new change_1.NoopChange();
}
// no such import declaration exists
var useStrict = ast_utils_1.findNodes(rootNode, ts.SyntaxKind.StringLiteral).filter(function (n) { return n.getText() === 'use strict'; });
var fallbackPos = 0;
if (useStrict.length > 0) {
fallbackPos = useStrict[0].end;
}
var open = isDefault ? '' : '{ ';
var close = isDefault ? '' : ' }';
// if there are no imports or 'use strict' statement, insert import at beginning of file
var insertAtBeginning = allImports.length === 0 && useStrict.length === 0;
var separator = insertAtBeginning ? '' : ';\n';
var toInsert = separator + "import " + open + symbolName + close +
(" from '" + fileName + "'" + (insertAtBeginning ? ';\n' : ''));
return ast_utils_1.insertAfterLastOccurrence(allImports, toInsert, fileToEdit, fallbackPos, ts.SyntaxKind.StringLiteral);
}
function addReducerToActionReducerMap(source, reducersPath, options) {
let initializer;
const actionReducerMap = source.statements
.filter(stm => stm.kind === ts.SyntaxKind.VariableStatement)
.filter((stm) => !!stm.declarationList)
.map((stm) => {
const { declarations, } = stm.declarationList;
const variable = declarations.find((decl) => decl.kind === ts.SyntaxKind.VariableDeclaration);
const type = variable ? variable.type : {};
return { initializer: variable.initializer, type };
})
.find(({ type }) => type.typeName.text === 'ActionReducerMap');
if (!actionReducerMap || !actionReducerMap.initializer) {
return new change_1.NoopChange();
}
let node = actionReducerMap.initializer;
const state = options.plural
? stringUtils.pluralize(options.name)
: stringUtils.camelize(options.name);
const keyInsert = state + ': from' + stringUtils.classify(options.name) + '.reducer,';
const expr = node;
let position;
let toInsert;
if (expr.properties.length === 0) {
position = expr.getEnd() - 1;
toInsert = ` ${keyInsert}\n`;
}
else {
node = expr.properties[expr.properties.length - 1];
position = node.getEnd() + 1;