Skip to content

Commit

Permalink
Add TS comparison scripts
Browse files Browse the repository at this point in the history
Ref: First-party TypeScript definitions #3166
  • Loading branch information
maxkfranz committed Aug 14, 2024
1 parent 3f60b38 commit 0d24994
Show file tree
Hide file tree
Showing 10 changed files with 1,634 additions and 3 deletions.
37 changes: 35 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-node-resolve": "^7.1.3",
"@rollup/plugin-replace": "^2.3.2",
"@types/cytoscape": "^3.21.5",
"benchmark": "^2.1.4",
"bluebird": "^3.5.0",
"chai": "^4.1.2",
Expand All @@ -122,6 +123,7 @@
"rimraf": "^3.0.0",
"rollup": "^2.8.2",
"rollup-plugin-license": "^2.3.0",
"rollup-plugin-terser": "^5.3.0"
"rollup-plugin-terser": "^5.3.0",
"typescript": "^5.5.4"
}
}
83 changes: 83 additions & 0 deletions typescript/extract-comm-ts-fns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const ts = require('typescript');
const fs = require('fs');
const path = require('path');

// Load and parse the TypeScript definition file from node_modules
const fileName = path.resolve(__dirname, '..', 'node_modules', '@types', 'cytoscape', 'index.d.ts');
const sourceFile = ts.createSourceFile(
fileName,
fs.readFileSync(fileName, 'utf8'),
ts.ScriptTarget.Latest,
true
);

// Helper function to recursively extract function names
function extractFunctions(node, parentName = '') {
let functions = [];

function getQualifiedName(name) {
return parentName ? `${parentName}.${name}` : name;
}

if (ts.isInterfaceDeclaration(node) || ts.isModuleDeclaration(node)) {
const name = node.name.text;
ts.forEachChild(node, child => {
functions = functions.concat(extractFunctions(child, getQualifiedName(name)));
});
} else if (ts.isMethodSignature(node) || (ts.isPropertySignature(node) && ts.isFunctionTypeNode(node.type))) {
const name = node.name.text;
functions.push(getQualifiedName(name));
} else {
ts.forEachChild(node, child => {
functions = functions.concat(extractFunctions(child, parentName));
});
}

return functions;
}

// Extract functions from the source file
const functions = extractFunctions(sourceFile);

// Filter out options and results entries
const filteredFunctions = functions.filter(fn => {
return !fn.includes('Options') && !fn.includes('Result');
});

// Format the function names according to the API documentation style
const formattedFunctions = filteredFunctions.map(fn => {
if (fn.startsWith('cytoscape.Core')) {
return 'cy.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.Collection')) {
return 'eles.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.Singular')) {
return 'ele.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.EdgeSingular')) {
return 'edge.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.EdgeCollection')) {
return 'edges.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.NodeSingular')) {
return 'node.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.NodeCollection')) {
return 'nodes.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.AbstractEventObject')) {
return 'event.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.LayoutManipulation') || fn.startsWith('cytoscape.LayoutEvents')) {
return 'layout.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.AnimationManipulation')) {
return 'ani.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.Style')) {
return 'style.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.ElementStylesheetStyle')) {
return 'stylesheet.' + fn.split('.').slice(2).join('.');
} else if (fn.startsWith('cytoscape.ElementStylesheetCSS')) {
return 'style.' + fn.split('.').slice(2).join('.');
}
return fn;
});

// Sort the formatted functions alphabetically
const sortedFunctions = formattedFunctions.sort();

// Output the functions as JSON
console.log(JSON.stringify(sortedFunctions, null, 2));
64 changes: 64 additions & 0 deletions typescript/extract-comm-ts-style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const ts = require('typescript');
const fs = require('fs');
const path = require('path');

// Load and parse the TypeScript definition file from node_modules
const fileName = path.resolve(__dirname, '..', 'node_modules', '@types', 'cytoscape', 'index.d.ts');
const sourceFile = ts.createSourceFile(
fileName,
fs.readFileSync(fileName, 'utf8'),
ts.ScriptTarget.Latest,
true
);

