Skip to content

Commit

Permalink
minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
giseburt committed Apr 28, 2024
1 parent fbf629f commit d24e583
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions src/javascript-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export class JavascriptFunction extends CodeResolvableBase {

const header = this.name ? `function ${this.name}` : ``;
const parametersValue = this.properties
.map((p) => doStringify(p, 0, ""))
.map((p) => javascriptStringify(p, 0, ""))
.join(", ");
const parameters = `(${parametersValue})`;
const arrow = this.name ? " " : " => ";
const bodyValue = (Array.isArray(this.body) ? this.body : [this.body])
.map((p) => dentPlus + doStringify(p, 0, ""))
.map((p) => dentPlus + javascriptStringify(p, 0, ""))
.join("\n");
const body = CodeTokenMap.instance.resolve(`{\n${bodyValue}\n${dent}}`, {
level: level + 1,
Expand Down Expand Up @@ -71,7 +71,7 @@ export class JavascriptDataStructure extends CodeResolvableBase {
super();
}
stringify(level = 0, idt = " ") {
return doStringify(this.body, level, idt) ?? "undefined";
return javascriptStringify(this.body, level, idt) ?? "undefined";
}
}

Expand Down Expand Up @@ -139,7 +139,54 @@ export class JavascriptDependencies extends CodeResolvableBase {
}
}

function doStringify(
/**
* Options for the JsConfigFile class.
*/
export interface JavascriptFileOptions
extends JavascriptDependenciesOptions,
JsonFileOptions {}

/**
* Represents a JS configuratin file (e.g. .eslintrc.js).
* Suppor
*/
export class JavascriptFile extends JsonFile {
public dependencies: JavascriptDependencies;
cjs: boolean;
constructor(
project: Project,
filePath: string,
options: JavascriptFileOptions
) {
super(project, filePath, { ...options, allowComments: false });
this.dependencies = new JavascriptDependencies();
this.cjs = options.cjs ?? false;
if (!options.obj) {
throw new Error('"obj" cannot be undefined');
}
}

protected synthesizeContent(resolver: IResolver): string | undefined {
const json = super.synthesizeContent(resolver);
if (!json) {
return undefined;
}
const data = JSON.parse(json);

const markerHeader = this.marker ? `// ${this.marker}\n` : "";
data["//"] = undefined;

return new JavascriptRaw(
`${markerHeader}${this.dependencies}
${
this.cjs ? `module.exports =` : `export default`
} ${JavascriptDataStructure.value(data)};
`
).resolve();
}
}

function javascriptStringify(
data: unknown,
level = 0,
idt: string = " "
Expand All @@ -158,7 +205,7 @@ function doStringify(
if (Array.isArray(data)) {
const r: Array<string> = [];
for (const val of data) {
const value = doStringify(val, level + 1, idt);
const value = javascriptStringify(val, level + 1, idt);
if (value) {
r.push(value);
}
Expand All @@ -183,7 +230,7 @@ function doStringify(
// 3. `[key]: "value"`
// 4. `...key` (value is ignored)

const value = doStringify(val, level + 1, idt);
const value = javascriptStringify(val, level + 1, idt);
let keyString = key;
// if the key is a token, resolve it (3,4)
if (unresolved(key)) {
Expand Down Expand Up @@ -217,50 +264,3 @@ function doStringify(
}
return JSON.stringify(data);
}

/**
* Options for the JsConfigFile class.
*/
export interface JavascriptFileOptions
extends JavascriptDependenciesOptions,
JsonFileOptions {}

/**
* Represents a JS configuratin file (e.g. .eslintrc.js).
* Suppor
*/
export class JavascriptFile extends JsonFile {
public dependencies: JavascriptDependencies;
cjs: boolean;
constructor(
project: Project,
filePath: string,
options: JavascriptFileOptions
) {
super(project, filePath, { ...options, allowComments: false });
this.dependencies = new JavascriptDependencies();
this.cjs = options.cjs ?? false;
if (!options.obj) {
throw new Error('"obj" cannot be undefined');
}
}

protected synthesizeContent(resolver: IResolver): string | undefined {
const json = super.synthesizeContent(resolver);
if (!json) {
return undefined;
}
const data = JSON.parse(json);

const markerHeader = this.marker ? `// ${this.marker}\n` : "";
data["//"] = undefined;

return new JavascriptRaw(
`${markerHeader}${this.dependencies}
${
this.cjs ? `module.exports =` : `export default`
} ${JavascriptDataStructure.value(data)};
`
).resolve();
}
}

0 comments on commit d24e583

Please sign in to comment.