Skip to content

build: allow for some schematics code to be linted #23006

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 1 commit into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('ng update typescript program module resolution', () => {
'migration-v6', MIGRATION_PATH, []);

writeFile('/node_modules/some-other-module/package.json', `{}`);
writeFile('/node_modules/some-other-module/styles.css', '')
writeFile('/node_modules/some-other-module/styles.css', '');

// We add an import to a non-existent sub-path of `some-other-module/styles`. The TypeScript
// module resolution logic could try various sub-paths. This previously resulted in an error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ describe('class inheritance misc checks', () => {

const {logOutput} = await runFixers();

// tslint:disable:max-line-length
expect(logOutput).toMatch(
/Found class "WithoutLabelProp".*extends "MatFormFieldControl.*must define "shouldLabelFloat"/);
expect(logOutput).not.toMatch(
/Found class "WithLabelProp".*extends "MatFormFieldControl".*must define "shouldLabelFloat"/);
// tslint:enable:max-line-length
});
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {UnitTestTree} from '@angular-devkit/schematics/testing';
import {createTestCaseSetup} from '@angular/cdk/schematics/testing';
import {migrateFileContent} from '@angular/material/schematics/ng-update/migrations/theming-api-v12/migration';
import {
migrateFileContent
} from '@angular/material/schematics/ng-update/migrations/theming-api-v12/migration';
import {join} from 'path';
import {MIGRATION_PATH} from '../../../../paths';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('v9 HammerJS removal', () => {
});

function appendContent(filePath: string, text: string) {
writeFile(filePath, text + tree.readContent(filePath))
writeFile(filePath, text + tree.readContent(filePath));
}

