Skip to content

Commit

Permalink
feat: Add new project.compilerOptions property that has the ability t…
Browse files Browse the repository at this point in the history
…o change the compiler options.

BREAKING CHANGE: ScriptTarget was moved from manipulation settings to be stored exclusively in the compiler options.
  • Loading branch information
dsherret committed Mar 25, 2018
1 parent 117433e commit 4da80ba
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 67 deletions.
5 changes: 1 addition & 4 deletions docs/manipulation/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: Manipulation Settings
The manipulation settings can be set when creating the main AST object:

```ts
import Project, {QuoteType, NewLineKind, IndentationText, ScriptTarget} from "ts-simple-ast";
import Project, {QuoteType, NewLineKind, IndentationText} from "ts-simple-ast";

const project = new Project({
// these are the defaults
Expand All @@ -16,8 +16,6 @@ const project = new Project({
indentationText: IndentationText.FourSpaces,
// LineFeed or CarriageReturnLineFeed
newLineKind: NewLineKind.LineFeed,
// defines what ts.ScriptTarget source files are created with
scriptTarget: ScriptTarget.Latest,
// Single or Double
quoteType: QuoteType.Double
}
Expand All @@ -40,7 +38,6 @@ Get more details about the settings by looking at the `manipulationSettings` pro
project.manipulationSettings.getIndentationText();
project.manipulationSettings.getNewLineKind();
project.manipulationSettings.getQuoteType();
project.manipulationSettings.getScriptTarget();
```

### Updating
Expand Down
8 changes: 4 additions & 4 deletions src/GlobalContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {CompilerFactory} from "./factories";
import {ts, CompilerOptions} from "./typescript";
import {LanguageService, TypeChecker} from "./compiler";
import {createWrappedNode} from "./utils/compiler/createWrappedNode";
import {ManipulationSettingsContainer} from "./ManipulationSettings";
import {ManipulationSettingsContainer, CompilerOptionsContainer} from "./options";
import {FileSystemWrapper} from "./fileSystem";
import {Logger, ConsoleLogger, LazyReferenceCoordinator} from "./utils";

