Skip to content

Commit

Permalink
fix: make /standalone entry work with require() (#2247)
Browse files Browse the repository at this point in the history
* chore: πŸ€– build cjs output for backward compatibility

This commit adds cjs output to rollup config.

* chore: πŸ€– add mapping for standalone.cjs

* chore: πŸ€– include standalone.cjs as package source

* chore: πŸ€– migrate .js files to .mjs

* chore: πŸ€– disable ESM as default

* refactor: πŸ’‘ delete unnecessary export

* chore: πŸ€– revert build config

* chore: πŸ€– add require to exports

Without this require, vscode-prettier can't resolve php plugin

* refactor: πŸ’‘ fix link in comments

* refactor: πŸ’‘ match both .js and .mjs for jest target

* Remove `require`

* Add a comment

---------

Co-authored-by: fisker Cheung <[email protected]>
  • Loading branch information
shufo and fisker authored Dec 16, 2023
1 parent 977d3f6 commit 69a68d8
Show file tree
Hide file tree
Showing 212 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ overrides:
- files: "**/*.cjs"
parserOptions:
sourceType: "script"
- files: "tests/**/*.js"
- files: "tests/**/*.mjs"
globals:
run_spec: true
- files: "build/*.js"
- files: "build/*.mjs"
parserOptions:
sourceType: module
6 changes: 3 additions & 3 deletions build/rollup.config.js β†’ build/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import terser from "@rollup/plugin-terser";
const getPath = (file) => url.fileURLToPath(new URL(file, import.meta.url));

export default {
input: getPath("../src/index.js"),
input: getPath("../src/index.mjs"),
output: {
file: "standalone.js",
format: "umd",
Expand All @@ -32,10 +32,10 @@ export default {
}),
commonjs(),
alias({
entries: [{ find: "assert", replacement: getPath("./shims/assert.js") }],
entries: [{ find: "assert", replacement: getPath("./shims/assert.mjs") }],
}),
inject({
Buffer: getPath("./shims/buffer.js"),
Buffer: getPath("./shims/buffer.mjs"),
}),
replace({
preventAssignment: true,
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions jest.config.js β†’ jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ const RUN_STANDALONE_TESTS = Boolean(process.env.RUN_STANDALONE_TESTS);
export default {
// collectCoverage: ENABLE_COVERAGE,
collectCoverageFrom: [
"<rootDir>/src/**/*.js",
"<rootDir>/src/**/*.mjs",
"!<rootDir>/node_modules/",
"!<rootDir>/tests_config/",
],
runner: "jest-light-runner",
transform: {},
setupFiles: ["<rootDir>/tests_config/run_spec.js"],
testRegex: "jsfmt\\.spec\\.js$|__tests__/.*\\.js$",
setupFiles: ["<rootDir>/tests_config/run_spec.mjs"],
// Matches `.js` file to prevent files use `.js` extension by mistake, https://github.com/prettier/plugin-php/pull/2247#discussion_r1331847801
testRegex: "jsfmt\\.spec\\.m?js$|__tests__/.*\\.m?js$",
snapshotSerializers: ["jest-snapshot-serializer-raw"],
globals: {
STANDALONE: RUN_STANDALONE_TESTS,
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"repository": "prettier/prettier-php",
"author": "Lucas Azzola <@azz>",
"license": "MIT",
"type": "module",
"unpkg": "./standalone.js",
"browser": "./standalone.js",
"exports": {
Expand Down Expand Up @@ -65,8 +64,8 @@
"test:node": "jest",
"test:standalone": "yarn run build && cross-env RUN_STANDALONE_TESTS=true yarn jest",
"prepublishOnly": "yarn test",
"prettier": "prettier --plugin=src/index.js --parser=php",
"build": "rollup --config build/rollup.config.js",
"prettier": "prettier --plugin=src/index.mjs --parser=php",
"build": "rollup --config build/rollup.config.mjs",
"debug": "node --inspect-brk node_modules/.bin/jest --runInBand"
},
"c8": {
Expand Down
2 changes: 1 addition & 1 deletion src/clean.js β†’ src/clean.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { printNumber, normalizeMagicMethodName } from "./util.js";
import { printNumber, normalizeMagicMethodName } from "./util.mjs";

const ignoredProperties = new Set([
"loc",
Expand Down
6 changes: 3 additions & 3 deletions src/comments.js β†’ src/comments.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { util as prettierUtil, doc } from "prettier";
import { isLookupNode } from "./util.js";
import { locStart, locEnd } from "./loc.js";
import { isLookupNode } from "./util.mjs";
import { locStart, locEnd } from "./loc.mjs";

const {
addLeadingComment,
Expand Down Expand Up @@ -830,7 +830,7 @@ function printComment(path, options) {
return options.printer.printComment(path, options);
}

// https://github.com/prettier/prettier/blob/master/src/main/comments.js#L440
// https://github.com/prettier/prettier/blob/6bff776efd0951b7812818d02d1cae3fda184737/src/main/comments/print.js#L119
function printDanglingComments(path, options, sameIndent, filter) {
const parts = [];
const node = path.getValue();
Expand Down
14 changes: 7 additions & 7 deletions src/index.js β†’ src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import {
LINGUIST_LANGUAGES_PHP,
LINGUIST_LANGUAGES_HTML_PHP,
} from "./linguist-languages.cjs";
import parse from "./parser.js";
import print from "./printer.js";
import clean from "./clean.js";
import options from "./options.js";
import parse from "./parser.mjs";
import print from "./printer.mjs";
import clean from "./clean.mjs";
import options from "./options.mjs";
import {
handleOwnLineComment,
handleEndOfLineComment,
handleRemainingComment,
getCommentChildNodes,
canAttachComment,
isBlockComment,
} from "./comments.js";
import { hasPragma, insertPragma } from "./pragma.js";
import { locStart, locEnd } from "./loc.js";
} from "./comments.mjs";
import { hasPragma, insertPragma } from "./pragma.mjs";
import { locStart, locEnd } from "./loc.mjs";

const { join, hardline } = doc.builders;

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/needs-parens.js β†’ src/needs-parens.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.js";
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.mjs";

function needsParens(path) {
const { parent } = path;
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/pragma.js β†’ src/pragma.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import memoize from "mem";
import parse from "./parser.js";
import parse from "./parser.mjs";

const reHasPragma = /@prettier|@format/;

Expand Down
10 changes: 5 additions & 5 deletions src/printer.js β†’ src/printer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
printComments,
isBlockComment,
hasLeadingOwnLineComment,
} from "./comments.js";
import pathNeedsParens from "./needs-parens.js";
import { locStart, locEnd } from "./loc.js";
} from "./comments.mjs";
import pathNeedsParens from "./needs-parens.mjs";
import { locStart, locEnd } from "./loc.mjs";

import {
getLast,
Expand All @@ -36,7 +36,7 @@ import {
getAncestorNode,
isReferenceLikeNode,
normalizeMagicMethodName,
} from "./util.js";
} from "./util.mjs";

const {
breakParent,
Expand Down Expand Up @@ -1753,7 +1753,7 @@ function printNode(path, options, print) {
if (node.value) {
return group([
name,
// see handleFunctionParameter() in ./comments.js - since there's
// see handleFunctionParameter() in ./comments.mjs - since there's
// no node to attach comments that fall in between the parameter name
// and value, we store them as dangling comments
hasDanglingComments(node) ? " " : "",
Expand Down
2 changes: 1 addition & 1 deletion src/util.js β†’ src/util.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { util as prettierUtil } from "prettier";
import { locStart } from "./loc.js";
import { locStart } from "./loc.mjs";

const { hasNewline, skipEverythingButNewLine, skipNewline } = prettierUtil;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prettier, plugin } from "../../tests_config/get_engine.js";
import { prettier, plugin } from "../../tests_config/get_engine.mjs";
import stripAnsi from "strip-ansi";

async function raiseSyntaxError() {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import url from "url";
import { createRequire } from "module";
import prettierModule from "prettier";
import prettierStandalone from "prettier/standalone";
import * as prettierPluginPhp from "../src/index.js";
import * as prettierPluginPhp from "../src/index.mjs";

const require = createRequire(import.meta.url);

Expand Down
4 changes: 2 additions & 2 deletions tests_config/run_spec.js β†’ tests_config/run_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs";
import path from "path";
import url from "url";
import { wrap as raw } from "jest-snapshot-serializer-raw";
import { prettier, plugin } from "./get_engine.js";
import { prettier, plugin } from "./get_engine.mjs";

const { AST_COMPARE, TEST_CRLF } = process.env;

Expand All @@ -29,7 +29,7 @@ global.run_spec = (importMeta, parsers, options) => {
path.extname(basename) === ".snap" ||
!fs.lstatSync(filename).isFile() ||
basename[0] === "." ||
basename === "jsfmt.spec.js"
basename === "jsfmt.spec.mjs"
) {
return;
}
Expand Down

0 comments on commit 69a68d8

Please sign in to comment.