@effect/language-service 0.40.1 → 0.41.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effect/language-service",
3
- "version": "0.40.1",
3
+ "version": "0.41.1",
4
4
  "description": "A Language-Service Plugin to Refactor and Diagnostic effect-ts projects",
5
5
  "main": "index.cjs",
6
6
  "bin": {
package/transform.js CHANGED
@@ -1214,6 +1214,56 @@ function parse(config) {
1214
1214
  var TypeScriptApi = Tag("TypeScriptApi");
1215
1215
  var TypeScriptProgram = Tag("TypeScriptProgram");
1216
1216
  var ChangeTracker = Tag("ChangeTracker");
1217
+ function getPackageJsonInfoCache(program) {
1218
+ try {
1219
+ if (hasProperty(program, "getModuleResolutionCache") && isFunction2(program.getModuleResolutionCache)) {
1220
+ const moduleResolutionCache = program.getModuleResolutionCache();
1221
+ if (hasProperty(moduleResolutionCache, "getPackageJsonInfoCache") && isFunction2(moduleResolutionCache.getPackageJsonInfoCache)) {
1222
+ return moduleResolutionCache.getPackageJsonInfoCache();
1223
+ }
1224
+ }
1225
+ } catch (_) {
1226
+ return void 0;
1227
+ }
1228
+ return void 0;
1229
+ }
1230
+ function getDirectoryPath(ts, path) {
1231
+ try {
1232
+ if (hasProperty(ts, "getDirectoryPath") && isFunction2(ts.getDirectoryPath)) {
1233
+ return ts.getDirectoryPath(path);
1234
+ }
1235
+ return path;
1236
+ } catch (_) {
1237
+ return path;
1238
+ }
1239
+ }
1240
+ function makeGetModuleSpecifier(ts) {
1241
+ if (!(hasProperty(ts, "moduleSpecifiers") && hasProperty(ts.moduleSpecifiers, "getModuleSpecifier") && isFunction2(ts.moduleSpecifiers.getModuleSpecifier))) return;
1242
+ const _internal = ts.moduleSpecifiers.getModuleSpecifier;
1243
+ return (compilerOptions, importingSourceFile, importingSourceFileName, toFileName, host, options) => {
1244
+ return _internal(
1245
+ compilerOptions,
1246
+ importingSourceFile,
1247
+ importingSourceFileName,
1248
+ toFileName,
1249
+ host,
1250
+ options
1251
+ );
1252
+ };
1253
+ }
1254
+ function makeGetTemporaryModuleResolutionState(ts) {
1255
+ if (hasProperty(ts, "getTemporaryModuleResolutionState") && isFunction2(ts.getTemporaryModuleResolutionState)) {
1256
+ const _internal = ts.getTemporaryModuleResolutionState;
1257
+ return (cache, program, compilerOptions) => _internal(cache, program, compilerOptions);
1258
+ }
1259
+ return void 0;
1260
+ }
1261
+ function makeGetPackageScopeForPath(ts) {
1262
+ if (hasProperty(ts, "getPackageScopeForPath") && isFunction2(ts.getPackageScopeForPath)) {
1263
+ const _internal = ts.getPackageScopeForPath;
1264
+ return (path, state) => _internal(path, state);
1265
+ }
1266
+ }
1217
1267
 
1218
1268
  // src/core/TypeScriptUtils.ts
1219
1269
  var TypeScriptUtils = Tag("TypeScriptUtils");
@@ -1222,6 +1272,8 @@ var nanoLayer = (fa) => pipe(
1222
1272
  flatMap2((ts) => pipe(fa, provideService(TypeScriptUtils, makeTypeScriptUtils(ts))))
1223
1273
  );
1224
1274
  function makeTypeScriptUtils(ts) {
1275
+ const getTemporaryModuleResolutionState = makeGetTemporaryModuleResolutionState(ts);
1276
+ const getPackageScopeForPath = makeGetPackageScopeForPath(ts);
1225
1277
  function parsePackageContentNameAndVersionFromScope(v) {
1226
1278
  if (!isObject(v)) return;
1227
1279
  if (!hasProperty(v, "packageJsonScope")) return;
@@ -1256,20 +1308,26 @@ function makeTypeScriptUtils(ts) {
1256
1308
  exportsKeys
1257
1309
  };
1258
1310
  }
1259
- function resolveModulePattern(program, sourceFile, pattern) {
1260
- if (pattern.indexOf("*") === -1) return [pattern.toLowerCase()];
1311
+ function resolveModuleWithPackageInfoFromSourceFile(program, sourceFile) {
1261
1312
  let packageJsonScope = parsePackageContentNameAndVersionFromScope(sourceFile);
1262
- if (!packageJsonScope && hasProperty(ts, "getPackageScopeForPath") && isFunction2(ts.getPackageScopeForPath) && hasProperty(ts, "getTemporaryModuleResolutionState") && isFunction2(ts.getTemporaryModuleResolutionState) && hasProperty(ts, "getPackageScopeForPath") && isFunction2(ts.getPackageScopeForPath)) {
1263
- const temporaryModuleResolutionState = ts.getTemporaryModuleResolutionState(
1264
- void 0,
1313
+ if (!packageJsonScope && getPackageScopeForPath && getTemporaryModuleResolutionState) {
1314
+ const packageJsonInfoCache = getPackageJsonInfoCache(program);
1315
+ const temporaryModuleResolutionState = getTemporaryModuleResolutionState(
1316
+ packageJsonInfoCache,
1265
1317
  program,
1266
1318
  program.getCompilerOptions()
1267
1319
  );
1320
+ const directoryPath = getDirectoryPath(ts, sourceFile.fileName);
1268
1321
  packageJsonScope = parsePackageContentNameAndVersionFromScope({
1269
1322
  ...sourceFile,
1270
- packageJsonScope: ts.getPackageScopeForPath(sourceFile.fileName, temporaryModuleResolutionState)
1323
+ packageJsonScope: getPackageScopeForPath(directoryPath, temporaryModuleResolutionState)
1271
1324
  });
1272
1325
  }
1326
+ return packageJsonScope;
1327
+ }
1328
+ function resolveModulePattern(program, sourceFile, pattern) {
1329
+ if (pattern.indexOf("*") === -1) return [pattern.toLowerCase()];
1330
+ const packageJsonScope = resolveModuleWithPackageInfoFromSourceFile(program, sourceFile);
1273
1331
  const referencedPackages = [];
1274
1332
  for (const statement of sourceFile.statements) {
1275
1333
  if (ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier)) {
@@ -1287,20 +1345,6 @@ function makeTypeScriptUtils(ts) {
1287
1345
  )
1288
1346
  );
1289
1347
  }
1290
- function makeGetModuleSpecifier() {
1291
- if (!(hasProperty(ts, "moduleSpecifiers") && hasProperty(ts.moduleSpecifiers, "getModuleSpecifier") && isFunction2(ts.moduleSpecifiers.getModuleSpecifier))) return;
1292
- const _internal = ts.moduleSpecifiers.getModuleSpecifier;
1293
- return (compilerOptions, importingSourceFile, importingSourceFileName, toFileName, host, options) => {
1294
- return _internal(
1295
- compilerOptions,
1296
- importingSourceFile,
1297
- importingSourceFileName,
1298
- toFileName,
1299
- host,
1300
- options
1301
- );
1302
- };
1303
- }
1304
1348
  function findNodeWithLeadingCommentAtPosition(sourceFile, position) {
1305
1349
  const sourceText = sourceFile.text;
1306
1350
  let result;
@@ -1689,6 +1733,7 @@ function makeTypeScriptUtils(ts) {
1689
1733
  findNodeAtPositionIncludingTrivia,
1690
1734
  parsePackageContentNameAndVersionFromScope,
1691
1735
  resolveModulePattern,
1736
+ resolveModuleWithPackageInfoFromSourceFile,
1692
1737
  findNodeWithLeadingCommentAtPosition,
1693
1738
  getCommentAtPosition,
1694
1739
  getAncestorNodesInRange,
@@ -1703,7 +1748,6 @@ function makeTypeScriptUtils(ts) {
1703
1748
  parseDataForExtendsClassCompletion,
1704
1749
  createEffectGenCallExpressionWithBlock,
1705
1750
  createReturnYieldStarStatement,
1706
- makeGetModuleSpecifier,
1707
1751
  parseAccessedExpressionForCompletion,
1708
1752
  getSourceFileOfNode
1709
1753
  };
@@ -3656,14 +3700,14 @@ var importFromBarrel = createDiagnostic({
3656
3700
  const tsUtils = yield* service(TypeScriptUtils);
3657
3701
  const typeChecker = yield* service(TypeCheckerApi);
3658
3702
  const program = yield* service(TypeScriptProgram);
3703
+ const getModuleSpecifier = makeGetModuleSpecifier(ts);
3704
+ const resolveExternalModuleName = makeResolveExternalModuleName(typeChecker);
3659
3705
  const packageNamesToCheck = flatten(
3660
3706
  languageServicePluginOptions.namespaceImportPackages.map(
3661
3707
  (packageName) => tsUtils.resolveModulePattern(program, sourceFile, packageName)
3662
3708
  )
3663
3709
  );
3664
3710
  const isImportedFromBarrelExport = (element) => {
3665
- const getModuleSpecifier = tsUtils.makeGetModuleSpecifier();
3666
- const resolveExternalModuleName = makeResolveExternalModuleName(typeChecker);
3667
3711
  if (!(getModuleSpecifier && resolveExternalModuleName)) return;
3668
3712
  const importDeclaration = ts.findAncestor(element, (node) => ts.isImportDeclaration(node));
3669
3713
  if (!importDeclaration) return;
@@ -4098,7 +4142,7 @@ var missingEffectError = createDiagnostic({
4098
4142
  // src/diagnostics/missingEffectServiceDependency.ts
4099
4143
  var missingEffectServiceDependency = createDiagnostic({
4100
4144
  name: "missingEffectServiceDependency",
4101
- code: 21,
4145
+ code: 22,
4102
4146
  severity: "off",
4103
4147
  apply: fn("missingEffectServiceDependency.apply")(function* (sourceFile, report) {
4104
4148
  const ts = yield* service(TypeScriptApi);
@@ -4420,6 +4464,96 @@ var multipleEffectProvide = createDiagnostic({
4420
4464
  })
4421
4465
  });
4422
4466
 
4467
+ // src/diagnostics/nonObjectEffectServiceType.ts
4468
+ var nonObjectEffectServiceType = createDiagnostic({
4469
+ name: "nonObjectEffectServiceType",
4470
+ code: 24,
4471
+ severity: "error",
4472
+ apply: fn("nonObjectEffectServiceType.apply")(function* (sourceFile, report) {
4473
+ const ts = yield* service(TypeScriptApi);
4474
+ const typeChecker = yield* service(TypeCheckerApi);
4475
+ const typeCheckerUtils = yield* service(TypeCheckerUtils);
4476
+ const typeParser = yield* service(TypeParser);
4477
+ function isPrimitiveType(type) {
4478
+ return typeCheckerUtils.unrollUnionMembers(type).some(
4479
+ (type2) => !!(type2.flags & ts.TypeFlags.String || type2.flags & ts.TypeFlags.Number || type2.flags & ts.TypeFlags.Boolean || type2.flags & ts.TypeFlags.StringLiteral || type2.flags & ts.TypeFlags.NumberLiteral || type2.flags & ts.TypeFlags.BooleanLiteral || type2.flags & ts.TypeFlags.Undefined || type2.flags & ts.TypeFlags.Null)
4480
+ );
4481
+ }
4482
+ const nodeToVisit = [];
4483
+ const appendNodeToVisit = (node) => {
4484
+ nodeToVisit.push(node);
4485
+ return void 0;
4486
+ };
4487
+ ts.forEachChild(sourceFile, appendNodeToVisit);
4488
+ while (nodeToVisit.length > 0) {
4489
+ const node = nodeToVisit.shift();
4490
+ if (ts.isClassDeclaration(node) && node.name && node.heritageClauses) {
4491
+ const serviceResult = yield* pipe(
4492
+ typeParser.extendsEffectService(node),
4493
+ orElse2(() => void_)
4494
+ );
4495
+ if (serviceResult && serviceResult.options && ts.isObjectLiteralExpression(serviceResult.options)) {
4496
+ const options = serviceResult.options;
4497
+ for (const property of options.properties) {
4498
+ if (!ts.isPropertyAssignment(property) || !ts.isIdentifier(property.name)) {
4499
+ continue;
4500
+ }
4501
+ const propertyName = ts.idText(property.name);
4502
+ const propertyValue = property.initializer;
4503
+ const errorToReport = {
4504
+ location: property.name,
4505
+ messageText: "Effect.Service requires the service type to be an object {} and not a primitive type. \nConsider wrapping the value in an object, or manually using Context.Tag or Effect.Tag if you want to use a primitive instead.",
4506
+ fixes: []
4507
+ };
4508
+ if (propertyName === "succeed") {
4509
+ const valueType = typeChecker.getTypeAtLocation(propertyValue);
4510
+ if (isPrimitiveType(valueType)) {
4511
+ report(errorToReport);
4512
+ }
4513
+ } else if (propertyName === "sync") {
4514
+ const valueType = typeChecker.getTypeAtLocation(propertyValue);
4515
+ const signatures = typeChecker.getSignaturesOfType(valueType, ts.SignatureKind.Call);
4516
+ for (const signature of signatures) {
4517
+ const returnType = typeChecker.getReturnTypeOfSignature(signature);
4518
+ if (isPrimitiveType(returnType)) {
4519
+ report(errorToReport);
4520
+ break;
4521
+ }
4522
+ }
4523
+ } else if (propertyName === "effect" || propertyName === "scoped") {
4524
+ const valueType = typeChecker.getTypeAtLocation(propertyValue);
4525
+ const effectResult = yield* pipe(
4526
+ typeParser.effectType(valueType, propertyValue),
4527
+ orElse2(() => void_)
4528
+ );
4529
+ if (effectResult) {
4530
+ if (isPrimitiveType(effectResult.A)) {
4531
+ report(errorToReport);
4532
+ continue;
4533
+ }
4534
+ } else {
4535
+ const signatures = typeChecker.getSignaturesOfType(valueType, ts.SignatureKind.Call);
4536
+ for (const signature of signatures) {
4537
+ const returnType = typeChecker.getReturnTypeOfSignature(signature);
4538
+ const effectReturnResult = yield* pipe(
4539
+ typeParser.effectType(returnType, propertyValue),
4540
+ orElse2(() => void_)
4541
+ );
4542
+ if (effectReturnResult && isPrimitiveType(effectReturnResult.A)) {
4543
+ report(errorToReport);
4544
+ break;
4545
+ }
4546
+ }
4547
+ }
4548
+ }
4549
+ }
4550
+ }
4551
+ }
4552
+ ts.forEachChild(node, appendNodeToVisit);
4553
+ }
4554
+ })
4555
+ });
4556
+
4423
4557
  // src/refactors/writeTagClassAccessors.ts
4424
4558
  var generate = fn("writeTagClassAccessors.generate")(function* (sourceFile, service2, className, atLocation, involvedMembers) {
4425
4559
  const ts = yield* service(TypeScriptApi);
@@ -5334,7 +5468,8 @@ var diagnostics = [
5334
5468
  multipleEffectProvide,
5335
5469
  outdatedEffectCodegen,
5336
5470
  overriddenSchemaConstructor,
5337
- unsupportedServiceAccessors
5471
+ unsupportedServiceAccessors,
5472
+ nonObjectEffectServiceType
5338
5473
  ];
5339
5474
 
5340
5475
  // src/transform.ts