Skip to content
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

Refactor lib/rules/__tests__/* test files to migrate to ESM #7024

Merged
merged 3 commits into from Jul 3, 2023

Conversation

ybiquitous
Copy link
Member

@ybiquitous ybiquitous commented Jul 2, 2023

Which issue, if any, is this issue related to?

Ref #5291

Is there anything in the PR that needs further explanation?

This change is performed by the following script (though it requires a bit of manual).

node to-esm.mjs lib/rules/*/__tests__/index.js
// to-esm.mjs
import { argv, stdout } from 'node:process';
import { readFile, unlink, writeFile } from 'node:fs/promises';
import { execSync } from 'node:child_process';

async function processFile(file) {
	const ruleFile = file.replace('/__tests__/index.js', '/index.js');

	if ((await readFile(ruleFile, 'utf8')).includes('\tdeprecated: true,')) {
		stdout.write(`Skip deprecated ${file}\n`);

		return;
	}

	let lines = (await readFile(file, 'utf8')).split('\n');

	lines = lines.filter((line) => !line.includes('use strict'));

	lines.forEach((line, i) => {
		line = line.replace(
			"const stripIndent = require('common-tags').stripIndent",
			"const { stripIndent } = require('common-tags')",
		);

		line = line.replace(
			"const { messages, ruleName } = require('..');",
			"import rule from '../index.js'; const { messages, ruleName } = rule;",
		);

		line = line.replace(/const (.+) = require\('(\..+)'\)/, 'import $1 from "$2.js"');
		line = line.replace(/const (.+) = require\('(.+)'\)/, 'import $1 from "$2"');

		lines[i] = line;
	});

	const newFile = file.replace(/\.js$/, '.mjs');
	const newContent = `${lines.join('\n')}\n`;

	await writeFile(newFile, newContent);

	stdout.write(`=> ${newFile}\n`);

	await unlink(file);
}

const files = argv.slice(2);

Promise.all(files.map((f) => processFile(f))).then(() => {
	execSync(`npx prettier --loglevel silent --write lib/rules`, { encoding: 'utf8' });
});

Note that this change does NOT include deprecated rules which were removed on the v16 branch.

This change is performed by the following script (though it requires a bit of manual).

```shell
node to-esm.mjs lib/rules/*/__tests__/index.js
```

```js
// to-esm.mjs
import { argv, stdout } from 'node:process';
import { readFile, unlink, writeFile } from 'node:fs/promises';
import { execSync } from 'node:child_process';

async function processFile(file) {
	const ruleFile = file.replace('/__tests__/index.js', '/index.js');

	if ((await readFile(ruleFile, 'utf8')).includes('\tdeprecated: true,')) {
		stdout.write(`Skip deprecated ${file}\n`);

		return;
	}

	let lines = (await readFile(file, 'utf8')).split('\n');

	lines = lines.filter((line) => !line.includes('use strict'));

	lines.forEach((line, i) => {
		line = line.replace(
			"const stripIndent = require('common-tags').stripIndent",
			"const { stripIndent } = require('common-tags')",
		);

		line = line.replace(
			"const { messages, ruleName } = require('..');",
			"import rule from '../index.js'; const { messages, ruleName } = rule;",
		);

		line = line.replace(/const (.+) = require\('(\..+)'\)/, 'import $1 from "$2.js"');
		line = line.replace(/const (.+) = require\('(.+)'\)/, 'import $1 from "$2"');

		lines[i] = line;
	});

	const newFile = file.replace(/\.js$/, '.mjs');
	const newContent = `${lines.join('\n')}\n`;

	await writeFile(newFile, newContent);

	stdout.write(`=> ${newFile}\n`);

	await unlink(file);
}

const files = argv.slice(2);

Promise.all(files.map((f) => processFile(f))).then(() => {
	execSync(`npx prettier --loglevel silent --write lib/rules`, { encoding: 'utf8' });
});
```
@changeset-bot
Copy link

changeset-bot bot commented Jul 2, 2023

⚠️ No Changeset found

Latest commit: 35b36b6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@ybiquitous ybiquitous marked this pull request as ready for review July 2, 2023 00:39
@ybiquitous ybiquitous marked this pull request as draft July 2, 2023 09:24
@ybiquitous
Copy link
Member Author

I'm investigating the Out of Memory error on CI (coverage):

