Skip to content

Commit

Permalink
Migrate to ESM (#2213)
Browse files Browse the repository at this point in the history
* Migrate to ESM

* Fix build script

* Remove unicorn

* Format

* Add `browser` entry

* Remove default export

* Add `./standalone` and `./package.json` entry

* Add `await` to example
  • Loading branch information
fisker authored Aug 18, 2023
1 parent f277152 commit eb900b0
Show file tree
Hide file tree
Showing 23 changed files with 277 additions and 253 deletions.
20 changes: 11 additions & 9 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ extends:
- plugin:prettier-doc/recommended
parserOptions:
ecmaVersion: 2023
sourceType: "module"
plugins:
- import
root: true
env:
es6: true
es2024: true
node: true
jest: true
rules:
eqeqeq: error
curly: error
import/no-extraneous-dependencies:
- error
- devDependencies: ["tests*/**", "scripts/**", "build/**"]
no-else-return: error
no-inner-declarations: error
no-useless-return: error
Expand All @@ -38,13 +36,17 @@ rules:
- error
- never
- exceptRange: true

import/extensions:
- error
- ignorePackages
import/no-extraneous-dependencies:
- error
- devDependencies: ["tests*/**", "scripts/**", "build/**"]
overrides:
- files: "{tests,tests_config}/**/*.js"
- files: "**/*.cjs"
parserOptions:
sourceType: "module"
rules:
# Turned off temporarily since we add a package.json without dependencies in these dirs
import/no-extraneous-dependencies: off
sourceType: "script"
- files: "tests/**/*.js"
globals:
run_spec: true
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ First, grab both standalone scripts from an npm CDN like [unpkg](https://unpkg.c
Then use Prettier with PHP, just like this:

```js
prettier.format(YOUR_CODE, {
await prettier.format(YOUR_CODE, {
plugins: prettierPlugins,
parser: "php",
});
Expand All @@ -153,10 +153,10 @@ Bundlers like webpack, Rollup or browserify automatically recognize how to handl

```js
import prettier from "prettier/standalone";
import phpPlugin from "@prettier/plugin-php/standalone";
import * as prettierPluginPhp from "@prettier/plugin-php/standalone";

prettier.format(YOUR_CODE, {
plugins: [phpPlugin],
await prettier.format(YOUR_CODE, {
plugins: [prettierPluginPhp],
parser: "php",
});
```
Expand Down
10 changes: 6 additions & 4 deletions build/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { resolve } from "path";
import { resolve, dirname } from "path";
import url from "url";

import nodeResolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import alias from "rollup-plugin-alias";
import inject from "rollup-plugin-inject";
import replace from "rollup-plugin-replace";
import babel from "rollup-plugin-babel";
import json from "rollup-plugin-json";
import { terser } from "rollup-plugin-terser";

const __dirname = dirname(url.fileURLToPath(import.meta.url));
const SRC_DIR = resolve(__dirname, "..", "src");
const BUILD_DIR = resolve(__dirname, "..", "build");

Expand Down Expand Up @@ -50,7 +52,7 @@ export default {
compact: false,
presets: [
[
require.resolve("@babel/preset-env"),
"@babel/preset-env",
{
targets: { browsers: [">0.25%", "not ie 11", "not op_mini all"] },
modules: false,
Expand Down
7 changes: 3 additions & 4 deletions build/shims/assert.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use strict";
function assert() {}
assert.strictEqual = function () {};
module.exports = assert;
const assert = new Proxy(() => {}, { get: () => assert });

export default assert;
9 changes: 4 additions & 5 deletions build/shims/buffer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";
const Buffer = {};
Buffer.isBuffer = function () {
return false;
const Buffer = {
isBuffer: () => false,
};
module.exports = Buffer;

export default Buffer;
4 changes: 1 addition & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"use strict";

const ENABLE_COVERAGE = !!process.env.CI;
const RUN_STANDALONE_TESTS = Boolean(process.env.RUN_STANDALONE_TESTS);

module.exports = {
export default {
collectCoverage: ENABLE_COVERAGE,
collectCoverageFrom: [
"<rootDir>/src/**/*.js",
Expand Down
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
"repository": "prettier/prettier-php",
"author": "Lucas Azzola <@azz>",
"license": "MIT",
"main": "src",
"type": "module",
"unpkg": "./standalone.js",
"browser": "./standalone.js",
"exports": {
".": {
"browser": "./standalone.js",
"default": "./src/index.js"
},
"./standalone": "./standalone.js",
"./package.json": "./package.json"
},
"files": [
"src",
"standalone.js"
Expand All @@ -17,6 +27,8 @@
},
"devDependencies": {
"@babel/preset-env": "^7.22.10",
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-node-resolve": "^15.1.0",
"codecov": "3.8.3",
"cross-env": "^7.0.2",
"eslint": "8.47.0",
Expand All @@ -34,10 +46,8 @@
"rollup": "^2.75.7",
"rollup-plugin-alias": "^2.0.0",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^10.0.2",
"rollup-plugin-inject": "^3.0.1",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.1.0",
"rollup-plugin-terser": "^7.0.2",
"strip-ansi": "^6.0.0"
Expand Down
12 changes: 5 additions & 7 deletions src/clean.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";

const util = require("./util");
import { printNumber, normalizeMagicMethodName } from "./util.js";

const ignoredProperties = new Set([
"loc",
Expand Down Expand Up @@ -28,7 +26,7 @@ const ignoredProperties = new Set([
function clean(node, newObj) {
if (node.kind === "string") {
// TODO if options are available in this method, replace with
// newObj.isDoubleQuote = !util.useSingleQuote(node, options);
// newObj.isDoubleQuote = !useSingleQuote(node, options);
delete newObj.isDoubleQuote;
}

Expand Down Expand Up @@ -66,7 +64,7 @@ function clean(node, newObj) {

// Normalize numbers
if (node.kind === "number") {
newObj.value = util.printNumber(node.value);
newObj.value = printNumber(node.value);
}

const statements = ["foreach", "for", "if", "while", "do"];
Expand Down Expand Up @@ -100,7 +98,7 @@ function clean(node, newObj) {
}

if (node.kind === "method" && node.name.kind === "identifier") {
newObj.name.name = util.normalizeMagicMethodName(newObj.name.name);
newObj.name.name = normalizeMagicMethodName(newObj.name.name);
}

if (node.kind === "noop") {
Expand All @@ -110,4 +108,4 @@ function clean(node, newObj) {

clean.ignoredProperties = ignoredProperties;

module.exports = clean;
export default clean;
20 changes: 10 additions & 10 deletions src/comments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
"use strict";
import { util as prettierUtil, doc } from "prettier";
import {
getNextNonSpaceNonCommentCharacterIndex,
isNextLineEmpty,
isPreviousLineEmpty,
isLookupNode,
} from "./util.js";

const {
addLeadingComment,
Expand All @@ -7,15 +13,9 @@ const {
skipNewline,
hasNewline,
hasNewlineInRange,
} = require("prettier").util;
} = prettierUtil;
const { join, indent, hardline, cursor, lineSuffix, breakParent } =
require("prettier").doc.builders;
const {
getNextNonSpaceNonCommentCharacterIndex,
isNextLineEmpty,
isPreviousLineEmpty,
isLookupNode,
} = require("./util");
doc.builders;

/*
Comment functions are meant to inspect various edge cases using given comment nodes,
Expand Down Expand Up @@ -1029,7 +1029,7 @@ function printAllComments(path, print, options, needsSemi) {
);
}

module.exports = {
export {
handleOwnLineComment,
handleEndOfLineComment,
handleRemainingComment,
Expand Down
57 changes: 32 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
"use strict";

const parse = require("./parser");
const print = require("./printer");
const clean = require("./clean");
const options = require("./options");
const comments = require("./comments");
const { join, hardline } = require("prettier").doc.builders;
const { hasPragma, insertPragma } = require("./pragma");
import { doc } from "prettier";
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 {
handleOwnLineComment,
handleEndOfLineComment,
handleRemainingComment,
getCommentChildNodes,
canAttachComment,
isBlockComment,
} from "./comments.js";
import { hasPragma, insertPragma } from "./pragma.js";

const { join, hardline } = doc.builders;

function createLanguage(linguistData, { extend, override }) {
const language = {};
Expand All @@ -30,13 +41,13 @@ function createLanguage(linguistData, { extend, override }) {
}

const languages = [
createLanguage(require("linguist-languages/data/PHP"), {
createLanguage(LINGUIST_LANGUAGES_PHP, {
override: {
parsers: ["php"],
vscodeLanguageIds: ["php"],
},
}),
createLanguage(require("linguist-languages/data/HTML+PHP"), {
createLanguage(LINGUIST_LANGUAGES_HTML_PHP, {
override: {
parsers: ["php"],
vscodeLanguageIds: ["php"],
Expand Down Expand Up @@ -81,13 +92,13 @@ const printers = {
getVisitorKeys,
insertPragma,
massageAstNode: clean,
getCommentChildNodes: comments.getCommentChildNodes,
canAttachComment: comments.canAttachComment,
isBlockComment: comments.isBlockComment,
getCommentChildNodes,
canAttachComment,
isBlockComment,
handleComments: {
ownLine: comments.handleOwnLineComment,
endOfLine: comments.handleEndOfLineComment,
remaining: comments.handleRemainingComment,
ownLine: handleOwnLineComment,
endOfLine: handleEndOfLineComment,
remaining: handleRemainingComment,
},
willPrintOwnComments(path) {
const node = path.getValue();
Expand Down Expand Up @@ -161,12 +172,8 @@ const printers = {
},
};

module.exports = {
languages,
printers,
parsers,
options,
defaultOptions: {
tabWidth: 4,
},
const defaultOptions = {
tabWidth: 4,
};

export { languages, printers, parsers, options, defaultOptions };
4 changes: 4 additions & 0 deletions src/linguist-languages.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";

module.exports.LINGUIST_LANGUAGES_PHP = require("linguist-languages/data/PHP");
module.exports.LINGUIST_LANGUAGES_HTML_PHP = require("linguist-languages/data/HTML+PHP");
2 changes: 0 additions & 2 deletions src/module.js

This file was deleted.

9 changes: 3 additions & 6 deletions src/needs-parens.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"use strict";

const assert = require("assert");

const { getPrecedence, shouldFlatten, isBitwiseOperator } = require("./util");
import assert from "assert";
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.js";

function needsParens(path) {
const parent = path.getParentNode();
Expand Down Expand Up @@ -255,4 +252,4 @@ function needsParens(path) {
return false;
}

module.exports = needsParens;
export default needsParens;
4 changes: 1 addition & 3 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";

const CATEGORY_PHP = "PHP";

module.exports = {
export default {
phpVersion: {
since: "0.13.0",
category: CATEGORY_PHP,
Expand Down
5 changes: 2 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";
const engine = require("php-parser");
import engine from "php-parser";

function parse(text, opts) {
const inMarkdown = opts && opts.parentParser === "markdown";
Expand Down Expand Up @@ -61,4 +60,4 @@ function parse(text, opts) {
return ast;
}

module.exports = parse;
export default parse;
11 changes: 3 additions & 8 deletions src/pragma.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"use strict";

const parse = require("./parser");
const memoize = require("mem");
import memoize from "mem";
import parse from "./parser.js";

const reHasPragma = /@prettier|@format/;

Expand Down Expand Up @@ -91,7 +89,4 @@ function insertPragma(text) {
${after}`;
}

module.exports = {
hasPragma,
insertPragma,
};
export { hasPragma, insertPragma };
Loading

0 comments on commit eb900b0

Please sign in to comment.