Skip to content

Commit 26ab3ae

Browse files
author
Shailesh M. Pachbhai
committed
feat: fix the js formatting
1 parent 474dd93 commit 26ab3ae

File tree

6 files changed

+45
-21
lines changed

6 files changed

+45
-21
lines changed

src/utils/lwcparser/fileutils/XmlFileProcessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class XmlFileProcessor implements FileProcessor {
1818
const filePath: string = file.location;
1919
const parser = new XmlParser(filePath);
2020
const fileDiffUtil = new FileDiffUtil();
21-
const fileContent: Map<string, string> = parser.removeNode(XML_TAG_TO_REPLACE);
21+
const fileContent: Map<string, string> = parser.removeNode(XML_TAG_TO_REPLACE, namespace);
2222
if (fileContent) {
2323
const diff = fileDiffUtil.getFileDiff(
2424
file.name + file.ext,

src/utils/lwcparser/jsParser/JavaScriptParser.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,38 @@
44
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
55
/* eslint-disable no-console */
66
import * as fs from 'fs';
7-
import * as parser from '@babel/parser';
7+
import { parse } from '@babel/parser';
88
import traverse from '@babel/traverse';
9-
import * as t from '@babel/types';
109
import { FileConstant } from '../fileutils/FileConstant';
1110

1211
const DEFAULT_NAMESPACE = 'c';
1312

1413
export class JavaScriptParser {
1514
// Function to replace strings in import declarations and write back to file
16-
public replaceImportSource(filePath: string, oldSource: string): Map<string, string> {
15+
public replaceImportSource(filePath: string, oldSource: string): Map<string, string> | null {
1716
const jsContentMap = new Map<string, string>();
17+
1818
// Read the JavaScript file
1919
const code = fs.readFileSync(filePath, 'utf-8');
20+
21+
// Skip processing if the file has specific markers or doesn't include the oldSource
2022
if (code.includes('Generated class DO NOT MODIFY') || !code.includes(oldSource + '/')) {
2123
return null;
2224
}
25+
26+
// Store the original file content
2327
jsContentMap.set(FileConstant.BASE_CONTENT, code);
24-
// Parse the code into an AST (Abstract Syntax Tree)
25-
const ast = parser.parse(code, {
28+
29+
// Parse the code into an AST
30+
const ast = parse(code, {
2631
sourceType: 'module', // Specify that we are parsing an ES module
27-
plugins: ['decorators'], // Include any relevant plugins if necessary (e.g., 'jsx', 'flow', etc.)
32+
plugins: ['decorators'], // Include any relevant plugins if necessary
2833
});
2934

30-
// Traverse the AST and modify import declarations
35+
// Array to store replacement operations
36+
const replacements: Array<{ original: string; updated: string }> = [];
37+
38+
// Traverse the AST and identify import declarations
3139
traverse(ast, {
3240
ImportDeclaration(path) {
3341
const importSource = path.node.source.value;
@@ -36,14 +44,20 @@ export class JavaScriptParser {
3644
if (importSource.includes(oldSource + '/')) {
3745
// Replace the old substring with the new substring
3846
const updatedSource = importSource.replace(oldSource, DEFAULT_NAMESPACE);
39-
// Update the AST with the new source
40-
path.node.source = t.stringLiteral(updatedSource);
47+
replacements.push({ original: importSource, updated: updatedSource });
4148
}
4249
},
4350
});
44-
// jsContentMap.set(FileConstant.MODIFIED_CONTENT, generate(ast, {}, code).code);
45-
jsContentMap.set(FileConstant.MODIFIED_CONTENT, code.replace(oldSource, DEFAULT_NAMESPACE));
46-
// return generate(ast, {}, code).code;
51+
52+
// Apply replacements directly to the original code
53+
let modifiedCode = code;
54+
replacements.forEach(({ original, updated }) => {
55+
const importRegex = new RegExp(`(["'])${original}(["'])`, 'g'); // Match the import string with quotes
56+
modifiedCode = modifiedCode.replace(importRegex, `$1${updated}$2`);
57+
});
58+
59+
// Store the modified content
60+
jsContentMap.set(FileConstant.MODIFIED_CONTENT, modifiedCode);
4761
return jsContentMap;
4862
}
4963

src/utils/lwcparser/xmlParser/XmlParser.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class XmlParser {
2727
}
2828
}
2929

30-
public removeNode(tagName: string, index = 0): Map<string, string> {
30+
public removeNode(tagName: string, namespace: string, index = 0): Map<string, string> {
3131
const xmlContentMap = new Map<string, string>();
3232
xmlContentMap.set(FileConstant.BASE_CONTENT, this.fileContent.replace('(/(<[^>]+?)/>/g', '$1 />'));
3333
if (!this.xmlDoc) {
@@ -37,8 +37,13 @@ export class XmlParser {
3737

3838
if (nodes.length > index) {
3939
const nodeToRemove = nodes[index];
40-
nodeToRemove.parentNode?.removeChild(nodeToRemove);
41-
40+
if (
41+
nodeToRemove.firstChild != null &&
42+
nodeToRemove.firstChild.nodeValue != null &&
43+
nodeToRemove.firstChild.nodeValue.includes(namespace)
44+
) {
45+
nodeToRemove.parentNode?.removeChild(nodeToRemove);
46+
}
4247
const serializer = new XMLSerializer();
4348
const xmlString: string = serializer.serializeToString(this.xmlDoc);
4449
xmlString.replace(/(<[^>]+?)\/>/g, '$1 />');

test/utils/lwc/parser/input/test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { LightningElement, track, api } from 'lwc';
2-
import * as LABELS from './labels';
2+
import * as LABELS from 'vlocity_ins/labels';
33
import { cloneDeep } from 'runtime_omnistudio_common/lodash';
44
export default class TestInputJsFile extends LightningElement {
55
labels = LABELS;
66

7+
print() {
8+
const test = 'vlocity_ins/testing';
9+
console.log(test);
10+
}
11+
712
@api
813
set actionData(val) {
914
if (val) {

test/utils/lwc/parser/javascriptparser.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ describe('JavaScriptParser', () => {
3434
// Mock file reading
3535
readFileSyncStub.returns(mockFileContent);
3636

37-
parser.replaceImportSource(mockFilePath, 'oldSource');
37+
parser.replaceImportSource(mockFilePath, 'vlocity_ins');
3838

3939
// Assert that readFileSync was called with correct arguments
4040
expect(readFileSyncStub.calledWith(mockFilePath, 'utf-8')).to.be.false;
4141
});
4242

4343
it('should replace import source correctly', () => {
4444
const mockFileContent = `
45-
import something from 'oldSource/module';
45+
import something from 'vlocity_ins/module';
4646
`;
4747

4848
// Mock file reading and writing

test/utils/lwc/parser/xmlparser.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ describe('XmlParser', () => {
1212
});
1313

1414
it('should parse XML string into a Document object', () => {
15-
const serializedXml = xmlParser.removeNode('runtimeNamespace').get('modified');
15+
const serializedXml = xmlParser.removeNode('runtimeNamespace', 'xyz').get('modified');
1616
expect(serializedXml).contains('<LightningComponentBundle');
1717
expect(serializedXml).contains('<isExposed>');
1818
});
1919

2020
it('should remove the runtimeNamespace element correctly', () => {
21-
const updatedXml = xmlParser.removeNode('runtimeNamespace');
21+
const updatedXml = xmlParser.removeNode('runtimeNamespace', 'xyz');
2222
expect(updatedXml).not.contains('<runtimeNamespace>');
2323
});
2424
});

0 commit comments

Comments
 (0)