Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/common/scripts/createLibFile.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { createDtsMinifier, folders, path, tsMorph } from "./deps.ts";
const { ts } = tsMorph;

const libFilesFilePath = path.join(folders.common, "src/data/libFiles.ts");
const libFilesFilePath = path.join(folders.common, "src/data/libFiles.generated.ts");
// todo: grab this from the TypeScript repo's tag
const libFolderPath = path.join(folders.root, "node_modules/typescript/lib");
const minifier = createDtsMinifier(ts);

let libFileText = "// dprint-ignore-file\nexport const libFiles: { fileName: string; text: string; }[] = [";
const entries = Array.from(Deno.readDirSync(libFolderPath));
entries.sort((a, b) => a.name.localeCompare(b.name));

for (const entry of Deno.readDirSync(libFolderPath)) {
for (const entry of entries) {
const isLibFile = entry.isFile && entry.name.startsWith("lib") && entry.name.endsWith(".d.ts");
if (!isLibFile)
continue;
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/common/src/getLibFiles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { libFiles } from "./data/libFiles";
import { libFiles } from "./data/libFiles.generated";
import { errors } from "./errors";
import { StandardizedFilePath } from "./fileSystem";
import { nameof } from "./utils";
Expand Down
14 changes: 14 additions & 0 deletions packages/ts-morph/lib/ts-morph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7107,6 +7107,13 @@ export declare class ImportClause extends ImportClauseBase<ts.ImportClause> {
isTypeOnly(): boolean;
/** Sets if this import declaration is type only. */
setIsTypeOnly(value: boolean): this;
/** Gets if this import clause has a defer phase modifier. */
isDeferred(): boolean;
/**
* Sets if this import declaration should have a defer keyword.
* @throws When not a namespace import.
*/
setIsDeferred(value: boolean): this;
/** Gets the default import or throws if it doesn't exit. */
getDefaultImportOrThrow(message?: string | (() => string)): Identifier;
/** Gets the default import or returns undefined if it doesn't exist. */
Expand Down Expand Up @@ -7134,6 +7141,13 @@ export declare class ImportDeclaration extends ImportDeclarationBase<ts.ImportDe
isTypeOnly(): boolean;
/** Sets if this import declaration is type only. */
setIsTypeOnly(value: boolean): this;
/** Gets if this import declaration has a `defer` phase modifier. */
isDeferred(): boolean;
/**
* Sets if this import declaration is a deferred import.
* @throws When the import is not a namespace import.
*/
setIsDeferred(value: boolean): this;
/** Gets the phase modifier of the import declaration. */
getPhaseModifier(): ImportPhaseModifierSyntaxKind | undefined;
/**
Expand Down
33 changes: 33 additions & 0 deletions packages/ts-morph/src/compiler/ast/module/ImportClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ export class ImportClause extends ImportClauseBase<ts.ImportClause> {
return this;
}

/** Gets if this import clause has a defer phase modifier. */
isDeferred() {
return this.compilerNode.phaseModifier === SyntaxKind.DeferKeyword;
}

/**
* Sets if this import declaration should have a defer keyword.
* @throws When not a namespace import.
*/
setIsDeferred(value: boolean) {
if (this.isDeferred() === value)
return this;

if (value) {
if (this.getNamespaceImport() == null)
throw new Error("Cannot set an import as deferred when not a namespace import.");

insertIntoParentTextRange({
parent: this,
insertPos: this.getStart(),
newText: "defer ",
});
} else {
const deferKeyword = this.getFirstChildByKindOrThrow(ts.SyntaxKind.DeferKeyword);
removeChildren({
children: [deferKeyword],
removeFollowingSpaces: true,
});
}

return this;
}

/**
* Gets the default import or throws if it doesn't exit.
*/
Expand Down
22 changes: 22 additions & 0 deletions packages/ts-morph/src/compiler/ast/module/ImportDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ export class ImportDeclaration extends ImportDeclarationBase<ts.ImportDeclaratio
return this;
}

/** Gets if this import declaration has a `defer` phase modifier. */
isDeferred() {
return this.getImportClause()?.isDeferred() ?? false;
}

/**
* Sets if this import declaration is a deferred import.
* @throws When the import is not a namespace import.
*/
setIsDeferred(value: boolean) {
const importClause = this.getImportClause();
if (importClause == null) {
if (!value)
return this;
else
throw new errors.InvalidOperationError("Cannot set an import as deferred when there is no import clause.");
}

importClause.setIsDeferred(value);
return this;
}

/** Gets the phase modifier of the import declaration. */
getPhaseModifier(): ImportPhaseModifierSyntaxKind | undefined {
return this.getImportClause()?.getPhaseModifier();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,25 @@ describe("ImportDeclaration", () => {
const text = "import defer * as feature from './some-feature.js';";
const { firstChild } = getInfoFromText<ImportDeclaration>(text);
expect(firstChild.getPhaseModifier()).to.equal(SyntaxKind.DeferKeyword);
expect(firstChild.isDeferred()).to.equal(true);
});

it("should add and remove the defer keyword", () => {
const text = "import defer * as feature from './some-feature.js';";
const { firstChild } = getInfoFromText<ImportDeclaration>(text);
firstChild.setIsDeferred(false);
firstChild.setIsDeferred(false);
expect(firstChild.getText()).to.equal("import * as feature from './some-feature.js';");
firstChild.setIsDeferred(true);
firstChild.setIsDeferred(true);
expect(firstChild.getText()).to.equal("import defer * as feature from './some-feature.js';");
});

it("should error setting for non-namespace import", () => {
const text = "import feature from './some-feature.js';";
const { firstChild } = getInfoFromText<ImportDeclaration>(text);
firstChild.setIsDeferred(false);
expect(() => firstChild.setIsDeferred(true)).to.throw(Error, "Cannot set an import as deferred when not a namespace import.");
});
});
});
Loading