eclipse-theia / theia

Eclipse Theia is a cloud & desktop IDE framework implemented in TypeScript.
http://theia-ide.org
Eclipse Public License 2.0
20.07k stars 2.5k forks source link

Provide assistance to the extension developers to keep JSON schema and class in sync #1934

Open simark opened 6 years ago

simark commented 6 years ago

When adding preferences, the pattern is to define a schema, such as

export const cppPreferencesSchema: PreferenceSchema = {
    type: "object",
    properties: {
        "cpp.buildConfigurations": {
            description: "List of build configurations",
            type: "array",
            items: {
                type: "object",
                properties: {
                    "name": {
                        type: "string"
                    },
                    "directory": {
                        type: "string"
                    }
                },
                required: ["name", "directory"],
            }
        }
    }
};

and then write a class that will be passed to PreferenceProxy.

export class CppConfiguration {
    "cpp.buildConfigurations": { name: string, directory: string }[];
}

It is important to keep these two in sync, otherwise the PreferenceProxy will try to access undefined properties, or properties with the wrong type. It would be nice if we had a way to keep them in sync.

The ideal way, I think, would be if we could generate the JSON schema based on the class in a Typescript compiler plugin. However, Typescript compiler plugins can't change how the javascript is emitted, so I don't think we can inject new code.

An alternative would be to write a plugin that checks that the class and the schema are equivalent, and produces a warning or an error if they are not.

akosyakov commented 6 years ago

We could try to add a custom tslint rule for it.

simark commented 6 years ago

Is there an advantage of doing a tslint plugin vs a compiler plugin?

akosyakov commented 6 years ago

ts compiler plugin is fine as well

svenefftinge commented 6 years ago

I wonder if the problem really is big enough to pull the weight of such extra tools.

simark commented 6 years ago

Well, I think it's really easy to make typos, update one and forget to update the other, forget to add a field to the "required" list, etc... And if there's an error, the consequence will be at runtime, so you may not see it in your testing.