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

Adds tsc --init option #4037

Merged
merged 9 commits into from
Aug 26, 2015
Merged

Adds tsc --init option #4037

merged 9 commits into from
Aug 26, 2015

Conversation

tinganho
Copy link
Contributor

Fixes #3079.

Defaults to compiler options:

{
     "module": "commonjs",
     "target": "es3",
     "noImplicitAny": true,
     "rootDir": ".",
     "outDir": "built",
     "sourceMap": false,
}

The compiler options are overridable and you are able to define more options via the CLI(with the exception of the CLI options help, watch, version, init).

Defaults to no files property but if one invokes with files:

tsc --init test.ts

It will add a files property with the specified files.

Adds an exclude of node_modules folder only.

It outputs comments and trailing commas in tsconfig.json. So this PR also adds parsing/stripping of comments and trailing commas in tsconfig.json file.

@tinganho tinganho changed the title Adds test --init option Adds tsc --init option Jul 27, 2015
@tinganho
Copy link
Contributor Author

I could alternatively put tsconfig.json emitter logic in a new file called configEmitter.ts? It is now in the emitter.ts which hosts the code for the main compiler emitter logic, which might not be so appropriate.

@@ -26,6 +26,12 @@ namespace ts {
return undefined;
}

export function writeConfigFile(file: string, compilerOptions: CompilerOptions, fileNames: string[]): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be in tsc.ts, i would even inline it there.

@tinganho
Copy link
Contributor Author

@mhegazy could you please reconsider supporting comments. In addition to giving people verbose description, people might want to make annotations themselves in the config file. I can rework my PR if it it doesn't fit TS.

@mhegazy
Copy link
Contributor

mhegazy commented Jul 28, 2015

I am fine doing comments. i just want them in a different change, it will make it easier to focus on one issue at a time, i.e. what are the defaults or the right things to emit, instead of talking about comments and parsing.

In general i think writing the output should be simpler. for JSON comments, i would be incline to use the scanner to write a small JSON parser, instead of removing comments.

@tinganho
Copy link
Contributor Author

In general i think writing the output should be simpler. for JSON comments, i would be incline to use the scanner to write a small JSON parser, instead of removing comments.

But then you would need to create a JSON parser? Right now, I think it is easier to remove the comments and pass them to JSON.parse.

@tinganho
Copy link
Contributor Author

I just updated the PR.

  • Removed trailing commas. Because VSCode doesn't support it
  • Moved emitter logic to program.ts.
  • Adds a success message after tsc --init.
  • noImplicitAny defaults to false.

Can we skip splitting the comments in another PR? It is really a small addition(15-20 LOC) to this PR and splitting it out means quite much work for me. I mean saving it another branch and then to remember the branch and merge later and sync changes.

@tinganho
Copy link
Contributor Author

tinganho commented Aug 5, 2015

ping @mhegazy

let { optionNameMap } = getOptionNameMap();
writeConfigFile();

function writeConfigFile() {
Copy link
Contributor

Choose a reason for hiding this comment

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

i do not think we need a full JSON serializer. That was my inital comment. for me this function, without the support for comments, can be written as:

JSON.stringify(opt, (k, v) => {
    switch (k) {
        case "target": return scriptTargetToString(k);
        case "module": return moduleKindToString(k);
        case "init":
        case "watch":
        case "version":
        case "help":
            return undefined;
        default: return v;
    }
}, "    ");

@mhegazy
Copy link
Contributor

mhegazy commented Aug 6, 2015

sorry for the delay. let me clarify my comments earlier..

  1. Please split the stripJsonTrivia into a different PR. i do not think we need it to support --init.
  2. The JSON serialization can be much simpler if you do not write comments in the file. I believe this is the right thing to do as the file is not a valid JSON file at this point.

@tinganho
Copy link
Contributor Author

@mhegazy I just update the PR, please take a look when you have time.

@mhegazy mhegazy merged commit 1670ce9 into microsoft:master Aug 26, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Aug 26, 2015

Sorry for the delay. I have manually merged it as there were merge conflicts, i have also marked the new declarations with the /* @internal */ to avoid them showing in the .d.ts, moved the init code to a helper, and streamlined the serialization. take a look in case i messed something up.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 26, 2015

I wounder if this would be something interesting to editors; @basarat, and @jrieken do you see adding a feature in the editor to initialize tsconfig.json? if so we need to expose this in a public API/ tsserver command.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 26, 2015

@tinganho feel free to send your original changes for supporting comments in a separate PR.

@jrieken
Copy link
Member

jrieken commented Aug 26, 2015

@mhegazy - I like the feature but I think for editors it would be more useful if this would be exposed via tsserver. As a nice bonus that way the server would also already know about the new project and wouldn't rely on another event.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 26, 2015

created #4466 to track the new feature.

basarat added a commit to TypeStrong/ntypescript that referenced this pull request Aug 27, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants