@prettier/plugin-php
Advanced tools
Comparing version 0.20.1 to 0.21.0
{ | ||
"name": "@prettier/plugin-php", | ||
"version": "0.20.1", | ||
"version": "0.21.0", | ||
"description": "Prettier PHP Plugin", | ||
@@ -29,26 +29,25 @@ "repository": "prettier/prettier-php", | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.22.10", | ||
"@babel/preset-env": "^7.22.14", | ||
"@rollup/plugin-alias": "^5.0.0", | ||
"@rollup/plugin-babel": "^6.0.3", | ||
"@rollup/plugin-commonjs": "^25.0.4", | ||
"@rollup/plugin-node-resolve": "^15.1.0", | ||
"codecov": "3.8.3", | ||
"@rollup/plugin-inject": "^5.0.3", | ||
"@rollup/plugin-json": "^6.0.0", | ||
"@rollup/plugin-node-resolve": "^15.2.1", | ||
"@rollup/plugin-replace": "^5.0.2", | ||
"@rollup/plugin-terser": "^0.4.3", | ||
"c8": "^8.0.1", | ||
"cross-env": "^7.0.2", | ||
"eslint": "8.47.0", | ||
"eslint": "8.51.0", | ||
"eslint-config-prettier": "9.0.0", | ||
"eslint-plugin-import": "2.28.0", | ||
"eslint-plugin-jest": "27.2.3", | ||
"eslint-plugin-prettier": "5.0.0", | ||
"eslint-plugin-import": "2.28.1", | ||
"eslint-plugin-jest": "27.4.2", | ||
"eslint-plugin-prettier": "5.0.1", | ||
"eslint-plugin-prettier-doc": "^1.1.0", | ||
"jest": "29.6.2", | ||
"jest-environment-jsdom": "29.6.2", | ||
"jest-light-runner": "^0.5.0", | ||
"jest-runner-eslint": "2.1.0", | ||
"jest-snapshot-serializer-raw": "^1.1.0", | ||
"jest": "29.7.0", | ||
"jest-environment-jsdom": "29.7.0", | ||
"jest-light-runner": "^0.5.1", | ||
"jest-snapshot-serializer-raw": "^2.0.0", | ||
"prettier": "^3.0.1", | ||
"rollup": "^2.75.7", | ||
"rollup-plugin-alias": "^2.0.0", | ||
"rollup-plugin-babel": "^4.3.2", | ||
"rollup-plugin-inject": "^3.0.1", | ||
"rollup-plugin-json": "^4.0.0", | ||
"rollup-plugin-replace": "^2.1.0", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"rollup": "^3.28.1", | ||
"strip-ansi": "^7.1.0" | ||
@@ -60,10 +59,26 @@ }, | ||
"scripts": { | ||
"lint": "yarn lint:eslint && yarn lint:prettier", | ||
"lint:eslint": "eslint .", | ||
"lint:prettier": "prettier . --check", | ||
"fix": "yarn fix:eslint && yarn fix:prettier", | ||
"fix:eslint": "eslint . --fix", | ||
"fix:prettier": "prettier . --write", | ||
"test": "yarn test:node && yarn test:standalone", | ||
"test:node": "jest", | ||
"test:standalone": "yarn run build-standalone && cross-env RUN_STANDALONE_TESTS=true yarn 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-standalone": "rollup -c build/rollup.config.js", | ||
"build": "rollup --config build/rollup.config.js", | ||
"debug": "node --inspect-brk node_modules/.bin/jest --runInBand" | ||
}, | ||
"c8": { | ||
"reporter": [ | ||
"lcov", | ||
"text" | ||
], | ||
"all": true, | ||
"include": [ | ||
"src/**" | ||
] | ||
} | ||
} |
@@ -100,2 +100,12 @@ <div align="center"> | ||
### Activate the plugin | ||
Create or modify your [prettier configuration file](https://prettier.io/docs/en/configuration) to activate the plugin: | ||
```json | ||
{ | ||
"plugins": ["@prettier/plugin-php"] | ||
} | ||
``` | ||
## Use | ||
@@ -238,3 +248,3 @@ | ||
"parser": "php", | ||
"plugins": ["@prettier/plugin-php"], | ||
"plugins": ["@prettier/plugin-php"] | ||
} | ||
@@ -241,0 +251,0 @@ ``` |
import { util as prettierUtil, doc } from "prettier"; | ||
import { | ||
getNextNonSpaceNonCommentCharacterIndex, | ||
isNextLineEmpty, | ||
isPreviousLineEmpty, | ||
isLookupNode, | ||
} from "./util.js"; | ||
import { isLookupNode } from "./util.js"; | ||
import { locStart, locEnd } from "./loc.js"; | ||
@@ -16,2 +12,5 @@ const { | ||
hasNewlineInRange, | ||
getNextNonSpaceNonCommentCharacterIndex, | ||
isNextLineEmpty, | ||
isPreviousLineEmpty, | ||
} = prettierUtil; | ||
@@ -278,9 +277,8 @@ const { join, indent, hardline, cursor, lineSuffix, breakParent } = | ||
followingNode, | ||
comment, | ||
options | ||
comment | ||
/* options */ | ||
) { | ||
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex( | ||
text, | ||
comment, | ||
options | ||
locEnd(comment) | ||
); | ||
@@ -334,4 +332,4 @@ const nextCharacter = text.charAt(nextCharIndex); | ||
followingNode, | ||
comment, | ||
options | ||
comment | ||
/* options */ | ||
) { | ||
@@ -344,4 +342,3 @@ if (!enclosingNode || enclosingNode.kind !== "if" || !followingNode) { | ||
text, | ||
comment, | ||
options | ||
locEnd(comment) | ||
); | ||
@@ -387,12 +384,8 @@ const nextCharacter = text.charAt(nextCharIndex); | ||
comment, | ||
text, | ||
options | ||
text | ||
/* options */ | ||
) { | ||
const isSameLineAsPrecedingNode = | ||
precedingNode && | ||
!hasNewlineInRange( | ||
text, | ||
options.locEnd(precedingNode), | ||
options.locStart(comment) | ||
); | ||
!hasNewlineInRange(text, locEnd(precedingNode), locStart(comment)); | ||
@@ -515,3 +508,9 @@ if ( | ||
function handleFunction(text, enclosingNode, followingNode, comment, options) { | ||
function handleFunction( | ||
text, | ||
enclosingNode, | ||
followingNode, | ||
comment | ||
/* options */ | ||
) { | ||
if ( | ||
@@ -527,4 +526,4 @@ enclosingNode && | ||
argumentsLocEnd = | ||
options.locEnd(enclosingNode.arguments[i]) > argumentsLocEnd | ||
? options.locEnd(enclosingNode.arguments[i]) | ||
locEnd(enclosingNode.arguments[i]) > argumentsLocEnd | ||
? locEnd(enclosingNode.arguments[i]) | ||
: argumentsLocEnd; | ||
@@ -534,8 +533,7 @@ } | ||
enclosingNode.body && | ||
options.locStart(comment) > argumentsLocEnd && | ||
options.locEnd(comment) < options.locStart(enclosingNode.body); | ||
locStart(comment) > argumentsLocEnd && | ||
locEnd(comment) < locStart(enclosingNode.body); | ||
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex( | ||
text, | ||
comment, | ||
options | ||
locEnd(comment) | ||
); | ||
@@ -549,3 +547,3 @@ // we additionally need to check if this isn't a trailing argument comment, | ||
) { | ||
if (options.locEnd(comment) < options.locStart(enclosingNode.type)) { | ||
if (locEnd(comment) < locStart(enclosingNode.type)) { | ||
// we need to store this as a dangling comment in case the type is nullable | ||
@@ -621,7 +619,11 @@ // ie function(): ?string {} - the "nullable" attribute is part of the | ||
function handleCommentInEmptyParens(text, enclosingNode, comment, options) { | ||
function handleCommentInEmptyParens( | ||
text, | ||
enclosingNode, | ||
comment | ||
/* options */ | ||
) { | ||
const nextCharIndex = getNextNonSpaceNonCommentCharacterIndex( | ||
text, | ||
comment, | ||
options | ||
locEnd(comment) | ||
); | ||
@@ -803,4 +805,4 @@ | ||
followingNode, | ||
comment, | ||
options | ||
comment | ||
/* options */ | ||
) { | ||
@@ -818,4 +820,3 @@ if (!enclosingNode || enclosingNode.kind !== "while" || !followingNode) { | ||
text, | ||
comment, | ||
options | ||
locEnd(comment) | ||
); | ||
@@ -883,7 +884,7 @@ const nextCharacter = text.charAt(nextCharIndex); | ||
function hasLeadingOwnLineComment(text, node, options) { | ||
function hasLeadingOwnLineComment(text, node) { | ||
return ( | ||
node.comments && | ||
node.comments.some( | ||
(comment) => comment.leading && hasNewline(text, options.locEnd(comment)) | ||
(comment) => comment.leading && hasNewline(text, locEnd(comment)) | ||
) | ||
@@ -903,3 +904,3 @@ ); | ||
if ( | ||
isNextLineEmpty(options.originalText, comment, options) && | ||
isNextLineEmpty(options.originalText, locEnd(comment)) && | ||
!isLastComment | ||
@@ -958,5 +959,3 @@ ) { | ||
contents, | ||
hasNewline(options.originalText, options.locEnd(comment)) | ||
? hardline | ||
: " ", | ||
hasNewline(options.originalText, locEnd(comment)) ? hardline : " ", | ||
]; | ||
@@ -979,3 +978,3 @@ } | ||
if ( | ||
hasNewline(options.originalText, options.locStart(comment), { | ||
hasNewline(options.originalText, locStart(comment), { | ||
backwards: true, | ||
@@ -998,4 +997,3 @@ }) | ||
options.originalText, | ||
comment, | ||
options | ||
locStart(comment) | ||
); | ||
@@ -1035,3 +1033,3 @@ | ||
const text = options.originalText; | ||
if (hasNewline(text, skipNewline(text, options.locEnd(comment)))) { | ||
if (hasNewline(text, skipNewline(text, locEnd(comment)))) { | ||
leadingParts.push(hardline); | ||
@@ -1038,0 +1036,0 @@ } |
@@ -19,2 +19,3 @@ import { doc } from "prettier"; | ||
import { hasPragma, insertPragma } from "./pragma.js"; | ||
import { locStart, locEnd } from "./loc.js"; | ||
@@ -59,6 +60,2 @@ const { join, hardline } = doc.builders; | ||
const loc = (prop) => (node) => { | ||
return node.loc && node.loc[prop] && node.loc[prop].offset; | ||
}; | ||
const parsers = { | ||
@@ -68,4 +65,4 @@ php: { | ||
astFormat: "php", | ||
locStart: loc("start"), | ||
locEnd: loc("end"), | ||
locStart, | ||
locEnd, | ||
hasPragma, | ||
@@ -144,3 +141,3 @@ }, | ||
} | ||
/* istanbul ignore next */ | ||
/* c8 ignore next 2 */ | ||
default: | ||
@@ -147,0 +144,0 @@ throw new Error(`Not a comment: ${JSON.stringify(comment)}`); |
@@ -1,2 +0,1 @@ | ||
import assert from "assert"; | ||
import { getPrecedence, shouldFlatten, isBitwiseOperator } from "./util.js"; | ||
@@ -95,4 +94,2 @@ | ||
if (pp === np && key === "right") { | ||
assert.strictEqual(parent.right, node); | ||
return true; | ||
@@ -99,0 +96,0 @@ } |
174
src/util.js
@@ -1,37 +0,6 @@ | ||
import { util as prettierUtil, version as prettierVersion } from "prettier"; | ||
import { util as prettierUtil } from "prettier"; | ||
import { locStart } from "./loc.js"; | ||
const { | ||
hasNewline, | ||
skipEverythingButNewLine, | ||
skipNewline, | ||
isNextLineEmpty: _isNextLineEmpty, | ||
isPreviousLineEmpty: _isPreviousLineEmpty, | ||
getNextNonSpaceNonCommentCharacterIndex: | ||
_getNextNonSpaceNonCommentCharacterIndex, | ||
} = prettierUtil; | ||
const { hasNewline, skipEverythingButNewLine, skipNewline } = prettierUtil; | ||
function lookupIfPrettier2(options, prop) { | ||
return parseInt(prettierVersion[0]) > 1 ? options[prop] : options; | ||
} | ||
function isPreviousLineEmpty(text, node, options) { | ||
return _isPreviousLineEmpty( | ||
text, | ||
node, | ||
lookupIfPrettier2(options, "locStart") | ||
); | ||
} | ||
function isNextLineEmpty(text, node, options) { | ||
return _isNextLineEmpty(text, node, lookupIfPrettier2(options, "locEnd")); | ||
} | ||
function getNextNonSpaceNonCommentCharacterIndex(text, node, options) { | ||
return _getNextNonSpaceNonCommentCharacterIndex( | ||
text, | ||
node, | ||
lookupIfPrettier2(options, "locEnd") | ||
); | ||
} | ||
function printNumber(rawNumber) { | ||
@@ -55,45 +24,43 @@ return ( | ||
// http://php.net/manual/en/language.operators.precedence.php | ||
const PRECEDENCE = {}; | ||
[ | ||
["or"], | ||
["xor"], | ||
["and"], | ||
const PRECEDENCE = new Map( | ||
[ | ||
"=", | ||
"+=", | ||
"-=", | ||
"*=", | ||
"**=", | ||
"/=", | ||
".=", | ||
"%=", | ||
"&=", | ||
"|=", | ||
"^=", | ||
"<<=", | ||
">>=", | ||
], | ||
["??"], | ||
["||"], | ||
["&&"], | ||
["|"], | ||
["^"], | ||
["&"], | ||
["==", "===", "!=", "!==", "<>", "<=>"], | ||
["<", ">", "<=", ">="], | ||
[">>", "<<"], | ||
["+", "-", "."], | ||
["*", "/", "%"], | ||
["!"], | ||
["instanceof"], | ||
["++", "--", "~"], | ||
["**"], | ||
].forEach((tier, i) => { | ||
tier.forEach((op) => { | ||
PRECEDENCE[op] = i; | ||
}); | ||
}); | ||
function getPrecedence(op) { | ||
return PRECEDENCE[op]; | ||
["or"], | ||
["xor"], | ||
["and"], | ||
[ | ||
"=", | ||
"+=", | ||
"-=", | ||
"*=", | ||
"**=", | ||
"/=", | ||
".=", | ||
"%=", | ||
"&=", | ||
"|=", | ||
"^=", | ||
"<<=", | ||
">>=", | ||
], | ||
["??"], | ||
["||"], | ||
["&&"], | ||
["|"], | ||
["^"], | ||
["&"], | ||
["==", "===", "!=", "!==", "<>", "<=>"], | ||
["<", ">", "<=", ">="], | ||
[">>", "<<"], | ||
["+", "-", "."], | ||
["*", "/", "%"], | ||
["!"], | ||
["instanceof"], | ||
["++", "--", "~"], | ||
["**"], | ||
].flatMap((operators, index) => | ||
operators.map((operator) => [operator, index]) | ||
) | ||
); | ||
function getPrecedence(operator) { | ||
return PRECEDENCE.get(operator); | ||
} | ||
@@ -191,10 +158,2 @@ | ||
function getParentNodeListProperty(path) { | ||
const { parent } = path; | ||
if (!parent) { | ||
return null; | ||
} | ||
return getNodeListProperty(parent); | ||
} | ||
function getLast(arr) { | ||
@@ -214,11 +173,2 @@ if (arr.length > 0) { | ||
function isLastStatement(path) { | ||
const body = getParentNodeListProperty(path); | ||
if (!body) { | ||
return true; | ||
} | ||
const { node } = path; | ||
return body[body.length - 1] === node; | ||
} | ||
function isFirstChildrenInlineNode(path) { | ||
@@ -538,19 +488,2 @@ const { node } = path; | ||
function getNextNode(path, node) { | ||
const { parent } = path; | ||
const children = getNodeListProperty(parent); | ||
if (!children) { | ||
return null; | ||
} | ||
const index = children.indexOf(node); | ||
if (index === -1) { | ||
return null; | ||
} | ||
return parent.children[index + 1]; | ||
} | ||
function isProgramLikeNode(node) { | ||
@@ -612,3 +545,3 @@ return ["program", "declare", "namespace"].includes(node.kind); | ||
function isNextLineEmptyAfterNamespace(text, node, locStart) { | ||
function isNextLineEmptyAfterNamespace(text, node) { | ||
let idx = locStart(node); | ||
@@ -673,13 +606,11 @@ idx = skipEverythingButNewLine(text, idx); | ||
]; | ||
const MagicMethodsMap = magicMethods.reduce((map, obj) => { | ||
map[obj.toLowerCase()] = obj; | ||
const magicMethodsMap = new Map( | ||
magicMethods.map((name) => [name.toLowerCase(), name]) | ||
); | ||
return map; | ||
}, {}); | ||
function normalizeMagicMethodName(name) { | ||
const loweredName = name.toLowerCase(); | ||
if (MagicMethodsMap[loweredName]) { | ||
return MagicMethodsMap[loweredName]; | ||
if (magicMethodsMap.has(loweredName)) { | ||
return magicMethodsMap.get(loweredName); | ||
} | ||
@@ -696,7 +627,4 @@ | ||
nodeHasStatement, | ||
getNodeListProperty, | ||
getParentNodeListProperty, | ||
getLast, | ||
getPenultimate, | ||
isLastStatement, | ||
getBodyFirstChild, | ||
@@ -722,7 +650,3 @@ lineShouldEndWithSemicolon, | ||
getAncestorNode, | ||
getNextNode, | ||
normalizeMagicMethodName, | ||
isPreviousLineEmpty, | ||
isNextLineEmpty, | ||
getNextNonSpaceNonCommentCharacterIndex, | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24
15
343
323479
5362