Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(material/schematics): create updateModuleSpecifier ts migration …
…fn (#25128) * move Update and writeUpdate to new file to avoid circular deps
- Loading branch information
1 parent
62607c2
commit c682965
Showing
5 changed files
with
127 additions
and
16 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
67 changes: 67 additions & 0 deletions
67
src/material/schematics/migration-utilities/typescript/import-operations.spec.ts
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,67 @@ | ||
import * as ts from 'typescript'; | ||
import {updateModuleSpecifier} from './import-operations'; | ||
|
||
describe('import operations', () => { | ||
describe('updateModuleSpecifier', () => { | ||
function runUpdateModuleSpecifierTest( | ||
description: string, | ||
opts: {old: string; new: string}, | ||
): void { | ||
const node = createNode(opts.old, ts.SyntaxKind.ImportDeclaration) as ts.ImportDeclaration; | ||
const update = updateModuleSpecifier(node!, {moduleSpecifier: 'new-module-name'}); | ||
const newImport = update?.updateFn(opts.old); | ||
expect(newImport).withContext(description).toBe(opts.new); | ||
} | ||
it('updates the module specifier of import declarations', () => { | ||
runUpdateModuleSpecifierTest('default export', { | ||
old: `import defaultExport from 'old-module-name';`, | ||
new: `import defaultExport from 'new-module-name';`, | ||
}); | ||
runUpdateModuleSpecifierTest('namespace import', { | ||
old: `import * as name from 'old-module-name';`, | ||
new: `import * as name from 'new-module-name';`, | ||
}); | ||
runUpdateModuleSpecifierTest('named import', { | ||
old: `import { export1 } from 'old-module-name';`, | ||
new: `import { export1 } from 'new-module-name';`, | ||
}); | ||
runUpdateModuleSpecifierTest('aliased named import', { | ||
old: `import { export1 as alias1 } from 'old-module-name';`, | ||
new: `import { export1 as alias1 } from 'new-module-name';`, | ||
}); | ||
runUpdateModuleSpecifierTest('multiple named import', { | ||
old: `import { export1, export2 } from 'old-module-name';`, | ||
new: `import { export1, export2 } from 'new-module-name';`, | ||
}); | ||
runUpdateModuleSpecifierTest('multiple named import w/ alias', { | ||
old: `import { export1, export2 as alias2 } from 'old-module-name';`, | ||
new: `import { export1, export2 as alias2 } from 'new-module-name';`, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
function createSourceFile(text: string): ts.SourceFile { | ||
return ts.createSourceFile('file.ts', text, ts.ScriptTarget.Latest); | ||
} | ||
|
||
function visitNodes(node: ts.SourceFile | ts.Node, visitFn: (node: ts.Node) => void): void { | ||
node.forEachChild(child => { | ||
visitFn(child); | ||
visitNodes(child, visitFn); | ||
}); | ||
} | ||
|
||
function getNodeByKind(file: ts.SourceFile, kind: ts.SyntaxKind): ts.Node | null { | ||
let node: ts.Node | null = null; | ||
visitNodes(file, (_node: ts.Node) => { | ||
if (_node.kind === kind) { | ||
node = _node; | ||
} | ||
}); | ||
return node; | ||
} | ||
|
||
function createNode(text: string, kind: ts.SyntaxKind): ts.Node | null { | ||
return getNodeByKind(createSourceFile(text), kind); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/material/schematics/migration-utilities/typescript/import-operations.ts
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,31 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Update} from '../update'; | ||
import * as ts from 'typescript'; | ||
|
||
/** Returns an Update that renames the module specifier of the given import declaration node. */ | ||
export function updateModuleSpecifier( | ||
node: ts.ImportDeclaration, | ||
opts: { | ||
moduleSpecifier: string; | ||
}, | ||
): Update { | ||
const moduleSpecifier = node.moduleSpecifier as ts.StringLiteral; | ||
return { | ||
offset: moduleSpecifier.pos, | ||
updateFn: (text: string) => { | ||
const index = text.indexOf(moduleSpecifier.text, moduleSpecifier.pos); | ||
return ( | ||
text.slice(0, index) + | ||
opts.moduleSpecifier + | ||
text.slice(index + moduleSpecifier.text.length) | ||
); | ||
}, | ||
}; | ||
} |
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,23 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/** Stores the data needed to make a single update to a file. */ | ||
export interface Update { | ||
/** The start index of the location of the update. */ | ||
offset: number; | ||
|
||
/** A function to be used to update the file content. */ | ||
updateFn: (text: string) => string; | ||
} | ||
|
||
/** Applies the updates to the given file content in reverse offset order. */ | ||
export function writeUpdates(content: string, updates: Update[]): string { | ||
updates.sort((a, b) => b.offset - a.offset); | ||
updates.forEach(update => (content = update.updateFn(content))); | ||
return content; | ||
} |