function writeHammerTypes() {
Expand All @@ -38,6 +38,10 @@ describe('v9 HammerJS removal', () => {
`);
}

function getDependencyVersion(name: string): string | undefined {
return (JSON.parse(tree.readContent('/package.json')) as PackageJson).dependencies[name];
}

it('should not throw if project tsconfig does not have explicit root file names', async () => {
// Generates a second project in the workspace. This is necessary to ensure that the
// migration runs logic to determine the correct workspace project.
Expand All @@ -60,12 +64,11 @@ describe('v9 HammerJS removal', () => {
it('should remove hammerjs from "package.json" file', async () => {
addPackageToPackageJson(tree, 'hammerjs', '0.0.0');

expect((JSON.parse(tree.readContent('/package.json')) as PackageJson).dependencies['hammerjs']).toBe('0.0.0');
expect(getDependencyVersion('hammerjs')).toBe('0.0.0');

await runMigration();

expect((JSON.parse(tree.readContent('/package.json')) as PackageJson).dependencies['hammerjs'])
.toBe(undefined);
expect(getDependencyVersion('hammerjs')).toBeUndefined();

// expect that there is a "node-package" install task. The task is
// needed to update the lock file.
Expand Down Expand Up @@ -126,7 +129,10 @@ describe('v9 HammerJS removal', () => {
it('should remove references to HammerModule', async () => {
writeFile('/projects/cdk-testing/src/test.module.ts', dedent`
import {NgModule} from '@angular/core';
import {HAMMER_GESTURE_CONFIG, HammerModule} from '@angular/platform-browser'; // some comment
import {
HAMMER_GESTURE_CONFIG,
HammerModule
} from '@angular/platform-browser'; // some comment
import {GestureConfig} from '@angular/material/core';

@NgModule({
Expand Down Expand Up @@ -161,7 +167,9 @@ describe('v9 HammerJS removal', () => {
it('should remove references to gesture config if imports are aliased', async () => {
writeFile('/projects/cdk-testing/src/test.module.ts', dedent`
import {NgModule} from '@angular/core';
import {HAMMER_GESTURE_CONFIG as configToken} from '@angular/platform-browser'; // some comment
import {
HAMMER_GESTURE_CONFIG as configToken
} from '@angular/platform-browser'; // some comment
import {GestureConfig as gestureConfig} from '@angular/material/core';

@NgModule({
Expand Down Expand Up @@ -279,6 +287,7 @@ describe('v9 HammerJS removal', () => {
});

it('should remove import scripts in project index files if found', async () => {
// tslint:disable:max-line-length
writeFile('/projects/cdk-testing/src/index.html', dedent`
<!doctype html>
<html lang="en">
Expand Down Expand Up @@ -312,6 +321,7 @@ describe('v9 HammerJS removal', () => {
</body>
<script src="some-other-script.js"></script>
</html>`);
// tslint:enable:max-line-length
});
});

Expand Down Expand Up @@ -661,6 +671,8 @@ describe('v9 HammerJS removal', () => {

expect(tree.readContent('/projects/cdk-testing/src/main.ts')).toContain(`import 'hammerjs';`);
expect(tree.exists('/projects/cdk-testing/src/gesture-config.ts')).toBe(true);

// tslint:disable:max-line-length
expect(tree.readContent('/projects/cdk-testing/src/app/app.module.ts')).toContain(dedent`\
import { NgModule } from '@angular/core';
import { BrowserModule, HAMMER_GESTURE_CONFIG, HammerModule } from '@angular/platform-browser';
Expand All @@ -680,6 +692,7 @@ describe('v9 HammerJS removal', () => {
bootstrap: [AppComponent]
})
export class AppModule { }`);
// tslint:enable:max-line-length
});

it('should add gesture config provider to app module if module is referenced through ' +
Expand Down Expand Up @@ -710,6 +723,8 @@ describe('v9 HammerJS removal', () => {

expect(tree.readContent('/projects/cdk-testing/src/main.ts')).toContain(`import 'hammerjs';`);
expect(tree.exists('/projects/cdk-testing/src/gesture-config.ts')).toBe(true);

// tslint:disable:max-line-length
expect(tree.readContent('/projects/cdk-testing/src/app/app.module.ts')).toContain(dedent`\
import { NgModule } from '@angular/core';
import { BrowserModule, HAMMER_GESTURE_CONFIG, HammerModule } from '@angular/platform-browser';
Expand All @@ -729,6 +744,7 @@ describe('v9 HammerJS removal', () => {
bootstrap: [AppComponent]
})
export class AppModule { }`);
// tslint:enable:max-line-length
});

it('should not add gesture config provider multiple times if already provided', async () => {
Expand Down Expand Up @@ -792,6 +808,7 @@ describe('v9 HammerJS removal', () => {

expect(tree.readContent('/projects/cdk-testing/src/main.ts')).toContain(`import 'hammerjs';`);
expect(tree.exists('/projects/cdk-testing/src/gesture-config.ts')).toBe(true);
// tslint:disable:max-line-length
expect(tree.readContent('/projects/cdk-testing/src/app/app.module.ts')).toContain(dedent`
import { HammerModule as myHammerModule, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';
import {NgModule} from '@angular/core';
Expand All @@ -802,13 +819,14 @@ describe('v9 HammerJS removal', () => {
providers: [{ provide: HAMMER_GESTURE_CONFIG, useClass: GestureConfig }],
})
export class AppModule {}`);
// tslint:enable:max-line-length
});
});

it('should not remove hammerjs if test target compilation scope does not contain hammerjs usage',
async () => {
addPackageToPackageJson(tree, 'hammerjs', '0.0.0');
expect((JSON.parse(tree.readContent('/package.json')) as PackageJson).dependencies['hammerjs']).toBe('0.0.0');
expect(getDependencyVersion('hammerjs')).toBe('0.0.0');

// we simulate a case where a component does not have any tests for. In that case,
// the test target compilation scope does not include "test.component.ts" and the
Expand All @@ -825,14 +843,14 @@ describe('v9 HammerJS removal', () => {

await runMigration();

expect((JSON.parse(tree.readContent('/package.json')) as PackageJson).dependencies['hammerjs']).toBe('0.0.0');
expect(getDependencyVersion('hammerjs')).toBe('0.0.0');
});

it('should not remove hammerjs from "package.json" file if used in one project while ' +
'unused in other project', async () => {
addPackageToPackageJson(tree, 'hammerjs', '0.0.0');

expect((JSON.parse(tree.readContent('/package.json')) as PackageJson).dependencies['hammerjs']).toBe('0.0.0');
expect(getDependencyVersion('hammerjs')).toBe('0.0.0');

await runner.runExternalSchematicAsync('@schematics/angular', 'application',
{name: 'second-project'}, tree).toPromise();
Expand All @@ -845,8 +863,7 @@ describe('v9 HammerJS removal', () => {
await runMigration();

expect(runner.tasks.some(t => t.name === 'node-package')).toBe(false);
expect((JSON.parse(tree.readContent('/package.json')) as PackageJson).dependencies['hammerjs'])
.toBe('0.0.0');
expect(getDependencyVersion('hammerjs')).toBe('0.0.0');
});

describe('with custom gesture config', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {createTestCaseSetup, readFileContent, resolveBazelPath} from '@angular/cdk/schematics/testing';
import {
createTestCaseSetup,
readFileContent,
resolveBazelPath,
} from '@angular/cdk/schematics/testing';
import {MIGRATION_PATH} from '../../../../paths';

describe('v9 material imports', () => {
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
// Exclude schematic template files and test cases which aren't valid TS files.
"src/material/schematics/ng-generate/*/files/**/*",
"src/cdk/schematics/ng-generate/*/files/**/*",
"src/cdk/schematics/ng-update/test-cases/**/*",
"src/material/schematics/ng-update/test-cases/**/*"
"src/cdk/schematics/ng-update/test-cases/**/*_input.ts",
"src/cdk/schematics/ng-update/test-cases/**/*_expected_output.ts",
"src/material/schematics/ng-update/test-cases/**/*_input.ts",
"src/material/schematics/ng-update/test-cases/**/*_expected_output.ts"
]
}
6 changes: 4 additions & 2 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@
// Exclude schematic template files and test cases that can't be linted.
"src/material/schematics/ng-generate/*/files/**/*",
"src/cdk/schematics/ng-generate/*/files/**/*",
"src/cdk/schematics/ng-update/test-cases/**/*",
"src/material/schematics/ng-update/test-cases/**/*"
"src/cdk/schematics/ng-update/test-cases/**/*_input.ts",
"src/cdk/schematics/ng-update/test-cases/**/*_expected_output.ts",
"src/material/schematics/ng-update/test-cases/**/*_input.ts",
"src/material/schematics/ng-update/test-cases/**/*_expected_output.ts"
]
}
}