From eb81121261f64e03e8e94060b2cd3d5005d5ecc3 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 29 Apr 2021 08:01:19 +0200 Subject: [PATCH] test: use ng-dev to detect circular dependencies With this change we use `ng-dev ts-circular-deps` to detect circular dependencies so that all repos in Angular org use the same tool. --- goldens/circular-deps/packages.json | 1 + package.json | 5 +- packages/circular-deps-test.conf.js | 71 +++++++++++++++++++++++++++++ tslint.json | 1 - yarn.lock | 5 -- 5 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 goldens/circular-deps/packages.json create mode 100644 packages/circular-deps-test.conf.js diff --git a/goldens/circular-deps/packages.json b/goldens/circular-deps/packages.json new file mode 100644 index 0000000000..fe51488c70 --- /dev/null +++ b/goldens/circular-deps/packages.json @@ -0,0 +1 @@ +[] diff --git a/package.json b/package.json index c4861438d6..e039c61523 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,9 @@ "postinstall": "yarn webdriver-update && yarn ngcc && yarn husky install", "//webdriver-update-README": "ChromeDriver version must match Puppeteer Chromium version, see https://github.com/GoogleChrome/puppeteer/releases http://chromedriver.chromium.org/downloads", "webdriver-update": "webdriver-manager update --standalone false --gecko false --versions.chrome 91.0.4472.19", - "ngcc": "ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points" + "ngcc": "ngcc --properties es2015 browser module main --first-only --create-ivy-entry-points", + "ts-circular-deps:check": "ng-dev ts-circular-deps check --config ./packages/circular-deps-test.conf.js", + "ts-circular-deps:approve": "ng-dev ts-circular-deps approve --config ./packages/circular-deps-test.conf.js" }, "repository": { "type": "git", @@ -218,7 +220,6 @@ "ts-node": "^9.0.0", "tslib": "^2.0.0", "tslint": "^6.1.3", - "tslint-no-circular-imports": "^0.7.0", "typescript": "4.2.4", "verdaccio": "5.0.4", "verdaccio-auth-memory": "^10.0.0", diff --git a/packages/circular-deps-test.conf.js b/packages/circular-deps-test.conf.js new file mode 100644 index 0000000000..b240faaaca --- /dev/null +++ b/packages/circular-deps-test.conf.js @@ -0,0 +1,71 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +const path = require('path'); +const fs = require('fs'); + +module.exports = { + baseDir: '../', + goldenFile: '../goldens/circular-deps/packages.json', + glob: './**/*.ts', + // Command that will be displayed if the golden needs to be updated. + approveCommand: 'yarn ts-circular-deps:approve', + resolveModule: resolveModule, +}; + +/** + * Custom module resolver that maps specifiers for local packages folder. + * This ensures that cross package/entry-point dependencies can be detected. + */ + +const LOCAL_MAPPINGS = [ + ['@angular-devkit/build-angular', 'angular_devkit/build_angular'], + ['@angular-devkit/architect', 'angular_devkit/architect'], + ['@angular-devkit/architect-cli', 'angular_devkit/architect_cli'], + ['@angular-devkit/benchmark', 'angular_devkit/benchmark'], + ['@angular-devkit/build-optimizer', 'angular_devkit/build_optimizer'], + ['@angular-devkit/build-webpack', 'angular_devkit/build_webpack'], + ['@angular-devkit/core', 'angular_devkit/core'], + ['@angular-devkit/schematics', 'angular_devkit/schematics'], + ['@angular-devkit/schematics_cli', 'angular_devkit/schematics_cli'], + ['@angular/cli', 'angular/cli'], + ['@schematics/angular', 'schematics/angular'], + ['@ngtools/webpack', 'ngtools/webpack'], +]; + +function resolveModule(specifier) { + let localSpecifierPath; + + for (const [key, value] of LOCAL_MAPPINGS) { + if (specifier.startsWith(key)) { + localSpecifierPath = path.join(__dirname, specifier.replace(key, value)); + break; + } + } + + if (!localSpecifierPath) { + return null; + } + + const lookups = [ + localSpecifierPath, + `${localSpecifierPath}.ts`, + path.join(localSpecifierPath, 'src/index.ts'), + path.join(localSpecifierPath, 'index.ts'), + ]; + + for (const lookup of lookups) { + try { + if (fs.statSync(lookup).isFile()) { + return lookup; + } + } catch {} + } + + return null; +} diff --git a/tslint.json b/tslint.json index 1e35f6d48f..60fed7fd0a 100644 --- a/tslint.json +++ b/tslint.json @@ -2,7 +2,6 @@ // Consider using no-identical-functions and disabling in faulty tests // Consider using cognitive-complexity and refactor // Consider using no-useless-cast but it's annoying - "extends": ["tslint-no-circular-imports"], "linterOptions": { "format": "codeFrame", "exclude": ["dist-schema/**", "**/third_party/**"] diff --git a/yarn.lock b/yarn.lock index dccf1f2310..2f51656656 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10861,11 +10861,6 @@ tslib@^1.10.0, tslib@^1.13.0, tslib@^1.8.1, tslib@^1.9.0: resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== -tslint-no-circular-imports@^0.7.0: - version "0.7.0" - resolved "https://registry.yarnpkg.com/tslint-no-circular-imports/-/tslint-no-circular-imports-0.7.0.tgz#9df0a15654d66b172e0b7843eed073fa5ae99b5f" - integrity sha512-k3wxpeMC4ef40UbpfBVHEHIzKfNZq5/SCtAO1YjGsaNTklo+K53/TWLrym+poA65RJFDiYgYNWvkeIIkJNA0Vw== - tslint@^6.1.3: version "6.1.3" resolved "https://registry.yarnpkg.com/tslint/-/tslint-6.1.3.tgz#5c23b2eccc32487d5523bd3a470e9aa31789d904"