<--- Last few GCs --->

[1864:0x5d73de0]   213282 ms: Scavenge (reduce) 1843.8 (2088.5) -> 1843.7 (2088.7) MB, 4.0 / 0.0 ms  (average mu = 0.255, current mu = 0.221) allocation failure; 
[1864:0x5d73de0]   213295 ms: Scavenge (reduce) 1844.2 (2088.7) -> 1843.9 (2089.2) MB, 3.6 / 0.0 ms  (average mu = 0.255, current mu = 0.221) allocation failure; 
[1864:0x5d73de0]   213315 ms: Scavenge (reduce) 1844.7 (2089.2) -> 1844.3 (2089.5) MB, 4.3 / 0.0 ms  (average mu = 0.255, current mu = 0.221) allocation failure; 


<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0xb7a940 node::Abort() [node]
 2: 0xa8e823  [node]
 3: 0xd5c990 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xd5cd37 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xf3a435  [node]
 6: 0xf3b338 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]
 7: 0xf4b843  [node]
 8: 0xf4c6b8 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 9: 0xf2701e v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
10: 0xf[283](https://github.com/stylelint/stylelint/actions/runs/5433854986/jobs/9885027327#step:6:284)e7 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
11: 0xf08930 v8::internal::Factory::AllocateRaw(int, v8::internal::AllocationType, v8::internal::AllocationAlignment) [node]
12: 0xf003a4 v8::internal::FactoryBase<v8::internal::Factory>::AllocateRawWithImmortalMap(int, v8::internal::AllocationType, v8::internal::Map, v8::internal::AllocationAlignment) [node]
13: 0xf0[273](https://github.com/stylelint/stylelint/actions/runs/5433854986/jobs/9885027327#step:6:274)8 v8::internal::FactoryBase<v8::internal::Factory>::NewRawTwoByteString(int, v8::internal::AllocationType) [node]
14: 0x11dfc65 v8::internal::String::SlowFlatten(v8::internal::Isolate*, v8::internal::Handle<v8::internal::ConsString>, v8::internal::AllocationType) [node]
15: 0x1084e79 v8::internal::CompilationCacheTable::LookupScript(v8::internal::Handle<v8::internal::CompilationCacheTable>, v8::internal::Handle<v8::internal::String>, v8::internal::LanguageMode, v8::internal::Isolate*) [node]
16: 0xdfefc5 v8::internal::CompilationCacheScript::Lookup(v8::internal::Handle<v8::internal::String>, v8::internal::ScriptDetails const&, v8::internal::LanguageMode) [node]
17: 0xe0a53b  [node]
18: 0xe0b29a v8::internal::Compiler::GetSharedFunctionInfoForScript(v8::internal::Isolate*, v8::internal::Handle<v8::internal::String>, v8::internal::ScriptDetails const&, v8::ScriptCompiler::CompileOptions, v8::ScriptCompiler::NoCacheReason, v8::internal::NativesFlag) [node]
19: 0xd7f99f  [node]
20: 0xd7fc22 v8::ScriptCompiler::CompileUnboundScript(v8::Isolate*, v8::ScriptCompiler::Source*, v8::ScriptCompiler::CompileOptions, v8::ScriptCompiler::NoCacheReason) [node]
21: 0xb6c6f2 node::contextify::ContextifyScript::New(v8::FunctionCallbackInfo<v8::Value> const&) [node]
22: 0xdbba00  [node]
23: 0xdbbfff v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [node]
24: 0x16fb7f9  [node]
Aborted (core dumped)

https://github.com/stylelint/stylelint/actions/runs/5433854986/jobs/9885027327#step:6:255

@ybiquitous
Copy link
Member Author

ybiquitous commented Jul 2, 2023

Switching Jest's coverage provider works! 2724122

I'll extract only the change with another pull request to clarify the PR's purpose.

EDIT: I've just opened PR #7025.

@ybiquitous ybiquitous marked this pull request as ready for review July 2, 2023 09:53
package.json Outdated Show resolved Hide resolved
Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes on main and filtering the deprecated rules out LGTM.

Thank you!

@ybiquitous ybiquitous merged commit b919a0b into main Jul 3, 2023
18 checks passed
@ybiquitous ybiquitous deleted the esm-rules-tests branch July 3, 2023 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants