@slim-lang/core 1.2.4 → 1.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/run-dev-slim.js +4 -2
- package/run-slim.js +2 -1
- package/src/bin/cli.js +26 -9
- package/src/bin/config.default.json +2 -1
- package/src/compile.js +9 -9
- package/src/handlers/parserHandler.js +1 -5
- package/src/modulePaths.js +21 -8
- package/src/test-runner.js +3 -2
- package/src/transform.js +2 -2
package/package.json
CHANGED
package/run-dev-slim.js
CHANGED
|
@@ -12,7 +12,8 @@ const config = JSON.parse(
|
|
|
12
12
|
fs.readFileSync("slimconfig.json", "utf8")
|
|
13
13
|
);
|
|
14
14
|
|
|
15
|
-
const
|
|
15
|
+
const distDir = typeof config.dist === "string" && config.dist.trim() ? config.dist : "slim-dist";
|
|
16
|
+
const entry = `${distDir}/${config.main}.js`;
|
|
16
17
|
const hot = process.argv.includes("--hot");
|
|
17
18
|
|
|
18
19
|
let app = null;
|
|
@@ -114,6 +115,7 @@ async function rebuild() {
|
|
|
114
115
|
await rebuild();
|
|
115
116
|
|
|
116
117
|
const watchTarget = config.watch ?? ".";
|
|
118
|
+
const ignoreDirs = new RegExp(`(^|[\\\\/])(node_modules|${distDir}|\\.git)([\\\\/]|$)`);
|
|
117
119
|
|
|
118
120
|
const watcher = chokidar.watch(watchTarget, {
|
|
119
121
|
ignoreInitial: true,
|
|
@@ -121,7 +123,7 @@ const watcher = chokidar.watch(watchTarget, {
|
|
|
121
123
|
stabilityThreshold: 150,
|
|
122
124
|
pollInterval: 50
|
|
123
125
|
},
|
|
124
|
-
ignored: p =>
|
|
126
|
+
ignored: p => ignoreDirs.test(p)
|
|
125
127
|
});
|
|
126
128
|
|
|
127
129
|
watcher.on("all", (_, file) => {
|
package/run-slim.js
CHANGED
|
@@ -11,8 +11,9 @@ const root = process.cwd();
|
|
|
11
11
|
|
|
12
12
|
const config = JSON.parse(readFileSync(path.join(root, 'slimconfig.json'), 'utf8'));
|
|
13
13
|
const mainFile = config.main;
|
|
14
|
+
const distDir = typeof config.dist === 'string' && config.dist.trim() ? config.dist : 'slim-dist';
|
|
14
15
|
|
|
15
|
-
const dist = path.join(root,
|
|
16
|
+
const dist = path.join(root, distDir, `${mainFile}.js`);
|
|
16
17
|
const args = ['--enable-source-maps', '--no-warnings', dist];
|
|
17
18
|
|
|
18
19
|
const proc = spawn('node', args, { stdio: 'inherit' });
|
package/src/bin/cli.js
CHANGED
|
@@ -15,9 +15,7 @@ import pkg from "../../package.json" with { type: "json" };
|
|
|
15
15
|
import defaultConfig from "./config.default.json" with { type: "json" };
|
|
16
16
|
|
|
17
17
|
const root = process.cwd()
|
|
18
|
-
|
|
19
|
-
// the same as the project root during local development. Resolve them from this
|
|
20
|
-
// file's location so the commands work when installed from npm too.
|
|
18
|
+
|
|
21
19
|
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..")
|
|
22
20
|
const compileScript = JSON.stringify(path.join(packageRoot, "src", "compile.js"))
|
|
23
21
|
const runScript = JSON.stringify(path.join(packageRoot, "run-slim.js"))
|
|
@@ -296,16 +294,35 @@ program
|
|
|
296
294
|
|
|
297
295
|
if(params.check) {
|
|
298
296
|
const githubRepo = pkg.repository.url.split("git+https://github.com/")[1].trim().split(".git")[0]
|
|
299
|
-
|
|
300
|
-
|
|
297
|
+
if(!/^[\w.-]+\/[\w.-]+$/.test(githubRepo)) {
|
|
298
|
+
error("Unable to determine a valid GitHub repository for version check")
|
|
299
|
+
return
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
const res = await fetch("https://raw.githubusercontent.com/" + githubRepo + "/main/package.json")
|
|
304
|
+
if(!res.ok) {
|
|
305
|
+
error(`Version check failed: received HTTP ${res.status} from GitHub`)
|
|
306
|
+
return
|
|
307
|
+
}
|
|
301
308
|
|
|
302
|
-
|
|
303
|
-
|
|
309
|
+
const githubPkg = await res.json()
|
|
310
|
+
if(typeof githubPkg?.version !== "string" || !/^\d+\.\d+\.\d+/.test(githubPkg.version)) {
|
|
311
|
+
error("Version check failed: unexpected response format")
|
|
312
|
+
return
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if(githubPkg.version != pkg.version) {
|
|
316
|
+
log(`Your version is not compatible with the latest version of Slim:
|
|
304
317
|
Current: ${githubPkg.version}
|
|
305
318
|
Your's: ${pkg.version}`)
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
log(`You on the latest Slim version`)
|
|
322
|
+
}
|
|
306
323
|
}
|
|
307
|
-
|
|
308
|
-
|
|
324
|
+
catch (err) {
|
|
325
|
+
error(`Version check failed: ${err?.message ?? err}`)
|
|
309
326
|
}
|
|
310
327
|
return
|
|
311
328
|
}
|
package/src/compile.js
CHANGED
|
@@ -7,7 +7,7 @@ import { readFile } from 'fs/promises';
|
|
|
7
7
|
import { Debug } from "./external/core.js";
|
|
8
8
|
import { stripComments } from "./parser.js";
|
|
9
9
|
import { UseError } from "./external/classErrors.js";
|
|
10
|
-
import { getDistPath, resolveSlimSource, PACKAGE_ROOT } from "./modulePaths.js";
|
|
10
|
+
import { getDistPath, resolveSlimSource, PACKAGE_ROOT, distDirName } from "./modulePaths.js";
|
|
11
11
|
|
|
12
12
|
const compiled = new Set()
|
|
13
13
|
|
|
@@ -19,7 +19,7 @@ let useStyle = "import"
|
|
|
19
19
|
|
|
20
20
|
function syncExternal() {
|
|
21
21
|
const srcExternal = path.join(PACKAGE_ROOT, "src/external")
|
|
22
|
-
const distExternal = path.resolve("
|
|
22
|
+
const distExternal = path.resolve(distDirName(), "external")
|
|
23
23
|
|
|
24
24
|
if (!fs.existsSync(srcExternal)) return
|
|
25
25
|
|
|
@@ -117,7 +117,7 @@ function compileFile(slimFile, isEntry = false, mainEntry = null) {
|
|
|
117
117
|
const { code: output, declarations: dts } = transform(code, abs, { jsdoc, declarations, check, uses: useStyle })
|
|
118
118
|
|
|
119
119
|
const outputPath = isEntry
|
|
120
|
-
? path.resolve(
|
|
120
|
+
? path.resolve(distDirName(), `${mainEntry}.js`)
|
|
121
121
|
: getDistPath(abs)
|
|
122
122
|
|
|
123
123
|
fs.mkdirSync(path.dirname(outputPath), { recursive: true })
|
|
@@ -128,8 +128,8 @@ function compileFile(slimFile, isEntry = false, mainEntry = null) {
|
|
|
128
128
|
|
|
129
129
|
function cleanDist(slimFileClear) {
|
|
130
130
|
const keep = new Set([
|
|
131
|
-
path.resolve(
|
|
132
|
-
path.resolve("
|
|
131
|
+
path.resolve(distDirName(), `${slimFileClear}.js`),
|
|
132
|
+
path.resolve(distDirName(), "mappings.json"),
|
|
133
133
|
])
|
|
134
134
|
|
|
135
135
|
function addDirToKeep(dir) {
|
|
@@ -140,11 +140,11 @@ function cleanDist(slimFileClear) {
|
|
|
140
140
|
if (entry.isDirectory()) addDirToKeep(full)
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
|
-
addDirToKeep(path.resolve("
|
|
143
|
+
addDirToKeep(path.resolve(distDirName(), "external"))
|
|
144
144
|
|
|
145
145
|
for (const slimFile of compiled) {
|
|
146
146
|
if (slimFile === path.resolve(`${slimFileClear}.slim`)) {
|
|
147
|
-
keep.add(path.resolve(
|
|
147
|
+
keep.add(path.resolve(distDirName(), `${slimFileClear}.js`))
|
|
148
148
|
} else {
|
|
149
149
|
keep.add(getDistPath(slimFile))
|
|
150
150
|
}
|
|
@@ -173,7 +173,7 @@ function cleanDist(slimFileClear) {
|
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
-
walkAndClean(path.resolve(
|
|
176
|
+
walkAndClean(path.resolve(distDirName()))
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
function writeJsConfig() {
|
|
@@ -191,7 +191,7 @@ function writeJsConfig() {
|
|
|
191
191
|
strict: false,
|
|
192
192
|
skipLibCheck: true
|
|
193
193
|
},
|
|
194
|
-
include: [
|
|
194
|
+
include: [distDirName()]
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 4) + "\n")
|
|
@@ -555,11 +555,7 @@ function parseTypesEdits(code) {
|
|
|
555
555
|
let bodyContent = body.content.trim()
|
|
556
556
|
|
|
557
557
|
const normalizedBody = bodyContent.replace(/;\s*$/, "").trim()
|
|
558
|
-
if (
|
|
559
|
-
throw new TypeDefError(`The "${typeName}" type body must contain exactly one return statement`)
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
if (normalizedBody === "return") bodyContent = "return true"
|
|
558
|
+
if (normalizedBody === "" || normalizedBody === "return") bodyContent = "return true"
|
|
563
559
|
|
|
564
560
|
edits.push({
|
|
565
561
|
start,
|
package/src/modulePaths.js
CHANGED
|
@@ -7,20 +7,32 @@ const slimExtension = ".slim"
|
|
|
7
7
|
|
|
8
8
|
export const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..")
|
|
9
9
|
|
|
10
|
+
function readConfig() {
|
|
11
|
+
try {
|
|
12
|
+
return JSON.parse(fs.readFileSync(path.join(process.cwd(), "slimconfig.json"), "utf8"))
|
|
13
|
+
} catch {
|
|
14
|
+
return {}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
10
18
|
let projectPackagesCache = null
|
|
11
19
|
export function projectPackagesDir() {
|
|
12
20
|
if (projectPackagesCache) return projectPackagesCache
|
|
13
21
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const config = JSON.parse(fs.readFileSync(path.join(process.cwd(), "slimconfig.json"), "utf8"))
|
|
17
|
-
if (typeof config.packages === "string" && config.packages.trim()) dir = config.packages
|
|
18
|
-
} catch {}
|
|
19
|
-
|
|
20
|
-
projectPackagesCache = path.resolve(dir)
|
|
22
|
+
const dir = readConfig().packages
|
|
23
|
+
projectPackagesCache = path.resolve(typeof dir === "string" && dir.trim() ? dir : "packages")
|
|
21
24
|
return projectPackagesCache
|
|
22
25
|
}
|
|
23
26
|
|
|
27
|
+
let distDirCache = null
|
|
28
|
+
export function distDirName() {
|
|
29
|
+
if (distDirCache) return distDirCache
|
|
30
|
+
|
|
31
|
+
const dir = readConfig().dist
|
|
32
|
+
distDirCache = typeof dir === "string" && dir.trim() ? dir : "slim-dist"
|
|
33
|
+
return distDirCache
|
|
34
|
+
}
|
|
35
|
+
|
|
24
36
|
function packageRoots() {
|
|
25
37
|
const projectPackages = projectPackagesDir()
|
|
26
38
|
const shippedPackages = path.join(PACKAGE_ROOT, "packages")
|
|
@@ -63,11 +75,12 @@ export function getDistPath(slimFile) {
|
|
|
63
75
|
} else if (isWithin(projectRoot, abs)) {
|
|
64
76
|
relative = path.relative(projectRoot, abs)
|
|
65
77
|
} else {
|
|
78
|
+
// Drop leading "../" so out-of-project sources can't escape the dist dir.
|
|
66
79
|
const stripped = path.relative(projectRoot, abs).split(path.sep).filter(seg => seg !== "..")
|
|
67
80
|
relative = stripped.length ? path.join(...stripped) : path.basename(abs)
|
|
68
81
|
}
|
|
69
82
|
|
|
70
|
-
return path.resolve(
|
|
83
|
+
return path.resolve(distDirName(), relative.replace(/\.slim$/, ".js"))
|
|
71
84
|
}
|
|
72
85
|
|
|
73
86
|
export function resolveSlimSource(raw, fromFile) {
|
package/src/test-runner.js
CHANGED
|
@@ -3,10 +3,11 @@ import path from "node:path"
|
|
|
3
3
|
import { spawnSync } from "node:child_process"
|
|
4
4
|
import { pathToFileURL } from "node:url"
|
|
5
5
|
import { transform } from "./transform.js"
|
|
6
|
+
import { distDirName } from "./modulePaths.js"
|
|
6
7
|
|
|
7
8
|
const root = process.cwd()
|
|
8
9
|
const runtimeImport = pathToFileURL(path.resolve("src/external/defaults.js")).href
|
|
9
|
-
const outDir = path.resolve("
|
|
10
|
+
const outDir = path.resolve(distDirName(), "__slim_tests__")
|
|
10
11
|
|
|
11
12
|
function findTests(target) {
|
|
12
13
|
if (target) {
|
|
@@ -19,7 +20,7 @@ function findTests(target) {
|
|
|
19
20
|
if (!fs.existsSync(dir)) return
|
|
20
21
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
21
22
|
if (entry.isDirectory()) {
|
|
22
|
-
if (entry.name !== "node_modules" && entry.name !==
|
|
23
|
+
if (entry.name !== "node_modules" && entry.name !== distDirName() && entry.name !== ".git") {
|
|
23
24
|
walk(path.join(dir, entry.name))
|
|
24
25
|
}
|
|
25
26
|
} else if (entry.name.endsWith(".test.slim")) {
|
package/src/transform.js
CHANGED
|
@@ -7,7 +7,7 @@ import { emitJsDoc, emitDeclarations, jsdocComment } from "./jsdoc.js"
|
|
|
7
7
|
import { checkTypes, formatDiagnostics } from "./checker.js"
|
|
8
8
|
import * as t from "@babel/types"
|
|
9
9
|
import path from "node:path"
|
|
10
|
-
import { getDistPath, resolveSlimImport, resolveSlimSource } from "./modulePaths.js"
|
|
10
|
+
import { getDistPath, resolveSlimImport, resolveSlimSource, distDirName } from "./modulePaths.js"
|
|
11
11
|
import {
|
|
12
12
|
PRE_SOURCE,
|
|
13
13
|
buildPreMap,
|
|
@@ -430,7 +430,7 @@ function resolvePath(raw, fromFile) {
|
|
|
430
430
|
|
|
431
431
|
function getRuntimePath(sourceFile, entry) {
|
|
432
432
|
const distFile = getDistPath(sourceFile)
|
|
433
|
-
const runtimeAbs = path.resolve(
|
|
433
|
+
const runtimeAbs = path.resolve(distDirName(), "external", entry)
|
|
434
434
|
const rel = path.relative(path.dirname(distFile), runtimeAbs)
|
|
435
435
|
const relFixed = rel.replace(/\\/g, "/")
|
|
436
436
|
return relFixed.startsWith(".") ? relFixed : "./" + relFixed
|