-
Notifications
You must be signed in to change notification settings - Fork 13
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
Type CommentJSONValue
object properties
#42
Labels
Comments
CommentJSONValue
object properties
In a project I'm working on, I had to parse tsconfig.json file, which can contains comments. The purpose of my project was to sync ts references and paths in a monorepo. This library lacks strong typing support (or at least I didn't understood how to). What I did: import { assign, CommentArray, CommentObject, parse as jsonCParse, stringify } from "comment-json";
type Reference = Partial<CommentObject> & {
path: string;
};
type CompilerOptions = Partial<CommentObject> & {
paths?: Record<string, CommentArray<string>>;
};
// Only the actual nodes I need
type TSConfigWithComments = Partial<CommentObject> & {
references?: Reference[];
compilerOptions?: CompilerOptions;
};
const tsConfigFile = await readFileAsync(projectData.tsConfigFile);
const tsConfig = jsonCParse(tsConfigFile) as TSConfigWithComments; // The only required cast here
if (tsConfig === undefined) {
throw new Error(`Error parsing ${projectData.tsConfigFile}`);
}
if (!tsConfig.references) { // create property if not present
assign(tsConfig, { references: [] });
}
if (!tsConfig.references) { // Actually useless, but makes TS know the references is not undefined (strictNullChecks enabled)
throw new Error("references should be defined");
}
tsConfig.references.push({ path: someppath; }) // Add reference node
// Ensure compilerOptions.path is present
if (!tsConfig.compilerOptions) {
assign(tsConfig, { compilerOptions: { paths: {} } });
}
if (!tsConfig.compilerOptions?.paths) {
assign(tsConfig.compilerOptions, { paths: {} });
}
// Makes TS know it's not undefined
if (!tsConfig.compilerOptions?.paths) {
throw new Error("compilerOptions.path should be defined");
}
tsConfig.compilerOptions.paths[`${someName}/*`] = new CommentArray(`${somePath}/src/*`);
const newJson = stringify(tsConfig, undefined, 2); Not sure if it's the proper way to handle it, but maybe it can helps? |
@stevebeauge Thanks! Will look into it. |
jace-roell
added a commit
to zowe/zowe-cli
that referenced
this issue
Jun 4, 2024
Loading
Loading status checks…
…-comment-json#42 Signed-off-by: jace-roell <jace.roell@hotmail.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tell us about your environment
I'm running this inside a vscode extension to parse the
extensions.json
Please show your use case / code slices / code link that could reproduce the issue
I want to have the returned value of
parse
correctly typed. It should return an json withrecommendations
asstring[]
, but I can't get it to work.What did you expect to happen?
I want to have the recommendations typed correctly.
Thank you for your help!!
The text was updated successfully, but these errors were encountered: