Skip to content

Fix/axis types #121

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

Merged
merged 17 commits into from
Jun 18, 2025
Prev Previous commit
Next Next commit
add script for checking js extension in imports (thx copilot)
  • Loading branch information
gka committed Jun 18, 2025
commit 9523634479d46316f02d3fdbc32ab6f6a335f8c4
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"prepack": "npx svelte-package",
"release-next": "npm version prerelease --preid next && npm publish && git push && git push --tags && sleep 1 && npm dist-tag add svelteplot@$(npm view . version) next",
"docs": "npm run build && cd build && rsync --recursive . vis4.net:svelteplot/alpha0/",
"screenshots": "node screenshot-examples.js"
"screenshots": "node screenshot-examples.js",
"check-js-extensions": "node scripts/check-js-extensions.js src"
},
"exports": {
".": {
Expand Down
138 changes: 138 additions & 0 deletions scripts/check-js-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/usr/bin/env node
/* eslint-disable no-console */

/**
* This script checks for missing .js extensions in import statements.
* It helps identify issues with ESM imports where TypeScript requires .js extensions.
*/

import { readFile, readdir, stat } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

// Convert file:// URLs to paths
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Regular expressions to match import statements without .js extensions
const regexImportFrom =
/import\s+(?:type\s+)?(?:{[^}]*}|\*\s+as\s+[^;]*|[^;{]*)\s+from\s+['"]([^'"]*)['"]/g;
const regexExportFrom =
/export\s+(?:type\s+)?(?:{[^}]*}|\*\s+as\s+[^;]*)\s+from\s+['"]([^'"]*)['"]/g;

// Skip node_modules and build directories
const excludedDirs = ['node_modules', 'build', '.svelte-kit', 'dist', '.git', 'examples', 'tests'];

// Only check certain file types
const includedExtensions = ['.ts', '.js', '.svelte'];

// Paths that should have .js extensions (relative paths and alias paths)
const shouldHaveJsExtension = (importPath) => {
// Skip Svelte imports
if (importPath.endsWith('.svelte')) return false;

// Skip npm package imports (those that don't start with . or /)
if (
!importPath.startsWith('.') &&
!importPath.startsWith('/') &&
!importPath.startsWith('$lib')
)
return false;

// Skip imports with extensions already
if (path.extname(importPath)) return false;

return true;
};

async function* walkDirectory(dir) {
const entries = await readdir(dir, { withFileTypes: true });

for (const entry of entries) {
const fullPath = path.join(dir, entry.name);

if (entry.isDirectory()) {
if (!excludedDirs.includes(entry.name)) {
yield* walkDirectory(fullPath);
}
} else if (includedExtensions.includes(path.extname(entry.name))) {
yield fullPath;
}
}
}

async function checkFile(filePath) {
const content = await readFile(filePath, 'utf8');
const issues = [];

// Find all import statements
let match;

// Check import statements
regexImportFrom.lastIndex = 0;
while ((match = regexImportFrom.exec(content)) !== null) {
const importPath = match[1];
if (shouldHaveJsExtension(importPath)) {
issues.push({
line: content.substring(0, match.index).split('\n').length,
importPath,
statement: match[0]
});
}
}

// Check export from statements
regexExportFrom.lastIndex = 0;
while ((match = regexExportFrom.exec(content)) !== null) {
const importPath = match[1];
if (shouldHaveJsExtension(importPath)) {
issues.push({
line: content.substring(0, match.index).split('\n').length,
importPath,
statement: match[0]
});
}
}

return { filePath, issues };
}

async function main() {
const rootDir = process.argv[2] || process.cwd();
console.log(`Checking for missing .js extensions in ${rootDir}...\n`);

let totalIssues = 0;
let filesWithIssues = 0;

for await (const filePath of walkDirectory(rootDir)) {
const { issues } = await checkFile(filePath);

if (issues.length > 0) {
console.log(`\x1b[33m${filePath}\x1b[0m`);
filesWithIssues++;

for (const issue of issues) {
totalIssues++;
console.log(
` Line ${issue.line}: Missing .js extension in import: ${issue.importPath}`
);
console.log(` ${issue.statement}`);
}
console.log('');
}
}

if (totalIssues === 0) {
console.log('\x1b[32mNo missing .js extensions found!\x1b[0m');
} else {
console.log(
`\x1b[31mFound ${totalIssues} missing .js extensions in ${filesWithIssues} files.\x1b[0m`
);
process.exit(1);
}
}

main().catch((err) => {
console.error('Error:', err);
process.exit(1);
});
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy