Skip to content

Commit

Permalink
feat(project): support trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
va-stefanek committed Jul 30, 2021
1 parent 0a24831 commit 67129b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/collection/action/rules/add-action-type.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export function addActionType(options: ActionSchema, stateDir: StateFilePaths):
}

let enumMember = `${options.name} = '[${options.prefix}] ${splitPascalCase(options.name)}'`;
enumMember = enumDeclaration.members.length ? ',' + enumMember : enumMember;
enumMember =
enumDeclaration.members.hasTrailingComma || !enumDeclaration.members.length
? enumMember
: ',' + enumMember;

const change = new InsertChange(stateDir.actions, enumDeclaration.end - 1, enumMember);
const recorder = host.beginUpdate(stateDir.actions);
Expand Down
27 changes: 14 additions & 13 deletions src/collection/reducer/rules/update-reducer.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ function createStateProperties(
): InsertChange[] {
const changes: InsertChange[] = [];
const initializer = reducerFile.initialState.initializer as ts.ObjectLiteralExpression;
let leadingComma = initializer.properties ? !!initializer.properties.length : true;
const addTrailingComma =
initializer.properties.hasTrailingComma || !initializer.properties.length;
const callExpression = findNode(reducerFile.initialState, ts.SyntaxKind.CallExpression);
const objectLiteral = callExpression
? findNode(callExpression, ts.SyntaxKind.ObjectLiteralExpression)
: null;
const initialStatePos = objectLiteral
? objectLiteral.getEnd() - 1
: reducerFile.initialState.getEnd() - 1;

if (!addTrailingComma) {
changes.push(new InsertChange(filePath, initialStatePos - 1, `,`));
}

stateProperties.forEach(stateProperty => {
const callExpression = findNode(reducerFile.initialState, ts.SyntaxKind.CallExpression);
const objectLiteral = callExpression
? findNode(callExpression, ts.SyntaxKind.ObjectLiteralExpression)
: null;
const initialStatePos = objectLiteral
? objectLiteral.getEnd() - 1
: reducerFile.initialState.getEnd() - 1;

// if there is no property in interface, add it to interface and initial state
if (!interfaceProps.find(prop => prop[0].getText() === stateProperty.key)) {
changes.push(
Expand All @@ -84,12 +88,9 @@ function createStateProperties(
new InsertChange(
filePath,
initialStatePos,
`${leadingComma ? ',' : ''}${stateProperty.key}: ${guessInitialValue(
stateProperty.type
)}\n`
`${stateProperty.key}: ${guessInitialValue(stateProperty.type)},\n`
)
);
leadingComma = true;
}
});

Expand Down

0 comments on commit 67129b6

Please sign in to comment.