Repository navigation
Tooling: Add .mts and .cts support #83071
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
Changes from all commits
f097763
e7c3340
7642c05
dd10191
78c9327
233f0e6
d668c6f
433253f
78314b4
ffe2963
87fa779
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ on: | |
| - '**.js' | ||
| - '**.ts' | ||
| - '**.tsx' | ||
| - '**.mts' | ||
| - '**.cts' | ||
| - '**.cjs' | ||
| - '**.mjs' | ||
| - '**.css' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,15 +13,21 @@ const config = [ | |
| ignores: [ '**/build/**', '**/node_modules/**', '**/vendor/**' ], | ||
| }, | ||
|
|
||
| /* | ||
| * ESLint's default file discovery covers only `.js`, `.mjs` and `.cjs`. | ||
| * Every other extension has to be named before any config below applies. | ||
| */ | ||
| { files: [ '**/*.jsx', '**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts' ] }, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that ESLint was skipping
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
|
|
||
| // Base recommended config from @wordpress/eslint-plugin. | ||
| ...wpPlugin.configs.recommended, | ||
|
|
||
| // Unit test overrides. | ||
| ...wpPlugin.configs[ 'test-unit' ].map( ( c ) => ( { | ||
| ...c, | ||
| files: [ | ||
| '**/@(test|__tests__)/**/*.{js,jsx,ts,tsx,mjs,cjs}', | ||
| '**/*.@(test|spec).{js,jsx,ts,tsx,mjs,cjs}', | ||
| '**/@(test|__tests__)/**/*.{js,jsx,ts,tsx,mjs,cjs,mts,cts}', | ||
| '**/*.@(test|spec).{js,jsx,ts,tsx,mjs,cjs,mts,cts}', | ||
| ], | ||
| } ) ), | ||
| ]; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { execFileSync } from 'node:child_process'; | ||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { rimrafSync } from 'rimraf'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| const currentDirectory = path.dirname( fileURLToPath( import.meta.url ) ); | ||
| const scriptPath = path.resolve( currentDirectory, '..', 'format.js' ); | ||
|
|
||
| /* | ||
| * One source file per module extension the script globs for. An extension the | ||
| * glob misses leaves its file unformatted instead of failing the run. | ||
| */ | ||
| const FIXTURES = { | ||
| 'entry.js': { | ||
| input: 'const value = {a:1};\nexport default value\n', | ||
| expected: 'const value = { a: 1 };\nexport default value;\n', | ||
| }, | ||
| 'entry.jsx': { | ||
| input: "const element = <div className='a' />;\nexport default element\n", | ||
| expected: | ||
| 'const element = <div className="a" />;\nexport default element;\n', | ||
| }, | ||
| 'entry.cjs': { | ||
| input: 'const value = {a:1};\nmodule.exports = value\n', | ||
| expected: 'const value = { a: 1 };\nmodule.exports = value;\n', | ||
| }, | ||
| 'entry.mjs': { | ||
| input: 'const value = {a:1};\nexport default value\n', | ||
| expected: 'const value = { a: 1 };\nexport default value;\n', | ||
| }, | ||
| 'entry.ts': { | ||
| input: 'const value: {a:number} = {a:1};\nexport default value\n', | ||
| expected: | ||
| 'const value: { a: number } = { a: 1 };\nexport default value;\n', | ||
| }, | ||
| 'entry.tsx': { | ||
| input: "const element = <div className='a' />;\nexport default element\n", | ||
| expected: | ||
| 'const element = <div className="a" />;\nexport default element;\n', | ||
| }, | ||
| 'entry.cts': { | ||
| input: 'const value: {a:number} = {a:1};\nexport = value\n', | ||
| expected: 'const value: { a: number } = { a: 1 };\nexport = value;\n', | ||
| }, | ||
| 'entry.mts': { | ||
| input: 'const value: {a:number} = {a:1};\nexport default value\n', | ||
| expected: | ||
| 'const value: { a: number } = { a: 1 };\nexport default value;\n', | ||
| }, | ||
| }; | ||
|
|
||
| describe( 'format script', () => { | ||
| let workingDirectory; | ||
|
|
||
| beforeEach( () => { | ||
| workingDirectory = fs.mkdtempSync( | ||
| path.join( os.tmpdir(), 'wp-scripts-format-' ) | ||
| ); | ||
| // The script resolves the project root from the nearest `package.json`. | ||
| fs.writeFileSync( | ||
| path.join( workingDirectory, 'package.json' ), | ||
| '{ "name": "format-fixture", "version": "0.0.0" }\n' | ||
| ); | ||
| for ( const [ fileName, { input } ] of Object.entries( FIXTURES ) ) { | ||
| fs.writeFileSync( path.join( workingDirectory, fileName ), input ); | ||
| } | ||
| } ); | ||
|
|
||
| afterEach( () => { | ||
| rimrafSync( workingDirectory ); | ||
| } ); | ||
|
|
||
| it( 'formats every supported module extension in a directory', () => { | ||
| execFileSync( process.execPath, [ scriptPath, '.' ], { | ||
| cwd: workingDirectory, | ||
| } ); | ||
|
|
||
| const formatted = Object.fromEntries( | ||
| Object.keys( FIXTURES ).map( ( fileName ) => [ | ||
| fileName, | ||
| fs.readFileSync( | ||
| path.join( workingDirectory, fileName ), | ||
| 'utf8' | ||
| ), | ||
| ] ) | ||
| ); | ||
|
|
||
| expect( formatted ).toEqual( | ||
| Object.fromEntries( | ||
| Object.entries( FIXTURES ).map( ( [ fileName, fixture ] ) => [ | ||
| fileName, | ||
| fixture.expected, | ||
| ] ) | ||
| ) | ||
| ); | ||
| } ); | ||
| } ); |
Uh oh!
There was an error while loading. Please reload this page.