Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(store): add ngrx-store-freeze migration #1901

Merged
merged 6 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/data/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
addExportToModule,
addImportToModule,
addProviderToModule,
replaceImport,
} from './utility/ast-utils';

export {
Expand Down
58 changes: 57 additions & 1 deletion modules/data/schematics-core/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import { Change, InsertChange, NoopChange } from './change';
import {
Change,
InsertChange,
NoopChange,
createReplaceChange,
ReplaceChange,
} from './change';
import { Path } from '@angular-devkit/core';

/**
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
Expand Down Expand Up @@ -636,3 +643,52 @@ export function insertImport(
ts.SyntaxKind.StringLiteral
);
}

export function replaceImport(
sourceFile: ts.SourceFile,
path: Path,
importFrom: string,
importAsIs: string,
importToBe: string
): ReplaceChange[] {
const imports = sourceFile.statements
.filter(ts.isImportDeclaration)
.filter(
({ moduleSpecifier }) =>
moduleSpecifier.getText(sourceFile) === `'${importFrom}'` ||
moduleSpecifier.getText(sourceFile) === `"${importFrom}"`
);

if (imports.length === 0) {
return [];
}

const changes = imports
.map(p => (p.importClause!.namedBindings! as ts.NamedImports).elements)
.reduce((imports, curr) => imports.concat(curr), [] as ts.ImportSpecifier[])
.map(specifier => {
if (!ts.isImportSpecifier(specifier)) {
return { hit: false };
}

if (specifier.name.text === importAsIs) {
return { hit: true, specifier, text: specifier.name.text };
}

// if import is renamed
if (
specifier.propertyName &&
specifier.propertyName.text === importAsIs
) {
return { hit: true, specifier, text: specifier.propertyName.text };
}

return { hit: false };
})
.filter(({ hit }) => hit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

.map(({ specifier, text }) =>
createReplaceChange(sourceFile, path, specifier!, text!, importToBe)
);

return changes;
}
27 changes: 14 additions & 13 deletions modules/data/schematics-core/utility/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,18 @@ export class RemoveChange implements Change {
order: number;
description: string;

constructor(
public path: string,
private pos: number,
private toRemove: string
) {
if (pos < 0) {
constructor(public path: string, public pos: number, public end: number) {
if (pos < 0 || end < 0) {
throw new Error('Negative positions are invalid');
}
this.description = `Removed ${toRemove} into position ${pos} of ${path}`;
this.description = `Removed text in position ${pos} to ${end} of ${path}`;
this.order = pos;
}

apply(host: Host): Promise<void> {
return host.read(this.path).then(content => {
const prefix = content.substring(0, this.pos);
const suffix = content.substring(this.pos + this.toRemove.length);
const suffix = content.substring(this.end);

// TODO: throw error if toRemove doesn't match removed string.
return host.write(this.path, `${prefix}${suffix}`);
Expand All @@ -109,7 +105,7 @@ export class ReplaceChange implements Change {

constructor(
public path: string,
private pos: number,
public pos: number,
public oldText: string,
public newText: string
) {
Expand Down Expand Up @@ -151,13 +147,18 @@ export function createReplaceChange(
export function createChangeRecorder(
tree: Tree,
path: Path,
changes: ReplaceChange[]
changes: Change[]
): UpdateRecorder {
const recorder = tree.beginUpdate(path);
for (const change of changes) {
const action = <any>change;
recorder.remove(action.pos, action.oldText.length);
recorder.insertLeft(action.pos, action.newText);
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
} else if (change instanceof RemoveChange) {
recorder.remove(change.pos, change.end - change.pos);
} else if (change instanceof ReplaceChange) {
recorder.remove(change.pos, change.oldText.length);
recorder.insertLeft(change.pos, change.newText);
}
}
return recorder;
}
1 change: 1 addition & 0 deletions modules/effects/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
addExportToModule,
addImportToModule,
addProviderToModule,
replaceImport,
} from './utility/ast-utils';

export {
Expand Down
58 changes: 57 additions & 1 deletion modules/effects/schematics-core/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import { Change, InsertChange, NoopChange } from './change';
import {
Change,
InsertChange,
NoopChange,
createReplaceChange,
ReplaceChange,
} from './change';
import { Path } from '@angular-devkit/core';

/**
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
Expand Down Expand Up @@ -636,3 +643,52 @@ export function insertImport(
ts.SyntaxKind.StringLiteral
);
}

export function replaceImport(
sourceFile: ts.SourceFile,
path: Path,
importFrom: string,
importAsIs: string,
importToBe: string
): ReplaceChange[] {
const imports = sourceFile.statements
.filter(ts.isImportDeclaration)
.filter(
({ moduleSpecifier }) =>
moduleSpecifier.getText(sourceFile) === `'${importFrom}'` ||
moduleSpecifier.getText(sourceFile) === `"${importFrom}"`
);

if (imports.length === 0) {
return [];
}

const changes = imports
.map(p => (p.importClause!.namedBindings! as ts.NamedImports).elements)
.reduce((imports, curr) => imports.concat(curr), [] as ts.ImportSpecifier[])
.map(specifier => {
if (!ts.isImportSpecifier(specifier)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not a duplicate of code in ast-utils.ts#L666

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that this code is repeated in all of the modules?
This is by design. If I'm not mistaken by doing it this way, we don't need to create a schematics-core package. (see #1025 and #1044).

return { hit: false };
}

if (specifier.name.text === importAsIs) {
return { hit: true, specifier, text: specifier.name.text };
}

// if import is renamed
if (
specifier.propertyName &&
specifier.propertyName.text === importAsIs
) {
return { hit: true, specifier, text: specifier.propertyName.text };
}

return { hit: false };
})
.filter(({ hit }) => hit)
.map(({ specifier, text }) =>
createReplaceChange(sourceFile, path, specifier!, text!, importToBe)
);

return changes;
}
27 changes: 14 additions & 13 deletions modules/effects/schematics-core/utility/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,18 @@ export class RemoveChange implements Change {
order: number;
description: string;

constructor(
public path: string,
private pos: number,
private toRemove: string
) {
if (pos < 0) {
constructor(public path: string, public pos: number, public end: number) {
if (pos < 0 || end < 0) {
throw new Error('Negative positions are invalid');
}
this.description = `Removed ${toRemove} into position ${pos} of ${path}`;
this.description = `Removed text in position ${pos} to ${end} of ${path}`;
this.order = pos;
}

apply(host: Host): Promise<void> {
return host.read(this.path).then(content => {
const prefix = content.substring(0, this.pos);
const suffix = content.substring(this.pos + this.toRemove.length);
const suffix = content.substring(this.end);

// TODO: throw error if toRemove doesn't match removed string.
return host.write(this.path, `${prefix}${suffix}`);
Expand All @@ -109,7 +105,7 @@ export class ReplaceChange implements Change {

constructor(
public path: string,
private pos: number,
public pos: number,
public oldText: string,
public newText: string
) {
Expand Down Expand Up @@ -151,13 +147,18 @@ export function createReplaceChange(
export function createChangeRecorder(
tree: Tree,
path: Path,
changes: ReplaceChange[]
changes: Change[]
): UpdateRecorder {
const recorder = tree.beginUpdate(path);
for (const change of changes) {
const action = <any>change;
recorder.remove(action.pos, action.oldText.length);
recorder.insertLeft(action.pos, action.newText);
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
} else if (change instanceof RemoveChange) {
recorder.remove(change.pos, change.end - change.pos);
} else if (change instanceof ReplaceChange) {
recorder.remove(change.pos, change.oldText.length);
recorder.insertLeft(change.pos, change.newText);
}
}
return recorder;
}
1 change: 1 addition & 0 deletions modules/entity/schematics-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
addExportToModule,
addImportToModule,
addProviderToModule,
replaceImport,
} from './utility/ast-utils';

export {
Expand Down
58 changes: 57 additions & 1 deletion modules/entity/schematics-core/utility/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import { Change, InsertChange, NoopChange } from './change';
import {
Change,
InsertChange,
NoopChange,
createReplaceChange,
ReplaceChange,
} from './change';
import { Path } from '@angular-devkit/core';

/**
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
Expand Down Expand Up @@ -636,3 +643,52 @@ export function insertImport(
ts.SyntaxKind.StringLiteral
);
}

export function replaceImport(
sourceFile: ts.SourceFile,
path: Path,
importFrom: string,
importAsIs: string,
importToBe: string
): ReplaceChange[] {
const imports = sourceFile.statements
.filter(ts.isImportDeclaration)
.filter(
({ moduleSpecifier }) =>
moduleSpecifier.getText(sourceFile) === `'${importFrom}'` ||
moduleSpecifier.getText(sourceFile) === `"${importFrom}"`
);

if (imports.length === 0) {
return [];
}

const changes = imports
.map(p => (p.importClause!.namedBindings! as ts.NamedImports).elements)
.reduce((imports, curr) => imports.concat(curr), [] as ts.ImportSpecifier[])
.map(specifier => {
if (!ts.isImportSpecifier(specifier)) {
return { hit: false };
}

if (specifier.name.text === importAsIs) {
return { hit: true, specifier, text: specifier.name.text };
}

// if import is renamed
if (
specifier.propertyName &&
specifier.propertyName.text === importAsIs
) {
return { hit: true, specifier, text: specifier.propertyName.text };
}

return { hit: false };
})
.filter(({ hit }) => hit)
.map(({ specifier, text }) =>
createReplaceChange(sourceFile, path, specifier!, text!, importToBe)
);

return changes;
}
Loading