Expand All @@ -25,13 +25,13 @@ export class GlobalContainer {
private readonly _lazyReferenceCoordinator: LazyReferenceCoordinator;
private readonly _languageService: LanguageService | undefined;
private readonly _fileSystemWrapper: FileSystemWrapper;
private readonly _compilerOptions: CompilerOptions;
private readonly _compilerOptions = new CompilerOptionsContainer();
private readonly _customTypeChecker: TypeChecker | undefined;
private readonly _logger = new ConsoleLogger();

constructor(fileSystemWrapper: FileSystemWrapper, compilerOptions: CompilerOptions, opts: GlobalContainerOptions) {
this._fileSystemWrapper = fileSystemWrapper;
this._compilerOptions = compilerOptions;
this._compilerOptions.set(compilerOptions);
this._compilerFactory = new CompilerFactory(this);
this._lazyReferenceCoordinator = new LazyReferenceCoordinator(this._compilerFactory);
this._languageService = opts.createLanguageService ? new LanguageService(this) : undefined;
Expand Down Expand Up @@ -116,7 +116,7 @@ export class GlobalContainer {
* Gets the encoding.
*/
getEncoding() {
return this.compilerOptions.charset || "utf-8";
return this.compilerOptions.get().charset || "utf-8";
}

/**
Expand Down
10 changes: 7 additions & 3 deletions src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {SourceFileStructure} from "./structures";
import {getTsConfigParseResult, getCompilerOptionsFromTsConfigParseResult, getFilePathsFromTsConfigParseResult, TsConfigParseResult,
FileUtils, ArrayUtils, matchGlobs} from "./utils";
import {DefaultFileSystemHost, VirtualFileSystemHost, FileSystemHost, FileSystemWrapper, Directory} from "./fileSystem";
import {ManipulationSettings, ManipulationSettingsContainer} from "./ManipulationSettings";
import {ManipulationSettings, ManipulationSettingsContainer, CompilerOptionsContainer} from "./options";
import {GlobalContainer} from "./GlobalContainer";

export interface Options {
Expand Down Expand Up @@ -106,6 +106,11 @@ export class Project {
return this.global.manipulationSettings;
}

/** Gets the compiler options for modification. */
get compilerOptions(): CompilerOptionsContainer {
return this.global.compilerOptions;
}

/**
* Adds an existing directory from the path or returns undefined if it doesn't exist.
*
Expand Down Expand Up @@ -489,8 +494,7 @@ export class Project {
* Gets the compiler options.
*/
getCompilerOptions(): CompilerOptions {
// return a copy
return {...this.global.compilerOptions};
return this.global.compilerOptions.get();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ts, SyntaxKind} from "../../typescript";
import CodeBlockWriter from "code-block-writer";
import * as errors from "../../errors";
import {GlobalContainer} from "../../GlobalContainer";
import {IndentationText} from "../../ManipulationSettings";
import {IndentationText} from "../../options";
import {StructureToText} from "../../structureToTexts";
import {insertIntoParentTextRange, getNextNonWhitespacePos, getPreviousMatchingPos, replaceSourceFileTextForFormatting,
getTextFromFormattingEdits} from "../../manipulation";
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/tools/LanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class LanguageService {
let version = 0;
const fileExistsSync = (path: string) => this.global.compilerFactory.containsSourceFileAtPath(path) || global.fileSystemWrapper.fileExistsSync(path);
const languageServiceHost: ts.LanguageServiceHost = {
getCompilationSettings: () => global.compilerOptions,
getCompilationSettings: () => global.compilerOptions.get(),
getNewLine: () => global.manipulationSettings.getNewLineKindAsString(),
getScriptFileNames: () => this.global.compilerFactory.getSourceFilePaths(),
getScriptVersion: fileName => {
Expand All @@ -46,9 +46,9 @@ export class LanguageService {
getCurrentDirectory: () => global.fileSystemWrapper.getCurrentDirectory(),
getDefaultLibFileName: options => {
if (this.global.fileSystemWrapper.getFileSystem() instanceof DefaultFileSystemHost)
return ts.getDefaultLibFilePath(global.compilerOptions);
return ts.getDefaultLibFilePath(global.compilerOptions.get());
else
return FileUtils.pathJoin(global.fileSystemWrapper.getCurrentDirectory(), "node_modules/typescript/lib/" + ts.getDefaultLibFileName(global.compilerOptions));
return FileUtils.pathJoin(global.fileSystemWrapper.getCurrentDirectory(), "node_modules/typescript/lib/" + ts.getDefaultLibFileName(global.compilerOptions.get()));
},
useCaseSensitiveFileNames: () => true,
readFile: (path, encoding) => {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/tools/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class Program {
* @internal
*/
reset(rootNames: string[], host: ts.CompilerHost) {
const compilerOptions = this.global.compilerOptions;
const compilerOptions = this.global.compilerOptions.get();
this._getOrCreateCompilerObject = () => {
if (this._createdCompilerObject == null)
this._createdCompilerObject = ts.createProgram(rootNames, compilerOptions, host);
Expand Down
4 changes: 2 additions & 2 deletions src/factories/CompilerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class CompilerFactory {
const {filePath = "tsSimpleAstTempFile.ts", createLanguageService = false} = opts;
return createTempSourceFile(filePath, sourceText, {
createLanguageService,
compilerOptions: this.global.compilerOptions,
compilerOptions: this.global.compilerOptions.get(),
manipulationSettings: this.global.manipulationSettings.get()
});
}
Expand Down Expand Up @@ -247,7 +247,7 @@ export class CompilerFactory {

private getSourceFileFromText(filePath: string, sourceText: string, options: AddSourceFileOptions): SourceFile {
const compilerSourceFile = createCompilerSourceFile(filePath, sourceText,
options.languageVersion != null ? options.languageVersion : this.global.manipulationSettings.getScriptTarget());
options.languageVersion != null ? options.languageVersion : this.global.compilerOptions.get().target);
return this.getSourceFile(compilerSourceFile);
}

Expand Down
2 changes: 1 addition & 1 deletion src/factories/createTempSourceFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ts, CompilerOptions, ScriptTarget} from "../typescript";
import {SourceFile} from "../compiler";
import {GlobalContainer} from "../GlobalContainer";
import {ManipulationSettings} from "../ManipulationSettings";
import {ManipulationSettings} from "../options";
import {VirtualFileSystemHost} from "../fileSystem/VirtualFileSystemHost";
import {FileSystemWrapper} from "../fileSystem/FileSystemWrapper";

Expand Down
2 changes: 1 addition & 1 deletion src/fileSystem/Directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ export class Directory {
const isDtsFile = options.declarationDir == null && options.outDir == null ? undefined : /\.d\.ts$/i;
const getStandardizedPath = (path: string | undefined) => path == null ? undefined : this.global.fileSystemWrapper.getStandardizedAbsolutePath(path, this.getPath());
const getSubDirPath = (path: string | undefined, dir: Directory) => path == null ? undefined : FileUtils.pathJoin(path, dir.getBaseName());
const hasDeclarationDir = this.global.compilerOptions.declarationDir != null || options.declarationDir != null;
const hasDeclarationDir = this.global.compilerOptions.get().declarationDir != null || options.declarationDir != null;

return emitDirectory(this, getStandardizedPath(options.outDir), getStandardizedPath(options.declarationDir));

Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export * from "./structures";
export {Project as default} from "./Project";
export {Options, CreateSourceFileOptions} from "./Project";
export {FileSystemHost, Directory, DirectoryEmitResult} from "./fileSystem";
export * from "./ManipulationSettings";
export * from "./options";
export {Constructor} from "./Constructor";
export {createWrappedNode, CreateWrappedNodeOptions} from "./utils/compiler/createWrappedNode";
export {getCompilerOptionsFromTsConfig, CompilerOptionsFromTsConfigOptions, CompilerOptionsFromTsConfigResult} from "./utils/tsconfig/getCompilerOptionsFromTsConfig";
export {printNode, PrintNodeOptions} from "./utils/compiler/printNode";
export {Constructor} from "./Constructor";
export {TypeGuards} from "./utils/TypeGuards";
export {SourceFileReferencingNodes} from "./utils/references/SourceFileReferenceContainer";
11 changes: 11 additions & 0 deletions src/options/CompilerOptionsContainer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {CompilerOptions} from "../typescript";
import {SettingsContainer} from "./SettingsContainer";

/**
* Holds the compiler options.
*/
export class CompilerOptionsContainer extends SettingsContainer<CompilerOptions> {
constructor() {
super({});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as objectAssign from "object-assign";
import {ts, ScriptTarget, NewLineKind, EditorSettings} from "./typescript";
import {QuoteType} from "./compiler";
import {newLineKindToString, fillDefaultEditorSettings} from "./utils";
import {ts, NewLineKind, EditorSettings} from "../typescript";
import {QuoteType} from "../compiler";
import {newLineKindToString, fillDefaultEditorSettings} from "../utils";
import {SettingsContainer} from "./SettingsContainer";

/** Kinds of indentation */
export enum IndentationText {
Expand All @@ -23,24 +24,24 @@ export interface ManipulationSettings {
indentationText: IndentationText;
/** New line kind */
newLineKind: NewLineKind;
/** Script target. */
scriptTarget: ScriptTarget;
/** Quote type used for string literals. */
quoteType: QuoteType;
}

/**
* Holds the manipulation settings.
*/
export class ManipulationSettingsContainer {
private readonly settings: ManipulationSettings = {
indentationText: IndentationText.FourSpaces,
newLineKind: NewLineKind.LineFeed,
scriptTarget: ScriptTarget.Latest,
quoteType: QuoteType.Double
};
export class ManipulationSettingsContainer extends SettingsContainer<ManipulationSettings> {
private editorSettings: EditorSettings | undefined;

constructor() {
super({
indentationText: IndentationText.FourSpaces,
newLineKind: NewLineKind.LineFeed,
quoteType: QuoteType.Double
});
}

/**
* Gets the editor settings based on the current manipulation settings.
*/
Expand Down Expand Up @@ -81,26 +82,12 @@ export class ManipulationSettingsContainer {
return this.settings.indentationText;
}

/**
* Gets the script target.
*/
getScriptTarget() {
return this.settings.scriptTarget;
}

/**
* Gets a copy of the manipulations settings as an object.
*/
get(): ManipulationSettings {
return {...this.settings};
}

/**
* Sets one or all of the settings.
* @param settings - Settings to set.
*/
set(settings: Partial<ManipulationSettings>) {
objectAssign(this.settings, settings);
super.set(settings);
this.editorSettings = undefined;
}
}
33 changes: 33 additions & 0 deletions src/options/SettingsContainer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as objectAssign from "object-assign";

export abstract class SettingsContainer<T extends object> {
private readonly defaultSettings: T;
protected settings: T;

constructor(defaultSettings: T) {
this.defaultSettings = objectAssign({}, defaultSettings);
this.settings = defaultSettings;
}

/**
* Resets the settings to the default.
*/
reset() {
this.settings = objectAssign({}, this.defaultSettings);
}

/**
* Gets a copy of the settings as an object.
*/
get(): T {
return objectAssign({}, this.settings);
}

/**
* Sets one or all of the settings.
* @param settings - Settings to set.
*/
set(settings: Partial<T>) {
objectAssign(this.settings, settings);
}
}
3 changes: 3 additions & 0 deletions src/options/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./CompilerOptionsContainer";
export * from "./ManipulationSettingsContainer";
export * from "./SettingsContainer";
2 changes: 1 addition & 1 deletion src/tests/compiler/file/sourceFileTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as errors from "../../../errors";
import {ts, LanguageVariant, ScriptTarget, NewLineKind} from "../../../typescript";
import {SourceFile, ImportDeclaration, ExportDeclaration, ExportAssignment, EmitResult, FormatCodeSettings, QuoteType,
FileSystemRefreshResult} from "../../../compiler";
import {IndentationText, ManipulationSettings} from "../../../ManipulationSettings";
import {IndentationText, ManipulationSettings} from "../../../options";
import {ImportDeclarationStructure, ExportDeclarationStructure, SourceFileSpecificStructure, ExportAssignmentStructure} from "../../../structures";
import {getInfoFromText} from "../testHelpers";
import {getFileSystemHostWithFiles} from "../../testHelpers";
Expand Down
17 changes: 6 additions & 11 deletions src/tests/manipulationSettingsTests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from "chai";
import {ManipulationSettings, ManipulationSettingsContainer, IndentationText} from "../ManipulationSettings";
import {ts, ScriptTarget, NewLineKind, EditorSettings, IndentStyle} from "../typescript";
import {ManipulationSettings, ManipulationSettingsContainer, IndentationText} from "../options";
import {ts, NewLineKind, EditorSettings, IndentStyle} from "../typescript";
import {QuoteType} from "../compiler";
import {StringUtils} from "../utils";

Expand Down Expand Up @@ -29,16 +29,14 @@ describe(nameof(ManipulationSettingsContainer), () => {
expect(settings.getQuoteType()).to.equal(settingsSettings.quoteType);
expect(settings.getNewLineKind()).to.equal(settingsSettings.newLineKind);
expect(settings.getIndentationText()).to.equal(settingsSettings.indentationText);
expect(settings.getScriptTarget()).to.equal(settingsSettings.scriptTarget);
}

it("should have the correct defaults", () => {
const settings = new ManipulationSettingsContainer();
checkSettings(settings, {
quoteType: QuoteType.Double,
newLineKind: NewLineKind.LineFeed,
indentationText: IndentationText.FourSpaces,
scriptTarget: ScriptTarget.Latest
indentationText: IndentationText.FourSpaces
});
});

Expand All @@ -51,8 +49,7 @@ describe(nameof(ManipulationSettingsContainer), () => {
checkSettings(settings, {
quoteType: QuoteType.Single,
newLineKind: NewLineKind.LineFeed,
indentationText: IndentationText.FourSpaces,
scriptTarget: ScriptTarget.Latest
indentationText: IndentationText.FourSpaces
});
});

Expand All @@ -61,15 +58,13 @@ describe(nameof(ManipulationSettingsContainer), () => {
settings.set({
quoteType: QuoteType.Single,
newLineKind: NewLineKind.CarriageReturnLineFeed,
indentationText: IndentationText.EightSpaces,
scriptTarget: ScriptTarget.ES3
indentationText: IndentationText.EightSpaces
});

checkSettings(settings, {
quoteType: QuoteType.Single,
newLineKind: NewLineKind.CarriageReturnLineFeed,
indentationText: IndentationText.EightSpaces,
scriptTarget: ScriptTarget.ES3
indentationText: IndentationText.EightSpaces
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/tests/projectTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {ts, SyntaxKind, CompilerOptions, ScriptTarget} from "../typescript";
import {EmitResult, Node, SourceFile, NamespaceDeclaration, InterfaceDeclaration, ClassDeclaration} from "../compiler";
import {VirtualFileSystemHost} from "../fileSystem";
import {Project} from "../Project";
import {IndentationText} from "../ManipulationSettings";
import {IndentationText} from "../options";
import {FileUtils} from "../utils";
import * as errors from "../errors";
import * as testHelpers from "./testHelpers";
Expand Down
2 changes: 1 addition & 1 deletion src/tests/utils/compiler/createWrappedNodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe(nameof(createWrappedNode), () => {
const compilerOptions = { target: ScriptTarget.ES2016 };
const node = createWrappedNode(child, { compilerOptions });

expect(node.global.compilerOptions).to.deep.equal(compilerOptions);
expect(node.global.compilerOptions.get()).to.deep.equal(compilerOptions);
});

it("should be able to provide a type checker", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/compiler/createCompilerSourceFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ts, ScriptTarget} from "../../typescript";

export function createCompilerSourceFile(filePath: string, text: string, scriptTarget: ScriptTarget) {
return ts.createSourceFile(filePath, text, scriptTarget, true);
export function createCompilerSourceFile(filePath: string, text: string, scriptTarget: ScriptTarget | undefined) {
return ts.createSourceFile(filePath, text, scriptTarget == null ? ScriptTarget.Latest : scriptTarget, true);
}
2 changes: 1 addition & 1 deletion src/utils/fillDefaultEditorSettings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ManipulationSettingsContainer, IndentationText} from "../ManipulationSettings";
import {ManipulationSettingsContainer, IndentationText} from "../options";
import {ts, IndentStyle, EditorSettings} from "../typescript";
import {setValueIfUndefined} from "./setValueIfUndefined";

Expand Down

0 comments on commit 4da80ba

Please sign in to comment.