// Helper function to recursively find the Css namespace
function findCssNamespace(node) {
let cssNode = null;

function recurse(currentNode) {
if (ts.isModuleDeclaration(currentNode) && currentNode.name.text === 'Css') {
cssNode = currentNode;
} else {
ts.forEachChild(currentNode, child => recurse(child));
}
}

recurse(node);
return cssNode;
}

// Helper function to recursively extract style properties from nested structures within Css namespace
function extractCssProperties(node) {
let styles = [];

if (ts.isInterfaceDeclaration(node)) {
node.members.forEach(member => {
if (ts.isPropertySignature(member)) {
const name = member.name.text;
styles.push(name);
}
});
} else {
ts.forEachChild(node, child => {
styles = styles.concat(extractCssProperties(child));
});
}

return styles;
}

// Find the Css namespace
const cssNamespace = findCssNamespace(sourceFile);

if (cssNamespace) {
// Extract styles from the Css namespace
const styles = extractCssProperties(cssNamespace);

// Remove duplicates and sort the styles alphabetically
const uniqueSortedStyles = Array.from(new Set(styles)).sort();

// Output the styles as JSON
console.log(JSON.stringify(uniqueSortedStyles, null, 2));
} else {
console.error('Css namespace not found in the provided TypeScript definition file.');
}
55 changes: 55 additions & 0 deletions typescript/extract-docs-fns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require('fs');
const path = require('path');

// Load and parse the JSON file
const jsonFilePath = path.resolve(__dirname, '..', 'documentation', 'docmaker.json');
const jsonData = JSON.parse(fs.readFileSync(jsonFilePath, 'utf8'));

// Helper function to recursively extract functions
function extractFunctions(section) {
let functions = [];

if (section.fns) {
section.fns.forEach(fn => {
// Add the main function and formats
if (fn.formats) {
fn.formats.forEach(format => {
if (format.name) {
functions.push(format.name);
} else {
functions.push(fn.name);
}
});
} else {
functions.push(fn.name);
}

// Add pureAliases the same number of times as the main function
if (fn.pureAliases) {
fn.pureAliases.forEach(alias => {
const count = functions.filter(f => f === fn.name).length;
for (let i = 0; i < count; i++) {
functions.push(alias);
}
});
}
});
}

if (section.sections) {
section.sections.forEach(subSection => {
functions = functions.concat(extractFunctions(subSection));
});
}

return functions;
}

// Extract functions from the JSON data
const functions = extractFunctions(jsonData);

// Sort the functions alphabetically
const sortedFunctions = functions.sort();

// Output the functions as JSON
console.log(JSON.stringify(sortedFunctions, null, 2));
40 changes: 40 additions & 0 deletions typescript/extract-docs-style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fs = require('fs');
const path = require('path');

// Load the Markdown file
const markdownFilePath = path.resolve(__dirname, '..', 'documentation', 'md', 'style.md');
const markdownContent = fs.readFileSync(markdownFilePath, 'utf8');

// Define a regex to match the style properties
const stylePropertyRegex = /\*\*`([^`]+)`\*\*/g;

// Extract style properties
let match;
const styleProperties = [];

while ((match = stylePropertyRegex.exec(markdownContent)) !== null) {
const property = match[1];
// Omit entries that have parentheses
if (!property.includes('(')) {
// Handle entries like <pos>
if (property.includes('<pos>')) {
['source', 'mid-source', 'target', 'mid-target'].forEach(pos => {
styleProperties.push(property.replace('<pos>', pos));
});
} else if (property.includes('pie-i')) {
// Handle pie-i properties
styleProperties.push(property);
for (let i = 1; i <= 16; i++) {
styleProperties.push(property.replace('pie-i', `pie-${i}`));
}
} else {
styleProperties.push(property);
}
}
}

// Remove duplicates and sort the style properties alphabetically
const uniqueSortedStyleProperties = Array.from(new Set(styleProperties)).sort();

// Output the style properties as JSON
console.log(JSON.stringify(uniqueSortedStyleProperties, null, 2));
Loading

0 comments on commit 0d24994

Please sign in to comment.